Jump to content

web form will not write to database


sabo

Recommended Posts

I wrote a small web form and it does not seem to transfer data to the php script.

 

Here is the html form

 

<form  method="post" action="insert.php">
<BR>
<BR>
<BR>
<center>
<table>

<tr> Enter your weight Watcher Food to keep track of your points.

<td>Enter the Food:</td>

<td><input type="text" name="food"></td>

</tr>
<BR>
<tr> Enter the Points for the food you ate.

<td>Points:</td>

<td><input type="text" name="points"></td>

</tr>


<tr>

<td><input type="submit" name="submit" value="Enter"></td>

</tr>
</center>
</form>

 

 

this php script works and enters the data into the databse when executed on the command line

 

#php insert.php

 

# cat insert.php
<?php

$food = $_POST['food'];
$points = $_POST['points'];
// Make a MySQL Connection
$con = mysql_connect("localhost", "root", "pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("watchers", $con);

// Insert a row of information into the table "example"
#$sql="INSERT INTO Meal (food, points)  VALUES ('$_POST[food]','$_POST[points]')";
$sql="INSERT INTO Meal (food, points)  VALUES ('$food','$points')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";

mysql_close($con)
?>

 

 

Any Ideas?

 

Thanks for reading

Link to comment
https://forums.phpfreaks.com/topic/144885-web-form-will-not-write-to-database/
Share on other sites

Thanks for the tips.    PFMaBiSmAd    and    9three

 

 

I have enabled display_errors.  It was set to off

 

 

E_ALL  was already enabled in /etc/php.ini

 

9three

 

Could you elaborate on your comment.  This is my first mysql php script. 

Can you mention the change that will send the DB my query.

 

thanks

A few changes:

 

# cat insert.php
<?php

$food = $_POST['food'];
$points = $_POST['points'];
// Make a MySQL Connection
$con = mysql_connect("localhost", "root", "pass") or die('Could not connect: ' . mysql_error());

mysql_select_db("watchers", $con);

// Insert a row of information into the table "example"
#$sql="INSERT INTO Meal (food, points)  VALUES ('$_POST[food]','$_POST[points]')";
$sql="INSERT INTO Meal (food, points)  VALUES ('$food','$points')";

// second param is not necessary
mysql_query($sql) or die(die('SQL Was: ' . $sql . "\nError: " . mysql_error());

echo "1 record added";

// not needed mysql_close($con);
?>

 

See what that does for you.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.