How to set-up php in order to insert data into a database?
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开发者_C百科'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>
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..
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');");
精彩评论