簡體   English   中英

PHP-MYSQL 更新錯誤

[英]PHP-MYSQL Update Error

在我的數據庫中,在 'users' 表中有列如 uname 、 name 、 sname 、 password 和 email 。 'uname' 是獨一無二的。
我無法更新姓名、姓名和電子郵件值。
我收到“

Error updating record: Erreur de syntaxe près de 'manager''' à la ligne 2 

首先我不知道為什么它是法語。 'manager' 是我在數據庫中定義的用戶名。
$uname = $_SESSION['username']; 值來自之前的腳本。

<?php
require_once("db_functions.php");
session_start();

$uname =  $_SESSION['username'];
$new_name="";
$new_sname="";
$new_email="";

if( !(isset($_SESSION['update'])) ||  $_SESSION['update'] != "1" )
{
$errorMsg= "Problem has occured in Update page";
echo $errorMsg;
// header  can be added.
}

else
{

 if(isset($_POST['Submit_update']))
    {
        $conn=db_connect();
       if ($conn) 
       {
        $SQL_select="SELECT * FROM users WHERE username=$uname";
        $select_result=mysqli_query($conn,$SQL_select);

        $new_name=mysqli_real_escape_string($conn,$_POST['name']);
        $new_sname=mysqli_real_escape_string($conn,$_POST['sname']);
        $new_email=mysqli_real_escape_string($conn,$_POST['email']);

      $SQL_update="UPDATE users SET name='$new_name', sname='$new_sname',
       email='$new_email' WHERE uname='$uname'";
      $update_result=mysqli_query($conn,$SQL_update);

        if ($update_result) { echo "Record updated successfully"; }
        else {  echo "Error updating record: " . mysqli_error($conn); }     
   mysqli_close($conn);

       }
else {
    $errorMsg=" Fail to Connect Database";
    echo $errorMsg;
  }   

    }


  }


?>

<!DOCTYPE html>
<html>
<head>
<title>Upload Page</title>
</head>
<body>
<form name="Update_Form" method="post" action="update.php">
    Name:<input type="text" name="name" value=""/><br/>
    <P>
    Surname:<input type="text" name="sname" value=""/><br/>
    <P>
    E-Mail:<input type="text" name="email" value=""/><br/>

    <input type="submit" name="Submit_update" value="Update"/>
</form>
</body>


</html>

我懷疑$uname有引號。 由於您沒有轉義$uname ,因此它結束了字符串值。

您應該使用准備好的查詢而不是替換變量,然后您就不需要轉義任何內容。

$stmt_update = mysqli_prepare($conn, "UPDATE users SET name= ?, sname= ?,
   email=? WHERE uname=?") or die("Error preparing update: " . mysqli_error($conn);
mysqli_stmt_bind_param($stmt_update, "ssss", $_POST['name'], $_POST['sname'], $_POST['email'], $uname);
mysqli_stmt_execute($stmt_update) or die(echo "Error updating record: " . mysqli_stmt_error($stmt_update));

如果您在$uname的值周圍有引號, where username=$uname在不將引號放入查詢的情況下工作,則不應該這樣做,它會使其余代碼更難使用該變量。 它將阻止上述查詢工作,因為它將在表內容中查找文字引號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM