2

I'm trying to insert some data into a database using an html form. The form is coded in html, the database is managed using MySQL and I'm using php to insert data. I think the problem is not on the code but in the php set-up (I include the code just in case).

I've installed MAMP (I'm working on a Snow Leopard Mac). PHP seems to be working properly (If I enter commands on the terminal everything works fine)

The action of the form is a php file called insertar.php

The problem is that when I click on the submit button after filling the form, the browser shows the code for insertar.php but it doesn't insert.

I would greatly appreciate your help.

PHP CODE:

<?php

$conn = mysql_connect("localhost","root","root");
if (!$conn)
  {
  die('Imposible efectuar la conexion' . mysql_error());
  }

mysql_select_db("zealot", $conn);

$id    = $_POST["id"];
$date  = $_POST["date"];
$name  = $_POST["name"];


mysql_query("insert into logistica_rastreo values('$id' , '$date' , '$name');");  


?>

HTML CODE

<form name="the_form" method="post" action="insertar.php">
    <table>
    id:        <input type="text" size="30" maxlength="40" name="id"/>                          
    date:      <input type="text" size="30" maxlength="40" name="date"/>           
    name:      <input type="text" size="30" maxlength="40" name="name"/>              
  </table>
  <br/>
  <input type="submit" value="Registrar" name="registrar" />
  <br/>
</form>
5
  • If your server shows code for insertar.php (which is type of PHP), than your server has not allowed PHP. Commented Sep 6, 2011 at 17:20
  • I run insertar.php removing every line that has $_POSTand using a manual insert in mysql_query and it works!. I think the php file is correct. [By the way, this is the first php script that I'm running in this computer] Commented Sep 6, 2011 at 17:23
  • 6
    Your server appears to be setup not to execute PHP, but it merely shows the PHP code. If you're using apache, are you sure mod_php is enabled? Commented Sep 6, 2011 at 17:25
  • @Konerak I´ve taken a look at the output of phpinfo and it says that mod_php5 is loaded. Any ideas why this is happening? Commented Sep 6, 2011 at 20:06
  • phpinfo works, but your other php code doesn't? So if you create file.php with contents <? phpinfo(); ?>, that executes? And otherfile.php with contents <? die('test 123'); ?> does not? Commented Sep 7, 2011 at 11:38

2 Answers 2

1

Hi deps_stats, you HTML and PHP code is perfectly ok as far as i can verify this...

You need to check your server settings from cpanel, and check whether query modules (i.e execution, deletion,etc are enabled or not)..

This problem is at your server level not at code level... Hope this would help..

Sign up to request clarification or add additional context in comments.

1 Comment

if you dont get, we could discuss overthis again... :)
0

Not sure if this is the problem, but it's good practice to always make sure to specify which columns you want to insert into. For example, instead of this:

mysql_query("insert into logistica_rastreo values('$id' , '$date' , '$name');");  

do something like this:

mysql_query("insert into logistica_rastreo(id,date,name) values('$id' , '$date' , '$name');");  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.