php 如何插入到数据库中的特定行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/689898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert into specific row in database?
提问by Simon Lehmann
Okay, so my database is as follows by this order:
好的,所以我的数据库按这个顺序如下:
id (primary - auto_increment), username (unique), password, fname, mail
id (primary - auto_increment), username (unique), password, fname, mail
I have it so that users register a username and password and it adds it to the database, first of all. No username can be the same, and when it does add the data to the database it auto increments the ID. All of that works, but now I made an account settings page in which the user can change their email and first name, but it isn't working. I have the user enter variables in a form on one page, and it posts their first name as ufname (for update first name) and umail (for update mail). Then on the next page which updates the database I have this code:
我有它,以便用户首先注册用户名和密码并将其添加到数据库中。用户名不能相同,当它确实将数据添加到数据库时,它会自动增加 ID。所有这些都有效,但现在我制作了一个帐户设置页面,用户可以在其中更改他们的电子邮件和名字,但它不起作用。我让用户在一个页面上的表单中输入变量,并将他们的名字发布为 ufname(用于更新名字)和 umail(用于更新邮件)。然后在更新数据库的下一页上,我有以下代码:
session_start();
if(!isset($_SESSION['userId'])) {
die(" You are not signed in. Please <a href='login.php'>click here</a> to sign in.");
} else {
$changeTXT = $_SESSION['username'];
$changeTXT = strtolower($changeTXT);
$changeTXT = ucfirst($changeTXT);
echo "Account Settings: <font color='red'>" . $changeTXT . "</font><br /><br />";
$ufname = $_POST['ufname'];
$umail = $_POST['umail'];
mysql_connect("localhost", "root", "sp1151") or die(mysql_error());
mysql_select_db("usersys") or die(mysql_error());
mysql_query("INSERT INTO userdb (id, username, password, fname, mail) VALUES('','','','$ufname', '$umail') ");
echo $umail . "<br /><br />";
echo $ufname;
}
Oh, I also have the users logged in on sessions too.
哦,我也让用户也登录了会话。
But how would I insert the first name and e-mail the user enters into their specific row on the database? My database name is userdb.
但是我如何将用户输入的名字和电子邮件插入数据库中的特定行?我的数据库名称是 userdb。
回答by David Snabel-Caunt
You need to run an UPDATE query to alter an existing row, not an INSERT.
您需要运行 UPDATE 查询来更改现有行,而不是 INSERT。
$sql = "UPDATE userdb SET fname = '$ufname', mail = '$umail' WHERE id = $id";
mysql_query($sql);
回答by Simon Lehmann
You need to do use and UPDATEstatement instead of an INSERT:
你需要做 use 和UPDATEstatement 而不是 an INSERT:
UPDATE userdb SET fname = ?, mail = ? WHERE username = ?;
That aside you should seriously consider using prepared statements with query parameters to prevent SQL injection attacs.
除此之外,您应该认真考虑使用带有查询参数的准备好的语句来防止 SQL 注入攻击。
回答by Simon Lehmann
Try this UPDATEquery:
试试这个UPDATE查询:
Update 'tableName' set 'columnName' = 'newEntry' where 'rowID' = 'value'.
回答by chosta
Have you considered using the SQL UPDATEquery?
您是否考虑过使用 SQLUPDATE查询?
回答by Maximiliano Guzman
Try this:
尝试这个:
mysql_query("UPDATE userdb SET fname = '$ufname', umail = '$umail' WHERE id = '$_SESSION['userId']' ");

