php
  • mysql
  • mysqli
  • 2014-10-29 91 views 0 likes 
    0

    我有一個表格顯示在用戶屏幕上。當用戶點擊編輯時,我希望他能夠點擊該特定行的參數並更新單個參數(例如,名稱)。那麼其他參數(例如,電子郵件,密碼和地址)應該保持不變。更新表中的特定行

    name email   password address  action 
    user [email protected] user  u.address edit 
    

    用於編輯鏈接的代碼是:

    echo"<td class='center'><a class='btn btn-info' href=\"admin_edit_user.php?id=".$row['id']."\"><i class='glyphicon glyphicon-edit icon-white'></i>Edit</a></td>"; 
    

    守則admin_edit_user.php頁:

    <form class="form-horizontal" role="form" action="admin_update_user.php" enctype="multipart/form-data" method="post"> 
        <div class="form-group"> 
         <label class="col-lg-3 control-label">Name</label> 
          <div class="col-lg-8"> 
           <input class="form-control" name="name" value="" type="text"> 
          </div> 
        </div> 
    
        <div class="form-group"> 
         <label class="col-lg-3 control-label">Email</label> 
          <div class="col-lg-8"> 
           <input class="form-control" name="email" value="" type="text"> 
          </div> 
        </div> 
    
        <div class="form-group"> 
         <label class="col-lg-3 control-label">Password</label> 
          <div class="col-lg-8"> 
           <input class="form-control" name="password" value="" type="text"> 
          </div> 
        </div> 
    
        <div class="form-group"> 
         <label class="col-lg-3 control-label">Address</label> 
          <div class="col-lg-8"> 
           <input class="form-control" name="address" value="" type="text"> 
          </div> 
        </div> 
    
        <div class="form-group"> 
         <label class="col-md-3 control-label"></label> 
          <div class="submit"> 
           <input class="btn btn-primary" value="Save Changes" type="submit" name="submit"> 
          </div> 
        </div>       
    </form> 
    

    這種形式的用戶被重定向到admin_update_user.php

    <?php 
        $con=mysqli_connect("localhost","root","","db"); 
        if (mysqli_connect_errno()) 
         { 
          echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
         } 
        $id = $_GET['id']; 
    
    
        $query="SELECT * FROM user"; 
        $result= mysqli_query($con, $query) or die(mysqli_error()); 
        while ($row= mysqli_fetch_array($result)) 
         { 
          $name_data=$row['name']; 
          $name_email=$row['email']; 
          $name_password=$row['password']; 
          $name_address=$row['address']; 
         } 
    
        if(isset($_POST['submit'])) 
         { 
          $name=$_POST['name']; 
          $email=$_POST['email']; 
          $password=$_POST['password']; 
          $address=$_POST['address']; 
    
          if(empty($name)) 
           { 
            //if the value is empty its going to set it equal to the database value 
            $name=$name_data; 
           } 
          else 
           $name=$name; 
    
          if(empty($email)) 
           { 
            $email=$name_email; 
           } 
          else 
           $email=$email; 
    
          if(empty($password)) 
           { 
    
            $password=$name_password; 
           } 
          else 
           $password=$password; 
    
          if(empty($address)) 
           { 
            $address=$name_address; 
           } 
          else 
           $address=$address; 
    
         } 
    //0: demo.name (value given for name) 
    //[email protected] (value given for email) 
    //demo (value given forpassword) 
    //demo 
    //demo.address (value given for address) 
    
         $sql = "UPDATE user SET name='".$name."',email='".$email."',password='".$password."',address='".$address."' WHERE id ='".$id."'"; 
    
         echo mysqli_errno($con) . ": " . mysqli_error($con) . "\n"; 
    
    
         if (!mysqli_query($con,$sql)) 
          { 
           die('Error: ' . mysqli_error($con)); 
          } 
         header("Location: admin_user_list.php"); 
         exit; 
         mysqli_close($con); 
        ?> 
    

    我在更新查詢中收到錯誤。如果有人能幫忙,我會非常感激。

    P.S @ FortMauris這裏是你希望看到的編輯部分。

    $id = $_GET['id']; 
        $name = mysqli_real_escape_string($con, $_POST['name']); 
        $email = mysqli_real_escape_string($con, $_POST['email']); 
        $password = mysqli_real_escape_string($con, $_POST['password']); 
        $address = mysqli_real_escape_string($con, $_POST['address']); 
    
        $query="SELECT * FROM user"; 
        $result= mysqli_query($con, $query) or die(mysqli_error()); 
        //get the value from database 
        while ($row= mysqli_fetch_array($result)) 
        { 
         $name_data=$row['name']; 
         $name_email=$row['email']; 
         $name_password=$row['password']; 
         $name_address=$row['address']; 
    
        } 
    
    if(isset($_POST['submit'])) 
    { 
        $name=$_POST['name']; 
        $email=$_POST['email']; 
        $password=$_POST['password']; 
        $address=$_POST['address']; 
    
        if(empty($name)) 
        { 
         //if the value is empty its going to set it equal to the database value 
         $name=$name_data; 
        } 
    
    
        if(empty($email)) 
        { 
         $email=$name_email; 
        } 
    
         if ($password == '') 
        { 
    
         $password=$name_password; 
        } 
    
        if(empty($address)) 
        { 
         $address=$name_address; 
        } 
    
    } 
    
    echo mysqli_errno($con) . ": " . mysqli_error($con) . "\n"; 
    
    $sql = "UPDATE user SET name='".$name."',email='".$email."',password='".$password."',address='".$address."' WHERE id ='".$id."'"; 
    
    
        if (!mysqli_query($con,$sql)) 
        { 
         die('Error: ' . mysqli_error($con)); 
        } 
    mysqli_close($con); 
    //header("Location: admin_user_list.php"); 
    exit; 
        mysqli_close($con); 
    
    +0

    你會得到什麼錯誤? – 2014-10-29 07:10:12

    +0

    你得到什麼錯誤 – Naincy 2014-10-29 07:10:47

    +0

    @sgt mysql_errno()期望參數1是資源 – sam 2014-10-29 07:15:11

    回答

    1
    • 在兩個頁面頂部session_start();
    • 不是添加在admin_edit_user此行添加此行$_SESSION['id']=$_GET['id'];
    • 比對admin_update_user.php加入這行$id=$_SESSION['id'];
    • 比在同一個文件這個文件admin_update_user.php你有這條線$id=$_GET['id'];去掉它。

      $sql = "UPDATE user SET name='".$name."',email='".mysqli_real_escape_string($con,$email)."',password='".$password."',address='".mysqli_real_escape_string($con,$address)."' WHERE id ='".$id."'";

    0

    我覺得你已經在查詢中保留關鍵字嘗試改變你的查詢,你是混合mysql和mysqli。

    刪除此行echo mysql_errno($con) . ": " . mysql_error($con) . "\n";

    $sql = "UPDATE `user` SET `name`='".$name."',`email`='".$email."',`password`='".$password."',`address`='".$address."' WHERE `id` =".$id; 
    
    if (!mysqli_query($con,$sql)) 
        { 
         die('Error: ' . mysqli_error($con)); 
        } 
    mysqli_close($con); 
    header("Location: admin_user_list.php"); 
    exit; 
    

    如果不到風度工作,然後打印您的查詢,並嘗試在phpMyAdmin或MySQL手動運行;

    +0

    我得到一個語法錯誤,但我不能明白出了什麼問題 – sam 2014-10-29 07:18:50

    +0

    我剛纔更新我的答案,你可以嘗試更新的代碼? – 2014-10-29 07:22:32

    +0

    我仍然收到相同的錯誤 – sam 2014-10-29 07:26:05

    0
    $sql = "UPDATE user SET name='".$name."',email='".$email."',password='".$password."',address='".$address."' WHERE id ='".$id."'"; 
    

    取而代之的是,請嘗試使用此:

    $sql = "UPDATE user SET name = '$name', email = '$email', password = '$password', address = '$address' WHERE id = $id"; 
    

    它是更清潔,會解決很多問題的查詢。

    編輯:

    的問題可能具有'"您的變量1。

    當你把它,它便會成爲像這樣:

    $sam = "Sam'"; 
    $sql = " UPDATE user SET name = 'sam'' "; 
    

    它檢測額外的支持引號,因此返回一個錯誤。

    +0

    它顯示相同的語法錯誤 – sam 2014-10-29 08:35:03

    +0

    好吧山姆,嘗試回顯所有變量($名稱,$電子郵件等),看看是否有問題的任何領域。我認爲問題在於Address,很可能在這些變量中有一個引號。嘗試逃避它或完全刪除它。 – FortMauris 2014-10-29 08:39:57

    +0

    我試過你的解決方案,但它仍然沒有給出預期的結果 – sam 2014-10-29 09:12:08

    相關問題