我是一個完整的小白,所以我提前道歉,因爲我的無知/純粹的愚蠢。PHP/SQL更新用戶行問題
我試圖讓表單提交到數據庫的用戶行。我有這與INSERT工作,但是它只是把它放到一個新的行,而不是用戶的(我認爲)下面的代碼並沒有給出任何具體的錯誤,只是有一個(或更可能是很多)是這個適當的方式來解決這個問題?如果有的話,請指出問題,或指出解決方案的方向。
謝謝!
<?php
$user = $_SESSION['username'];
$con=mysqli_connect("localhost","USER","PASS","DB NAME");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error(); }
// escape variables for security
$title = mysqli_real_escape_string($con, $_POST['title']);
$firstname = mysqli_real_escape_string($con, $_POST['firstname']);
$lastname = mysqli_real_escape_string($con, $_POST['lastname']);
$jobtitle = mysqli_real_escape_string($con, $_POST['jobtitle']);
$address = mysqli_real_escape_string($con, $_POST['address']);
$postcode = mysqli_real_escape_string($con, $_POST['postcode']);
$telephone = mysqli_real_escape_string($con, $_POST['telephone']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$sql =
mysql_query("UPDATE users SET title='$title', firstname='$firstname', lastname='$lastname', jobtitle='$jobtitle', address='$address', postcode='$postcode', telephone='$telephone', email='$email' WHERE username='$user'");
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "Thank you, your details have now been submitted. From here you can Book and Pay for the Events you want to attend.";
mysqli_close($con);
?>
不要混合mysql_ *和mysqli_ *函數 – Phantom
我可能會建議,因爲您使用mysqli開始時考慮使用預處理語句,而不是轉義每個變量,只需將它們作爲命名參數傳遞給準備好的聲明。如果你在任何地方都使用它,將會阻止你永遠忘記逃避變量。 – scragar