在 php 中更新 mysql 的一行
我有一个看起来像这样的表:
姓名用户名密码角色
学生 [电子邮件受保护] 学生
学生2 学生2 [电子邮件受保护] 学生2 学生
和我希望能够在 php 中编辑一行。 这些值取自 html 文件,如下所示:
用户名
[email ;受保护] <--这将写在文本框中
密码
Student2 <--这将写在文本框中
姓名
Student2 <--这将写在文本框中
姓氏
Student2 <--这将写在文本框中< /em>
角色
学生<--这将写在文本框中
我的 php 文件是:
<?php
$hostname = "localhost";
$database = "mydb";
$username = "myuser";
$password = "mypsw";
$link = mysql_connect( $hostname , $username , $password ) or
die("Prosoxi!Provlima stin sundesi me ton server : " . mysql_error());
mysql_select_db($database,$link);
mysql_query("UPDATE user
SET username = '".mysql_real_escape_string($_POST[nusername])."',
SET password = '".mysql_real_escape_string($_POST[npassword])."',
SET name = '".mysql_real_escape_string($_POST[nname])."',
SET surname = '".mysql_real_escape_string($_POST[nsurname])."',
SET role = '".mysql_real_escape_string($_POST[nrole])."'
WHERE username='".mysql_real_escape_string($_POST[us])."'");
mysql_close($link);
header("Location: users.php");
?>
1.更新没有发生,所以 php 文件中有一些错误,我找不到。
2.如果我选择某个用户名,如何才能在 html 文件中使用正确的值来实现已填充的框?
有人可以帮助我吗?先感谢您。 :)
I have a table that looks like this:
name surname username password role
student student [email protected] student student
student2 student2 [email protected] student2 student
and I want to be able to edit a row in php.
The values are taken from a html file like this:
Username
[email protected] <--This will be written in a text box
Password
student2 <--This will be written in a text box
Name
student2 <--This will be written in a text box
Surname
student2 <--This will be written in a text box
Role
student <--This will be written in a text box
My php file is:
<?php
$hostname = "localhost";
$database = "mydb";
$username = "myuser";
$password = "mypsw";
$link = mysql_connect( $hostname , $username , $password ) or
die("Prosoxi!Provlima stin sundesi me ton server : " . mysql_error());
mysql_select_db($database,$link);
mysql_query("UPDATE user
SET username = '".mysql_real_escape_string($_POST[nusername])."',
SET password = '".mysql_real_escape_string($_POST[npassword])."',
SET name = '".mysql_real_escape_string($_POST[nname])."',
SET surname = '".mysql_real_escape_string($_POST[nsurname])."',
SET role = '".mysql_real_escape_string($_POST[nrole])."'
WHERE username='".mysql_real_escape_string($_POST[us])."'");
mysql_close($link);
header("Location: users.php");
?>
1.The update does not happen, so there's something wrong in the php file, that I can't find.
2. How can I achieve already filled boxes in the html file, with the right values, if I choose a certain username?
Can someone help me? Thank you in advance. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这里发生了很多事情。
始终使用整数作为主键,例如:id MEDIUMINT NOT NULL AUTO_INCRMENT。然后将其设为主键。
您需要使用
mysql_real_escape_string()
清理您对数据库的输入您需要连接您的查询,因此它应该如下所示:
mysql_query("更新用户
SET 用户名 = '".mysql_real_escape_string($_POST[nusername])."'
SET 密码 = '".mysql_real_escape_string($_POST[npassword])."'
SET name = '".mysql_real_escape_string($_POST[nname])."'
SET surname = '".mysql_real_escape_string($_POST[nsurname])."'
SET 角色 = '".mysql_real_escape_string($_POST[nrole])."'
WHERE username='".mysql_real_escape_string($_POST[us])."'");
这是更正后的代码:
注意 _POST var 周围的单引号,$_POST['nusername'] 你有 $_POST[nusername]。
尝试现在,看看它是否更新。
There is a lot going on here.
Always us a integer as a primary key, example: id MEDIUMINT NOT NULL AUTO_INCREMENT. Then make it the primary key.
you need to sanitize your input to the database using
mysql_real_escape_string()
you need to concatenate your query, so it should look like this:
mysql_query("UPDATE user
SET username = '".mysql_real_escape_string($_POST[nusername])."'
SET password = '".mysql_real_escape_string($_POST[npassword])."'
SET name = '".mysql_real_escape_string($_POST[nname])."'
SET surname = '".mysql_real_escape_string($_POST[nsurname])."'
SET role = '".mysql_real_escape_string($_POST[nrole])."'
WHERE username='".mysql_real_escape_string($_POST[us])."'");
Here is corrected code:
notice the single quote surrounding the _POST var, $_POST['nusername'] you had $_POST[nusername].
Try it now, and see if it updates.
当您遇到查询问题时,请始终回显查询本身以查看是否正在处理正确的数据!此外,我会这样编写查询:
When you have a query issue, always echo the query itself to see if the correct data is going through! Furthermore, I would write the query like this:
如果其他一切都失败,则回显 SQL 语句,然后粘贴到 SQL Browser/PHPMyAdmin 上,然后在那里进行调试。然后您只需将代码替换为无错误的代码即可。
您需要确保您发送的数据也不受 sql 注入的影响。有人可能会绕过它..
If everything else fails echo the SQL statement then paste on SQL Browser/PHPMyAdmin then debug it there. Then you just replace the code with the error-free one.
You need to make sure that the data you are sending are sql-inject free as well. Someone might just bypass it..
您的查询需要修改如下。
此外,您还可能受到 SQL 注入攻击。所以你最好也使用
mysql_real_escape_string()
函数。http://php.net/manual/en/function.mysql -real-escape-string.php
另外,我想建议您采取一些步骤来解决此类问题。这只是一个例子。
步骤 1
当您的 PHP 代码中需要 SQL 语句时。您最好先将其编写在 MySQL 工具中,然后使用示例值进行测试。
第 2 步:
如果查询工作正常,则将其复制到 php.ini。并用
mysql_real_escape_string()
支持替换值。第 3 步:
执行您的查询。
步骤 4:
您可以查看是否有任何错误。
编辑:
为您解答问题2“如果我选择某个用户名,如何才能在html文件中使用正确的值来实现已填充的框?”可能是这样的。
首先,您必须编写一条选择语句并获取您想要的任何数据。前任。
然后输入您的 HTML 代码。前任:
Your query need to be modified as bellow.
Also you are open for the SQL Injection attacks. So you better use
mysql_real_escape_string()
function as well.http://php.net/manual/en/function.mysql-real-escape-string.php
Also I would like to suggest you few steps to over come this kind of issue. This is just an example.
Step 1
When you need a SQL statement in your PHP code. You better write it in your MySQL tool first and test it with sample values.
Step 2:
If the query works fine then copy it to php. And replace values with
mysql_real_escape_string()
support.Step 3:
Execute your query.
Step 4 :
You can see any if there any errors available.
EDIT:
Answer for you Question 2 "How can I achieve already filled boxes in the html file, with the right values, if I choose a certain username?" could be like this.
First you have to write a select statement and get whatever data you want. Ex.
Then put your HTML code. Ex:
显然您发布的最新代码存在语法错误,当您从帖子中获取数据时,您有
$_POST[nusername]
,它应该是$_POST['nusername'] 因为它是数组的索引,我还建议回显查询并注释标头调用,以便您可以看到发送到 MySQL 的查询是什么
Apparently there is a syntax error in the latest code you posted, when you obtain the data from post you have
$_POST[nusername]
and it should be$_POST['nusername']
since it is an index of the array, and I also recommend echoing the query and commenting the header call so you can see what is the query that is being sent to MySQL