2016-04-24 69 views
0

我試圖做多在PHP中刪除,但要得到一個錯誤:多刪除錯誤:「試圖獲得非對象的屬性在」

Trying to get property of non-object in...

我不知道爲什麼這是發生 - 我做錯了什麼?

<?php 
    error_reporting(E_ALL); 

ini_set('display_errors', 1); 

//--------------------------------------DELETE RECORD - DATABASE----------------------------------------------- 
//Obtain login credentials 
require_once 'login.php'; 

//Create connection to database 
$conn = new mysqli($hn, $un, $pw, $db); 

//Check if connection succeeded 
if ($conn->connect_error) die($conn->connect_error); 

//Verify if user already chose record to be deleted from the database table 
if (isset($_GET['id']) && is_numeric($_GET['id'])) { 

    $id = $_GET['id']; 

    //Delete record from table 
    $query = "DELETE FROM jazz WHERE id=$id"; 

    $result = $conn->query($query); 

    //If query fails, display error message 
    if (!$result) { 
      echo "DELETE failed: $query <br />" . $conn->error . "<br /><br />"; 
    } else { 
      //If query succeeded, display success message 
      echo "Record deleted successfully!"; 
    } 
} 

//--------------------------------------RETRIEVE RECORDS - DATABASE----------------------------------------------- 

//Retrieve records 
$query = "SELECT id, year, artist, album, Publisher, subgenre, rating FROM jazz"; 
$result = $conn->query($query); 

//Check if query succeeded 
if (!$result) die ("Database access failed: " . $conn->error); 

//Sets up Multi Delete Ability 

**if(isset($_POST['delete'])) 
{ 
$cnt=array(); 
$cnt=count($_POST['checkbox']); 
for($i=0;$i<$cnt;$i++) 
    { 
    $del_id=$_POST['checkbox'][$i]; 
    $query = "DELETE FROM jazz WHERE id='$.id'"; 
    $result = $conn->query($query); 
    if (!$result) echo "DELETE failed: $query<br>" . 
     $conn->error . "<br><br>"; 
    # mysqli_query($conn , $result); 
    } 
}** 


echo "<!DOCTYPE html>"; 
echo "<html>"; 
echo "<head>"; 
echo "<title>Delete Record</title>"; 
echo "<link rel='stylesheet' href='view.css'>"; 
echo "</head>"; 
echo "<body>"; 
echo "<header><h1>Delete Record</h1></header>"; 
echo "<form action = 'delete_test.php' method = 'post'>"; 
echo "<table>"; 
echo "<tr class='row'>"; 
echo "<th class='col-1'>ID</th>"; 
echo "<th class='col-1'>Year</th>"; 
echo "<th class='col-2'>Artist</th>"; 
echo "<th class='col-3'>Album</th>"; 
echo "<th class='col-4'>Rating</th>"; 
echo "<th class='col-5'>Subgenre</th>"; 
echo "<th class='col-6'>Publisher</th>"; 
echo "<th class='col-7'>Delete</th>"; 
echo "</tr>"; 

**//Display retrieved records  ----------------START 
if (is_object($result)){ 
$rows = $result->num_rows; 
} 
$rows = $result->num_rows;**   ----------------END 

for ($j = 0 ; $j < $rows ; ++$j) { 
    $result->data_seek($j); 
    $row = $result->fetch_array(MYSQLI_NUM); 

    echo "<tr class='row'>"; 
    echo "<td class='col-1'>" . $row[0] . "</td>"; 
    echo "<td class='col-2'>" . $row[1] . "</td>"; 
    echo "<td class='col-3'>" . $row[2] . "</td>"; 
    echo "<td class='col-4'>" . $row[3] . "</td>"; 
    echo "<td class='col-5'>" . $row[4] . "</td>"; 
    echo "<td class='col-6'>" . $row[5] . "</td>"; 
    echo "<td class='col-7'>" . $row[6] . "</td>"; 
    echo "<td class='col-8'><input type = 'checkbox' input name =    'checkbox[]' value='.$row[0].'</td>"; 
    echo "</tr>"; 

} 
echo "<input type = 'submit' value = 'delete' name = 'delete' class = 'button'>"; 



echo "</table></form></body></html>"; 
echo "<a href='Project - Homepage.html'>Home</a>"; 

//Close connection 
$conn->close(); 

?> 
+0

在哪一行? –

+0

它在行85 – GOAT

+0

什麼是85行?我們不能計數到85示出了它在問題 –

回答

1

我認爲你必須糾正

$query = "DELETE FROM jazz WHERE id='$.id'"; 

行與

$query = "DELETE FROM jazz WHERE id='$id'"; 
1

不應該查詢是

$query = "DELETE FROM jazz WHERE id='$id'"; 

,而不是

$query = "DELETE FROM jazz WHERE id='$.id'"; 

,什麼是這個$.id ????

+0

所以 - 作出修正,它似乎已經變得好一點。 不清楚我的第二個參數是在這種情況下。 如果(isset($ _ POST [ '刪除'])){ $ CNT =陣列(); $ cnt = count($ _ POST ['checkbox']); 爲($ I = 0; $ I <$ CNT; $ I ++){ $ = del_id $ _ POST [ '複選框'] [$ i]於; $ query =「DELETE from jazz where id =」。$ del_id; mysqli_query($ query); } } – GOAT

+0

仍然會出錯? – rahul

+0

現在扔​​一個不同的......謝謝你的幫助! – GOAT

相關問題