Jump to content

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

What output from your code are you getting and are you developing and debugging this on a system with error_reporting set to E_ALL and display_errors set to ON so that you immediately see any php generated errors?

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.