douduandiao1368 2016-04-24 18:13
浏览 44

多删除错误:“试图获取非对象的属性”

I'm trying to do a multi delete in PHP, but keep getting an error:

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

I'm not sure why that's happening - what am I doing wrong?

    <?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();

?>
  • 写回答

2条回答 默认 最新

  • dongzhou8764 2016-04-24 18:21
    关注

    I think you must correct the line with

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

    with

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

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号