mysql post from html table


mysql post from html table

我正试图让我的sql从html表单发布信息到我的数据库,但不断获得:

( ! ) SCREAM: Error suppression ignored for
( ! ) Notice: Undefined index: firstname in C:'wamp'www'insert.php on line 29
Call Stack
#   Time    Memory  Function    Location
1   0.0005  247624  {main}( )   ..'insert.php:0

我使用的代码是:

<form action="insert.php" method="post">
   Name: <input type="text" name="firstname">
   <input type="submit" value="Submit">
</form>
<?php
// This is the connection to my database
$con = mysql_connect('127.0.0.1', 'shane', 'diamond89');
if (!$con){
   die('Could not Connect: ' . mysql_error());
}
// This selects which database i want to connect to
$selected = mysql_select_db("shane",$con);
if (!$con){
   die("Could not select data");
}
// This inserts new information to the Database
$name = $_POST['name'];
$query = "INSERT INTO test1 VALUES('id', '$name')";
mysql_query($query) or die('Error' . mysql_error());
// This closes my connection
mysql_close($con)
?>

您的表单有一个名为firstname而不是name的输入,因此$_POST['name']应该是$_POST['firstname']

改变
    // This inserts new information to the Database
    $name = $_POST['name'];

   // This inserts new information to the Database
   $name = $_POST['firstname'];
名称:
<?php
// This is the connection to my database
$con = mysql_connect('127.0.0.1', 'shane', 'diamond89');
if (!$con){
die('Could not Connect: ' . mysql_error());
}
// This selects which database i want to connect to
$selected = mysql_select_db("shane",$con);
if (!$con){
die("Could not select data");
}
if($_POST['firstname'])
{
// This inserts new information to the Database
$name = $_POST['firstname'];
$query = "INSERT INTO test1 VALUES('id', '$name')";
mysql_query($query) or die('Error' . mysql_error());
// This closes my connection
mysql_close($con)
}
?>

第一次查看页面时,$_POST未定义,因为表单尚未发布。一旦用户提交,$_POST将有效。

你应该在使用它之前试着看看这些值是否存在。

if (isset($_POST['xxx']))
{
/* Do Something */
}

提交表单后是否获得值?试一试:

<?php
print_r($_POST);
?>

我支持AB Åttìtúðê Þêrfëçt更正您的PHP代码的答案。但是,这可能会有所帮助:

通过wamp托盘图标,打开php.ini文件并找到

    error_reporting = E_ALL

替换w/

    error_reporting = E_ALL & ~E_NOTICE

保存文件并重启wamp

来自:http://forum.wampserver.com/read.php?2

, 72161年、72201年

谢谢你的帮助,我花了一点时间才解决了这个问题。我想到了

    <?php
// This is the connection to my database
$con = mysql_connect('127.0.0.1', 'shane', 'diamond89');
if (!$con){
die('Could not Connect: ' . mysql_error());
}
// This selects which database i want to connect to
$selected = mysql_select_db("shane",$con);
if (!$con){
die("Could not select examples");
}
$name=$_POST['firstname'];
$sql="INSERT INTO test1(name)VALUES('$name')";
$result=mysql_query($sql);
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='sql_table.php'>Back to main page</a>";

}

else {
echo "ERROR";

}

// This closes my connection
mysql_close($con)
?>