0

我正在尝试让我的 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)
?>
4

6 回答 6

6

您的表单有一个名为firstnamenot的输入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'];
于 2013-05-21T11:24:03.383 回答
1
姓名:
<?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)
}
?>
于 2013-05-21T11:26:11.440 回答
0

感谢您的所有帮助,我通过拉扯头发解决了这个问题。我想出了

    <?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)
?>
于 2013-05-22T10:39:34.473 回答
0

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

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

    error_reporting = E_ALL

替换为/

    error_reporting = E_ALL & ~E_NOTICE

然后保存文件并重启wamp

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

于 2013-05-22T07:13:27.783 回答
0

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

<?php

print_r($_POST);

?>
于 2013-05-21T12:02:45.243 回答
0

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

您应该在使用之前尝试查看这些值是否存在。

例如

if (isset($_POST['xxx']))
{
/* Do Something */
}
于 2013-05-21T12:21:05.130 回答