我有一个连接到mysql数据库的php页面。我知道与数据库的连接很好,因为我有一个PHP代码,可以将数据库中的信息显示在网页上。当我尝试将新数据插入数据库时,页面刷新并且不插入数据。我检查过以确保insert into命令具有正确的值。
<?php
if (isset($_POST['User_Name']))
{
include "connect_to_mysql.php";
$name = mysql_real_escape_string($_POST["Name"]);
$sql = mysql_query("SELECT TestID FROM test WHERE Name='$name' LIMIT 1")or die (mysql_error());
$productMatch = mysql_num_rows($sql);
if ($productMatch > 0)
{
echo 'Sorry you tried to place a duplicate "User Account" into the system, <a href="index.php">click here</a>';
exit();
}
else
{
$sql = mysql_query("INSERT INTO test (TestID,Name)
VALUES('', '$name')") or die (mysql_error());
$uid = mysql_insert_id();
header("location: index.php");
exit();
}
}
?>
<?php
include "connect_to_mysql.php";
$User_list = "";
$sql = mysql_query("SELECT * FROM test");
$UserCount = mysql_num_rows($sql);
if ($UserCount > 0)
{
while($row = mysql_fetch_array($sql))
{
$id = $row["TestID"];
$name = $row["Name"];
$User_list .= "Users ID: $id - <strong>$name</strong> <br />";
}
}
else
{
$User_list = "You have no users listed in the database.";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<div align="center" id="mainWrapper">
<div id="pageContent"><br />
<div align="right" style="margin-right:32px;"><a href="index.php#UserForm">+ Add New User</a></div>
<div align="left" style="margin-left:24px;">
<h2>User list</h2>
<?php echo $User_list; ?>
</div>
<hr />
<a name="UserForm" id="UserForm"></a>
<h3>
↓ Add New User Form ↓
</h3>
<form action="index.php" enctype="multipart/form-data" name="myForm" id="myform" method="post">
<table width="90%" border="0" cellspacing="0" cellpadding="6">
<tr>
<td width="20%" align="right">Name</td>
<td width="80%"><label>
<input name="name" type="text" id="name" size="50" />
</label></td>
</tr>
<tr>
<td> </td>
<td><label>
<input type="submit" name="button" id="button" value="Add This Name Now" />
</label></td>
</tr>
</table>
</form>
<br />
<br />
</div>
</div>
</body>
</html>
答案 0 :(得分:0)
我马上看到两个问题(可能还有更多)。首先,PHP数组键区分大小写。您正在访问$_POST['Name']
,但表单输入为name
。其次,您正在测试$_POST['User_Name']
,它似乎不存在于任何地方:
// Look for name in the $_POST
if (isset($_POST['name']))
{
include "connect_to_mysql.php";
// name is case-sensitive
$name = mysql_real_escape_string($_POST["name"]);
稍后,如果您的表在TestID
上有一个AUTO_INCREMENT标识,您应该省略它或在插入语句中插入NULL:
// Don't include TestID if it is AUTO_INCREMENT. That will happen automatically
$sql = mysql_query("INSERT INTO test (Name)
VALUES('$name')") or die (mysql_error());
答案 1 :(得分:0)
我认为如果你改变
会有效 if (isset($_POST['User_Name']))
到
if (isset($_POST['Name']))
您检查表单中是否存在某些内容。
答案 2 :(得分:0)
的增加:强> 的
如果TestID
是自动增量,请更改以下
"INSERT INTO test (TestID,Name) VALUES('', '$name')"
到
"INSERT INTO test (Name) VALUES('$name')"
答案 3 :(得分:0)
如果您没有收到任何错误,这意味着您在MySQL语法中有错误,那么测试它的两种方法是将语法复制到PHPMyAdmin或您的本机MySQL命令行,并查看是否出现输出错误。或者你可以做的另一件事是修改你的所有mysql_query()
;添加mysql_query()or die(mysql_error());