2016-01-04 116 views
-2

我正在處理一個項目,我想用$_GET方法更新另一個用戶的詳細信息。我的問題是,當用戶點擊ID,它確實去編輯頁面,但是當我改變某些東西並按下更新按鈕時,它不會更新。我不知道我在這裏做錯了什麼。如果有人能幫助我,我會非常感激。更新查詢不起作用php

//編輯

我的代碼工作,現在,大家好,我只是改變了$ _ POST到$ _REQUEST現在我的狀態更新。謝謝所有幫助我..謝謝你..這是我的編輯的代碼..我已經取出了密碼字段,但我有一個疑問..使用請求安全嗎?

<?php 
include '../../connection.php'; 

$sid = $_REQUEST['sid']; 
$query = "SELECT * FROM STUDENT WHERE STU_ID='$sid'"; 
$result = mysqli_query($connection, $query); 

if(mysqli_num_rows($result)>0){ 
    while($row = mysqli_fetch_assoc($result)){ 
    $unm = $row["STU_UNAME"]; 
    $fnm = $row["STU_FNAME"]; 
    $lnm = $row["STU_LNAME"]; 
    $dob = $row["STU_DOB"]; 
    $add = $row["STU_ADD"]; 
    $tlp = $row["STU_PHONE"]; 
     $sem = $row["STU_SEM"]; 
     $img = $row["STU_IMG"]; 
     $sts = $row["STU_STATUS"]; 
     $cid = $row["CRS_ID"]; 
    } 
} 
else{ 
    $no = "0 result!"; 
} 

if($_SERVER["REQUEST_METHOD"] == "POST"){ 
//insert details in data 

$sid = $_POST["sid"]; $snm = $_POST["snm"]; $fst = $_POST["fnm"]; $lst = $_POST["lnm"]; $sdb = $_POST["dob"]; 
$sad = $_POST["add"]; $shp = $_POST["tlp"]; $stt = $_POST["sts"]; $sem = $_POST["sem"]; $cid = $_POST["cid"]; 

$sql = "UPDATE STUDENT SET 
     STU_ID='$sid', STU_UNAME='$snm', STU_FNAME= '$fst', STU_LNAME='$lst', STU_DOB='$sdb', STU_ADD='$sad', STU_PHONE='$shp', 
     STU_STATUS='$stt', STU_SEM='$sem', CRS_ID = '$cid' WHERE STU_ID='$sid'"; 

//check if data is updated 
if (mysqli_query($connection, $sql)) { 
    header("Location: searchStudent.php"); 
} 
else { 
    echo "Error: " . $sql . "<br>" . mysqli_error($connection); 
    } 
} 
?> 

這裏是我的表單代碼:

<form class="contact_form" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
<ul> 
    <li> 
     <h2>Edit Students Details</h2> 
     <span class="required_notification">* Denotes Required Field</span> 
    </li> 
    <li> 
     <label for="id">Student ID: </label> 
     <input type="text" name="sid" value="<?php echo $sid;?>"/> 
</li> 
<li> 
    <label for="name">Username: </label> 
    <input type="text" name="snm" value="<?php echo $unm;?>"/> 
</li> 
<li> 
    <label for="name">First Name: </label> 
    <input type="text" name="fnm" value="<?php echo $fnm;?>"/> 
</li> 
<li> 
    <label for="name">Last Name: </label> 
    <input type="text" name="lnm" value="<?php echo $lnm;?>"/> 
</li> 
<li> 
    <label for="dob">Date of Birth: </label> 
    <input type="date" name="dob" value="<?php echo $dob;?>"/> 
</li> 
<li> 
    <label for="add">Address: </label> 
    <textarea name="add" rows="4" cols="50"><?php echo $add;?></textarea> 
</li> 
<li> 
    <label for="tlp">Phone: </label> 
    <input type="text" name="tlp" value="<?php echo $tlp;?>"/> 
</li> 
<li> 
    <label for="sts">Status: </label> 
    <select name="sts"> 
     <option selected><?php echo $sts;?></option> 
     <option value="FULLTIME">FULL TIME</option> 
     <option value="PARTTIME">PART TIME</option> 
    </select> 
</li> 
<li> 
    <label for="sem">Semester: </label> 
    <select name="sem"> 
     <option selected><?php echo $sem;?></option> 
     <option value="1">1</option> 
     <option value="2">2</option> 
     <option value="3">3</option> 
     <option value="4">4</option> 
     <option value="5">5</option> 
     <option value="6">6</option> 
     <option value="7">7</option> 
    </select> 
</li> 
<li> 
    <label for="crs">Course: </label> 
    <select name="cid"> 
     <option selected><?php echo $cid;?></option> 
     <option value="AL">AL</option> 
     <option value="DBM">DBM</option> 
     <option value="DIT">DIT</option> 
     <option value="DTM">DTM</option> 
     <option value="FIS">FIS</option> 
    </select> 
</li> 
<li> 
    <button class="submit" type="submit" name="update">Update</button> 
</li> 

+0

任何錯誤消息? –

+0

也張貼您的錯誤訊息 – Butterfly

+1

回聲「錯誤:」。 $ sql。 「
」。 mysqli_error($連接); –

回答

0

確保您的形式方法是POST

試試這個代碼:

<?php 
include '../../connection.php'; 
// 
$id = $_POST['id']; 
$query = "SELECT * FROM STUDENT WHERE STU_ID='$id'"; 
$result = mysqli_query($connection, $query); 

if(mysqli_num_rows($result)>0){ 
    while($row = mysqli_fetch_assoc($result)){ 
    $unm = $row["STU_UNAME"]; 
    $fnm = $row["STU_FNAME"]; 
    $lnm = $row["STU_LNAME"]; 
    $pwd = $row["STU_PWD"]; 
    $dob = $row["STU_DOB"]; 
    $add = $row["STU_ADD"]; 
    $tlp = $row["STU_PHONE"]; 
    $sem = $row["STU_SEM"]; 
    $img = $row["STU_IMG"]; 
    $sts = $row["STU_STATUS"]; 
    $cid = $row["CRS_ID"]; 
    } 
} 
else{ 
    $no = "0 result!"; 
} 


$pwdErr = $cpwdErr= ""; 
if($_SERVER["REQUEST_METHOD"] == "POST"){ 
    if($_POST["pwd"] == $_POST["cpwd"]){ 
    if(strlen($_POST["pwd"])>8){ 
    //insert details in data 

    $sid = $_POST["sid"]; $pwd = $_POST["pwd"]; $snm = $_POST["snm"]; $fst = $_POST["fnm"]; $lst = $_POST["lnm"]; 
    $sdb = $_POST["dob"]; $sad = $_POST["add"]; $shp = $_POST["tlp"]; $stt = $_POST["sts"]; $sem = $_POST["sem"]; 
    $cid = $_POST["cid"]; 

    $sql = "UPDATE STUDENT SET 
      STU_ID='$sid', STU_PWD='$pwd', STU_UNAME='$snm', STU_FNAME= '$fst', STU_LNAME='$lst', STU_DOB='$sdb', STU_ADD='$sad', STU_PHONE='$shp', 
      STU_STATUS='$stt', STU_SEM='$sem', CRS_ID = '$cid' WHERE STU_ID='$id'"; 

     //check if data is updated 
     if (mysqli_query($connection, $sql)) { 
      header("Location: searchStudent.php"); 
     } 
     else { 
      echo "Error: " . $sql . "<br>" . mysqli_error($connection); 
     } 
    } 
    else{ 
     $pwdErr = "Invalid/Password must be more than 8 characters!"; 
    } 
} 
else{ 
    $cpwdErr = "Password not same!"; 
    } 
} 
?> 

如何使用示例here的準備說明獲得搭乘。

希望這對你有幫助!

+0

我的確喜歡你說的並且改變了它,但是現在的問題是,當用戶點擊id時,它會轉到編輯頁面,但不會顯示用戶的詳細信息。這是我用於單擊編輯鏈接時的代碼Edit然後進入更新頁面 –

+0

'print_r($ _ POST)的輸出是什麼;退出;'。只需檢查並將其與您的更新查詢值進行比較。 –

+0

什麼都沒有,他們只顯示Array() –

0

你的錯誤是在你的表單使用POST,但得到它與$ _ POST

$id = $_POST['id']; 
+0

那是以前,現在我已經改變了我的代碼以$ _ POST我的代碼[「身份證」] –

+0

那它應該是 '$ ID = $ _ POST [「SID」];' –

+0

因爲輸入學生姓名是'sid' –

0

拿到找零$ _GET價值的很好的問題是你張貼的ID是「SID」,但您只使用「id」像$_POST['id']而不是$_POST['sid']。所以用這個 - 而不是

$id = $_POST['sid']; 

-

$id = $_POST['id']