2013-11-26 41 views
-1

編輯一個mysql主題所以我創建這個CMS的教師信息作爲我的論文的一部分。當我需要創建/刪除主題時,我的代碼似乎工作正常。但是,當我嘗試更新它們時,它告訴我編輯過程失敗。不知道究竟在哪裏出錯。無法使用Php

以下是我的主題查詢。我有點新的PHP,所以如果我犯了一些明顯的錯誤,請耐心等待。

if (isset($_POST['submit'])) 
{ 
//form was submitted 
    $staff_id = mysql_real_escape_string((int) $_POST["staff_id"]); 
    $first_name = mysql_real_escape_string($_POST["first_name"]); 
    $last_name = mysql_real_escape_string($_POST["last_name"]); 
    $age = mysql_real_escape_string((int) $_POST["age"]); 
    $email = mysql_real_escape_string($_POST["email"]); 
    $phone_no = mysql_real_escape_string((int) $_POST["phone_no"]); 
    $department = mysql_real_escape_string($_POST["department"]); 
    $visible = mysql_real_escape_string((int) $_POST["visible"]); 
    $message = "Your details have been received"; 

    //Database Query 
    $query = "UPDATE subjects SET "; 
    $query .= "first_name = '{$first_name}', "; 
    $query .= "last_name = '{$last_name}', "; 
    $query .= "age = '{$age}', "; 
    $query .= "email = '{$email}', "; 
    $query .= "phone_no = '{$phone_no}', "; 
    $query .= "department = '{$department}', "; 
    $query .= "visible = '{$visible}', "; 
    $query .= "WHERE staff_id = '{$staff_id}', "; 
    $query .= "LIMIT 1"; 

    $result = mysqli_query($connect, $query); 
    if($result && mysqli_affected_rows($connect) == 1) 
     { 
      //Success 
      echo "Subject Edited. "; 
     } 
    else 
     { 
      //failure 
      echo "Subject Editing failed. "; 
     } 
} 

,這是這是我們用來發送數據進行編輯

<form action="editsub.php" method ="post"> 
      <p>Staff ID to edit: 
       <input type="number" name="staff_id" value ="" /> 
      </p> 
      <p>First Name: 
       <input type="text" name="first_name" value ="" />     
      </p> 
      <p>Last Name: 
       <input type="text" name="last_name" value ="" />      
      </p> 
      <p>Age: 
       <input type="number" name="age" value ="" />  
      </p> 
      <p>E-mail: 
       <input type="text" name="email" value ="" />  
      </p> 
      <p>Phone Number: 
       <input type="number" name="phone_no" value ="" /> 
      </p> 
      <p>Department: 
       <input type="text" name="department" value ="" /> 
      </p> 
      <p>Visible: 
       <input type="radio" name="visible" value ="0" /> No &nbsp; 
       <input type="radio" name="visible" value ="1" /> Yes 
      </p> 
      <input type="submit" name="submit" value="Edit Entry" /> 
      </form> 

因此,如果有人能指出其中的錯誤是我會非常感激的形式。感謝

+0

錯誤報告踢出了什麼? http://us1.php.net/manual/en/mysqli.error.php('echo mysqli_error($ connect);') – LuckySpoon

+1

你在where子句中有一個不必要的逗號。 –

+0

@AnonySterling你應該添加這個答案而不是評論。 (好眼睛:)) –

回答

0

就注意到了這一點 - 變化:

$query .= "visible = '{$visible}', "; 
$query .= "WHERE staff_id = '{$staff_id}', "; 

到:

$query .= "visible = '{$visible}'"; 
$query .= "WHERE staff_id = '{$staff_id}'"; 

即:不正確逗號

+1

你是一個救星。我不敢相信這很簡單。 #facepalm感謝luckypoon和安東尼英鎊 –

+0

Upvotes和接受我的代表收集讚賞:)很高興幫助! – LuckySpoon

0

這裏這段代碼

//Database Query 
$query = "UPDATE subjects SET "; 
$query .= "first_name = '{$first_name}', "; 
$query .= "last_name = '{$last_name}', "; 
$query .= "age = '{$age}', "; 
$query .= "email = '{$email}', "; 
$query .= "phone_no = '{$phone_no}', "; 
$query .= "department = '{$department}', "; 
$query .= "visible = '{$visible}', "; 
$query .= "WHERE staff_id = '{$staff_id}', "; 
$query .= "LIMIT 1"; 

應該是這樣

//Database Query 
$query = "UPDATE subjects SET "; 
$query .= "first_name = '$first_name', "; 
$query .= "last_name = '$last_name', "; 
$query .= "age = '$age', "; 
$query .= "email = '$email', "; 
$query .= "phone_no = '$phone_no', "; 
$query .= "department = '$department', "; 
$query .= "visible = '$visible' "; 
$query .= "WHERE staff_id = '$staff_id' "; 
$query .= "LIMIT 1"; 
0

我認爲你的查詢是錯誤的。在「WHERE」和「LIMIT」之前有一個「,」。同時刪除「,」,然後重試。