trying to post data into table with php & sql - getting "Notice: Undefined index:..."
Here's the html:
<form action="php/buzz_data.php" method="get">
<div>Image: <input type="text" name="buzz_img" size="30" /></div>
<div>Link: <input type="text" name="buzz_link" size="30" /></div>
<div>Description: <textarea rows="5" cols="30" name="buzz_desc"></textarea></div>
<div>Title: <input type="text" name="buzz_title" size="30" /></div>
<input type="submit" value="Submit" />
<input type="submit" value="Clear" />
</form>
Here's the php:
<?php
$con = mysql_connect("开发者_如何学Clocalhost","***","***");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ubook247", $con);
$sql="INSERT INTO buzz_data (buzz_page_title, buzz_img, buzz_link, buzz_desc, buzz_title)
VALUES
('$_POST[buzz_page_title]','$_POST[buzz_img]','$_POST[buzz_link]','$_POST[buzz_desc]','$_POST[buzz_title]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
and here's the result:
Notice: Undefined index: buzz_img in C:\wamp\www\ubook247\php\buzz_data.php on line 12
Notice: Undefined index: buzz_link in C:\wamp\www\ubook247\php\buzz_data.php on line 12
Notice: Undefined index: buzz_desc in C:\wamp\www\ubook247\php\buzz_data.php on line 12
Notice: Undefined index: buzz_title in C:\wamp\www\ubook247\php\buzz_data.php on line 12
1 record added
The db and table are both correctly set up as a new record was created, it just didn't add the data from the inputs.
<form action="php/buzz_data.php" method="get">
You are using GET
, so access the values from the $_GET[]
variable, not from $_POST[]
. Or you can change the method to "post"
.
Also, say hello to Bobby Tables!
Maybe you should also try
$sql=sprintf("INSERT INTO buzz_data (buzz_page_title, buzz_img, buzz_link, buzz_desc, buzz_title)
VALUES (%s,%s,%s,%s)",
$_POST["buzz_img"],$_POST["buzz_link"],$_POST["buzz_desc"],$_POST["buzz_title"]);
Your array indices also should be in quotes, try something like
VALUES('{$_GET['buzz_page_title']}', '{$_GET['buzz_img']}', ...
in the VALUE
clause.
精彩评论