2016-04-24 48 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

相关问题