2017-07-08 100 views
1

我想更新一個已經創建的數據庫和表的數據,但它給錯誤:SQL更新查詢錯誤在PHP

錯誤更新記錄:您的SQL語法錯誤;檢查對應於你的MySQL服務器版本正確的語法使用手動接近「其中EMPID = 1」在行1

這裏是代碼:

<!DOCTYPE html> 
<?php 
if(isset($_REQUEST['update'])) 
{ 
$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "bc140401259_DB"; 

// Create connection 
$conn = mysqli_connect($servername, $username, $password, $dbname); 
// Check connection 
if (!$conn) { 
    die("Connection failed: " . mysqli_connect_error()); 
} 

$employeeID=""; 
if (! empty($_POST['empID'])){ 
    $employeeID = $_POST['empID']; 
} 
$Name=""; 
if (! empty($_POST['name'])){ 
    $Name = $_POST['name']; 
} 
$Address=""; 
if (! empty($_POST['address'])){ 
    $Address = $_POST['address']; 
} 
$Dateofbirth=""; 
if (! empty($_POST['DOB'])){ 
    $Dateofbirth = $_POST['DOB']; 
} 
$Salary=""; 
if (! empty($_POST['salary'])){ 
    $Salary = $_POST['salary']; 
} 
$timestamp=""; 
if (! empty($_POST['timeIn'])){ 
    $timestamp = $_POST['timeIn']; 
} 

$sql = "UPDATE Employee SET Name = $Name, Address = $Address, Dateofbirth = $Dateofbirth, Salary = $Salary, Timein = $timestampemp where empID = $employeeID"; 

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

mysqli_close($conn); 
} 

?> 

<html> 
<body> 

<form action="" method="post"> 
<h2>Add Employee Form</h2> 
    Employee ID: <input type="text" name="empID" > 
    <br><br> 
    Name: <input type="text" name="name" > 
    <br><br> 

    Address: <input type="text" name="address" > 
    <br><br> 

    Date of Birth: <input type="text" name="DOB" > 
    <br><br> 

    Salary: <input type="text" name="salary" > 
    <br><br> 

    Time In: <input type="text" name="timeIn" > 
    <br><br> 

<input type="submit" name="update" value="UPDATE" /> 

</form> 
</body> 
</html> 
+0

php變量的名字是__case_sensitive__ –

+0

第二 - 你查詢將失敗,因爲你不知道引號。 –

+0

查看我的回答 –

回答

1

錯誤在這行

$sql = "UPDATE Employee SET Name = $Name, Address = $Address, Dateofbirth = $Dateofbirth, Salary = $Salary, Timein = $timestampemp where empID = $employeeID"; 

新代碼

 <?php 
    if(isset($_REQUEST['update'])) 
    { 
    $servername = "localhost"; 
    $username = "root"; 
    $password = ""; 
    $dbname = "bc140401259_DB"; 

    // Create connection 
    $conn = mysqli_connect($servername, $username, $password, $dbname); 
    // Check connection 
    if (!$conn) { 
     die("Connection failed: " . mysqli_connect_error()); 
    } 

    $employeeID=""; 
    if (! empty($_POST['empID'])){ 
     $employeeID = $_POST['empID']; 
    } 
    $Name=""; 
    if (! empty($_POST['name'])){ 
     $Name = $_POST['name']; 
    } 
    $Address=""; 
    if (! empty($_POST['address'])){ 
     $Address = $_POST['address']; 
    } 
    $Dateofbirth=""; 
    if (! empty($_POST['DOB'])){ 
     $Dateofbirth = $_POST['DOB']; 
    } 
    $Salary=""; 
    if (! empty($_POST['salary'])){ 
     $Salary = $_POST['salary']; 
    } 
    $timestamp=""; 
    if (! empty($_POST['timeIn'])){ 
     $timestamp = $_POST['timeIn']; 
    } 
    $sql = 'UPDATE Employee SET Name = "'.$Name.'", Address = "'.$Address.'", Dateofbirth = "'.$Dateofbirth.'", Salary = "'.$Salary.'", Timein = "'.$timestamp.'" where empID = '.$employeeID; 
    //echo $sql; die(); 

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

    ?> 

在這行數據類型錯誤與concodinate錯誤

+0

我粘貼了該行,現在錯誤更改爲: 更新記錄時出錯:您的SQL語法錯誤;檢查對應於您的MySQL服務器版本的手冊,以便在第一行''1'附近使用正確的語法 –

+0

我檢查了它的工作等待我將更新完整代碼 –

+0

檢查此代碼 –

1

你定義if裏面的變量塊。當if代碼塊退出時,該變量被銷燬,因此出現錯誤。您需要將變量移到if塊之外。

也許初始化變量,如:

$Name = ""; 
... 

,然後把你的if S,但沒有else塊:

if (...) { 
    $Name = ... 
} 

請注意,你的代碼是容易受到SQL注入,非常危險的攻擊。

0

你在這行有錯誤,

$sql = "UPDATE Employee SET empID = $employeeID, Name = $Name, Address = $address, Dateofbirth = $DOB, Salary = $salary, Timein = $timeIn"; 

你只是放錯位置的變量名$Dateofbirth$Salary$timestamp,與

$sql = "UPDATE Employee SET empID = $employeeID, Name = $Name, Address = $address, Dateofbirth = $Dateofbirth, Salary = $Salary, Timein = $timestamp"; 

取代這個,你也前人的精力用這樣來初始化變量,然後把值放在它們裏面,如果它們出現值

$employeeID=""; 
if (! empty($_POST['empID'])){ 
    $employeeID = $_POST['empID']; 
} 
+0

請檢查更新後的帖子 –

1

嘗試使用此

$sql = "UPDATE Employee SET empID ='". $employeeID."', Name = '".$Name."', Address = '".$Address."', Dateofbirth = '".$Dateofbirth."',Salary ='". $salary."', Timein = '".$timestamp."'"; 
1

簡單的查詢在$時間戳拼寫錯誤

<!DOCTYPE html> 
<?php 
if(isset($_REQUEST['update'])) 
{ 
$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "bc140401259_DB"; 

// Create connection 
$conn = mysqli_connect($servername, $username, $password, $dbname); 
// Check connection 
if (!$conn) { 
    die("Connection failed: " . mysqli_connect_error()); 
} 

$employeeID=""; 
if (! empty($_POST['empID'])){ 
    $employeeID = $_POST['empID']; 
} 
$Name=""; 
if (! empty($_POST['name'])){ 
    $Name = $_POST['name']; 
} 
$Address=""; 
if (! empty($_POST['address'])){ 
    $Address = $_POST['address']; 
} 
$Dateofbirth=""; 
if (! empty($_POST['DOB'])){ 
    $Dateofbirth = $_POST['DOB']; 
} 
$Salary=""; 
if (! empty($_POST['salary'])){ 
    $Salary = $_POST['salary']; 
} 
$timestamp=""; 
if (! empty($_POST['timeIn'])){ 
    $timestamp = $_POST['timeIn']; 
} 

$sql = "UPDATE Employee SET Name = $Name, Address = $Address, Dateofbirth = $Dateofbirth, Salary = $Salary, Timein = $timestamp where empID = $employeeID"; 

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

mysqli_close($conn); 
} 

?> 

<html> 
<body> 

<form action="" method="post"> 
<h2>Add Employee Form</h2> 
    Employee ID: <input type="text" name="empID" > 
    <br><br> 
    Name: <input type="text" name="name" > 
    <br><br> 

    Address: <input type="text" name="address" > 
    <br><br> 

    Date of Birth: <input type="text" name="DOB" > 
    <br><br> 

    Salary: <input type="text" name="salary" > 
    <br><br> 

    Time In: <input type="text" name="timeIn" > 
    <br><br> 

<input type="submit" name="update" value="UPDATE" /> 

</form> 
</body> 
</html>