如何使用php在mysql中搜索记录?
感谢您的帮助,我的数据库操作程序现在有了添加、更新和删除功能。 但我在 mysql 数据库中搜索记录时遇到困难。 请帮助我纠正我的代码,如果您知道我可以用作参考的网站,请告诉我。谢谢,这是我的搜索代码:
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("koro", $con);
mysql_query("UPDATE student
WHERE IDNUMBER ='$_POST[INAME]'");
while ($row = mysql_fetch_array($query))
{
$variable1=$row["IDNUMBER"];
$variable2=$row["LNAME"];
$variable3=$row["FNAME"];
$variable3=$row["MNAME"];
$variable3=$row["GRADEYR"];
$variable3=$row["ADDRESS"];
}
mysql_close($con);
?>
这是我正在使用的 html 表单,我认为这就是问题所在。我不知道如何将结果放入文本框中。
Thanks for all your help, I now have add, update, and delete functions on my database manipulation program.
But I'm having difficulty on searching for records in mysql database.
Please help me in correcting my codes, and if you know a site that I can use as my reference please do tell. Thanks, here is my search code:
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("koro", $con);
mysql_query("UPDATE student
WHERE IDNUMBER ='$_POST[INAME]'");
while ($row = mysql_fetch_array($query))
{
$variable1=$row["IDNUMBER"];
$variable2=$row["LNAME"];
$variable3=$row["FNAME"];
$variable3=$row["MNAME"];
$variable3=$row["GRADEYR"];
$variable3=$row["ADDRESS"];
}
mysql_close($con);
?>
And here is the html form that I'm using, and I think this is where the problem is. I don't have any idea on how to put the results in the text box.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查
mysql_query
的文档:它为UPDATE< 返回 TRUE 或 FALSE /code> 查询。如果您需要新值,请在
UPDATE
之后进行选择。更重要的是,您的代码容易受到 SQL 注入的攻击。最好的解决方案是从过时的 mysql 驱动程序切换到 PDO。
将数组解包到单独的变量中(就像您所做的那样)是不必要的。这也是有问题的:请注意您如何将最后几个名称错误地输入为
$variable3
。另外,不要使用“or die”(除非在非常有限的情况)。
Check the documentation for
mysql_query
: it returns TRUE or FALSE forUPDATE
queries. If you want the new values, follow theUPDATE
with a select.More importantly, your code is vulnerable to SQL injection. The best solution is to switch from the outdated mysql driver to PDO.
Unpacking an array into separate variables (as you do) is unnecessary. It's also problematic: notice how you've mistyped the last few names as
$variable3
.Also, don't use "or die" (except in very limited circumstances).
不要在代码中使用
die ();
并定义要查询的变量来传递参数或在参数中写入查询。有关更多信息,请查看 PHP 官方网站或 w3school 上的文档。Don't use
die ();
in code and define the variable to query to pass an argument or write query in argument. For further information check the documentation on PHP official web site or w3school.