php不向mysql传递数据
我有一个连接到mysql数据库的php页面。我知道与数据库的连接很好,因为我有一个php代码,可以将数据库中的信息显示在网页上。当我尝试将新数据插入数据库时,页面会刷新,数据不会插入。我已检查以确保insert into命令具有正确的值php不向mysql传递数据,php,mysql,Php,Mysql,我有一个连接到mysql数据库的php页面。我知道与数据库的连接很好,因为我有一个php代码,可以将数据库中的信息显示在网页上。当我尝试将新数据插入数据库时,页面会刷新,数据不会插入。我已检查以确保insert into命令具有正确的值 <?php if (isset($_POST['User_Name'])) { include "connect_to_mysql.php"; $name = m
<?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>
我马上看到两个问题,可能还有更多问题。首先,PHP数组键区分大小写。您正在访问$\u POST['Name'],但表单输入是Name。其次,您正在测试$u POST['User\u 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上有一个自动增量id,则应该忽略它或在insert语句中插入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());
我想如果你改变,它会起作用的
if (isset($_POST['User_Name']))
到
您检查表单中不存在的内容是否存在。添加:
如果TestID为自动增量,请更改以下选项
"INSERT INTO test (TestID,Name) VALUES('', '$name')"
到
如果您没有收到任何错误,这意味着您的MySQL语法有错误,有两种测试方法可以将语法复制到PHPMyAdmin或本机MySQL命令行中,然后查看是否有输出错误。或者,您可以做的另一件事是修改所有mysql_查询;通过添加mysql_queryor或diemsql_error的函数 也许你可以尝试打开一个MySQL实例,然后直接插入数据?因为当你试图用PHP将东西插入数据库时,它不会真正告诉你出了什么问题,但是如果你直接使用MySQL,它会告诉你出了什么问题,可能是一个未转义的引号或者一些次要但很难捕捉的东西。不建议再使用旧的MySQL_*函数。他们计划弃用。取而代之的是考虑使用一个支持准备好的语句的现代API,比如PDO或MyQLL.THNX。真不敢相信我错过了这么简单的事情。我猜花一整天的时间在编码上而没有休息不是一个好主意。
"INSERT INTO test (TestID,Name) VALUES('', '$name')"
"INSERT INTO test (Name) VALUES('$name')"