我想使用PHP将数据从表单更新到数据库。但是我的代码出现了太多问题,例如"无法更新数据:未知列' Nabeel'在'字段列表'"。
请检查代码并给我解决方案。
谢谢, 纳比尔。
<!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>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
function doSomething() {
alert('Are you sure you want to delete this file ??');
}
</script>
</head>
<body>
<h2>Update Data Using PHP</h2>
</div>
<?php
$con = mysql_connect("localhost", "root", "");
$db = mysql_select_db("firstphp", $con);
if (isset($_GET['submit'])) {
$id = $_GET['id'];
$username = $_GET['username'];
$passward = $_GET['passward'];
$name = $_GET['name'];
$query = mysql_query("update users set
username='$username', passward='$passward', name='$name' where id='$id'", $con);
}
$query = mysql_query("select * from users", $con);
while ($row = mysql_fetch_array($query))
{
echo "<b><a href='update.php?update={$row['id']}'>{$row['id']}</a></b>";
echo "<br />";
}
?>
</div><?php
if (isset($_GET['submit'])) {
$submit = $_GET['submit'];
$query1 = mysql_query("select * from users where id=$submit", $con);
while ($row1 = mysql_fetch_array($query1)) {
echo "<form method='get'>";
echo "<hr/>";
echo"<input class='input' type='hidden' name='id' value='{$row1["id"]}' />";
echo "<br />";
echo "<label>" . "Name:" . "</label>" . "<br />";
echo"<input class='input' type='text' name='username' value='{$row1["username"]}' />";
echo "<br />";
echo "<label>" . "Email:" . "</label>" . "<br />";
echo"<input class='input' type='text' name='passward' value='{$row1["passward"]}' />";
echo "<br />";
echo "<label>" . "Name:" . "</label>" . "<br />";
echo"<input class='input' type='text' name='name' value='{$row1["name"]}' />";
echo "<br />";
echo "<input class='submit' type='submit' name='submit' value='submit' />";
echo "</form>";
}
}
if (isset($_GET['submit'])) {
echo '<div class="form" id="form3"><br><br><br><br><br><br>
<Span>Data Updated Successfuly......!!</span></div>';
}
mysql_close($con);
?>
</bod
></body>
</html>
答案 0 :(得分:0)
$query1 = mysql_query("select * from users where id=$submit", $con);
更改为
$query1 = mysql_query("select * from users where id='".$submit."'", $con);
答案 1 :(得分:0)
试试这个:
$query1 = mysql_query("select * from users where id={$submit}", $con);
大括号将确保您的PHP变量被解释,而不是简单的字符串。
答案 2 :(得分:0)
1)您不能使用where id=$submit
,因为Id需要是一个整数。
$submit
始终具有值submit
(因为您将其设置为:
echo "<input class='submit' type='submit' name='submit' value='submit' />";
您想要使用此行中的ID值:
echo"<input class='input' type='hidden' name='id' value='{$row1["id"]}' />";
2)替换
$query1 = mysql_query("select * from users where id=$submit", $con);
带
$query1 = mysql_query("select * from users where id=$id", $con);
并确保id
是一个整数。
3)不要使用mysql,而是使用mysqli:The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
4)你拼错了password
这个词。从&#39; passward&#39;变更到password
5)你的HTML在最后被破坏了:
</bod
></body>
</html>
删除</bod >
6)无需使用HTML4 doctype声明。取代
<!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">
与
<!DOCTYPE html>