简体   繁体   中英

How to insert into specific row in database?

Okay, so my database is as follows by this order:

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:

session_start();
if(!isset($_SESSION['userId'])) {
die("&nbsp;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.

You need to run an UPDATE query to alter an existing row, not an INSERT.

$sql = "UPDATE userdb SET fname = '$ufname', mail = '$umail' WHERE id = $id";
mysql_query($sql);

You need to do use and UPDATE statement instead of 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.

试试这个UPDATE查询:

Update 'tableName' set 'columnName' = 'newEntry' where 'rowID' = 'value'.

您是否考虑过使用SQL UPDATE查询?

尝试这个:

mysql_query("UPDATE userdb SET fname = '$ufname', umail = '$umail' WHERE id = '$_SESSION['userId']' ");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM