2015-11-12 461 views
0

下面的代碼應該允許您查看朋友列表 並編輯或從同一頁面刪除。查看工作,但刪除和更新不。任何幫助,將不勝感激。 查看代碼:爲什麼mysqli查詢返回false?

$query ="SELECT * FROM tblFriends"; 
     $result = mysqli_query($conn,$query); 
     while($row =$result->fetch_assoc()){ 

     $fname=$row['fname']; 
     $lname=$row['lname']; 
     $address=$row['address']; 
     $desc=$row['description']; 
     $zip=$row['zip']; 
     $city=$row['city']; 
     $state=$row['state']; 
     $id =$row['key']; 

    echo 
    "<tr> 
    <td><input type='text' name='fname' value='$fname'/></td> 
    <td><input type='text' name='lname' value='$lname'/></td> 
    <td><input type='text' name='address' value='$address'/></td> 
    <td><input type='text' name='city' value='$city'/></td> 
    <td><input type='text' name='desc' value='$desc'/></td> 
    <td><input type='text' name='state' value='$state'/></td> 
    <td><input type='submit' name='Edit' value='Edit'/></td> 
    <td><input type='submit' name='Delete' value='Delete'/></td> 
    <input type='hidden' name='id' value='$id'/> 
    <input type='hidden' name='zip' value='$zip'/> 
    </tr>"; 
    } 

更新代碼:

 if(isset($_POST['Edit'])){ 
      $fname1=$_POST['fname']; 
      $lname1=$_POST['lname']; 
      $city1=$_POST['city']; 
      $state1=$_POST['state']; 
      $zip1=$_POST['zip']; 
      $desc1=$_POST['desc']; 
      $address1=$_POST['address']; 
      $id1=$_POST['id']; 
      $UpdateQuery ="UPDATE tblfriends SET fname='$fname1', lname='$lname1',city='$city1',address='$address1',zip='$zip1',state='$state1' WHERE id=$id1"; 
      echo $id1; 
      if(mysqli_query($conn,$UpdateQuery)){ 
       echo "Updated"; 
      }else{ 
       echo "Not Updated"; 
      } 

    } 

刪除代碼: 這mysqli_query和更新一個都返回假

if(isset($_POST['Delete'])){ 
      $id2 =$_POST['id']; 
      $deleteQuery= "DELETE FROM tblfriends WHERE id=$id2"; 
      if(mysqli_query($conn,$deleteQuery)){ 
       echo "Deleted"; 
      }else{ 
       echo "Not Deleted"; 
      } 
+0

你有錯誤報告打開?你應該經常檢查你的查詢是否有錯誤。 – Andrius

+0

@Andrius我不知道有錯誤報告,我怎麼知道它是否在打開? – DohnJoe

+0

'ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(E_ALL);'幫助。如果您有任何錯誤,請編輯您的問題。 – Andrius

回答

0

我看着你通過你quesry哈哈,點出一個奇怪的?

$id =$row['key']; 

"UPDATE tblfriends SET fname='$fname1', lname='$lname1',city='$city1',address='$address1',zip='$zip1',state='$state1' WHERE id=$id1 

"DELETE FROM tblfriends WHERE id=$id2"; 

只是爲了幫助你,你的id名是關鍵嗎?但你檢查ID是否匹配ID?不知道,但可能是你的問題

所以您的查詢更改爲此

$query ="SELECT * FROM tblFriends"; 
$result = mysqli_query($conn,$query); 
while($row = $result->fetch_assoc()){ 

    $fname=$row['fname']; 
    $lname=$row['lname']; 
    $address=$row['address']; 
    $desc=$row['description']; 
    $zip=$row['zip']; 
    $city=$row['city']; 
    $state=$row['state']; 
    $id =$row['key']; 

/** 
**Wrapped your input into a form which will post the request 
**/ 
    echo 
    " 
    <form action=\"#\" method=\"POST\"> 
    <tr> 
    <td><input type='text' name='fname' value='$fname'/></td> 
    <td><input type='text' name='lname' value='$lname'/></td> 
    <td><input type='text' name='address' value='$address'/></td> 
    <td><input type='text' name='city' value='$city'/></td> 
    <td><input type='text' name='desc' value='$desc'/></td> 
    <td><input type='text' name='state' value='$state'/></td> 
    <td><input type='submit' name='Edit' value='Edit'/></td> 
    <td><input type='submit' name='Delete' value='Delete'/></td> 
    <input type='hidden' name='id' value='$id'/> 
    <input type='hidden' name='zip' value='$zip'/> 
    </tr> 
    <br> 
    </form> 
    "; 
}; 

if(isset($_POST['Edit'])){ 
    $fname1= $_POST['fname']; 
    $lname1= $_POST['lname']; 
    $city1= $_POST['city']; 
    $state1= $_POST['state']; 
    $zip1= $_POST['zip']; 
    $desc1= $_POST['desc']; 
    $address1= $_POST['address']; 
    $id1= $_POST['id']; 



/** 
**Updated your query to match the key rather than ID. `key` 
**/ 

    $UpdateQuery ="UPDATE tblfriends SET fname='$fname1', lname='$lname1',city='$city1',address='$address1',zip='$zip1',state='$state1' WHERE `key` = '$id1' "; 
    echo $id1; 
    if(mysqli_query($conn,$UpdateQuery)){ 
     echo "Updated"; 
    }else{ 
     echo "Not Updated"; 
    }; 

}; 

if(isset($_POST['Delete'])){ 
    $id2 = $_POST['id']; 

/** 
**Updated your query to match the key rather than ID. `key` 
**/ 
    $deleteQuery= "DELETE FROM tblfriends WHERE `key` = '$id2' "; 
    if(mysqli_query($conn,$deleteQuery)){ 
     echo "Deleted"; 
    }else{ 
     echo "Not Deleted"; 
    } 
}; 
0

更改此查詢"DELETE FROM tblfriends WHERE id=$id2" with "DELETE FROM tblfriends WHERE id='$id2'"。通過在查詢編輯器中運行它來檢查查詢更好(如tab sql中的phpmyadmin)。 嘗試這樣的測試在你的代碼:

$deleteQuery= "DELETE FROM tblfriends WHERE id=$id2"; 
die($deleteQuery); 

然後複製在瀏覽器中顯示的結果,然後粘貼在phpMyAdmin。在那裏運行。希望它可以幫助你