简体   繁体   中英

Delete data from mysql database php

Firstly i'm not a developer so bear with me :)

I'm trying to delete a row from a small inventory system we are setting up but its still not allowing me to do anything, this is my current code:

Assigned page:

<?php
$password = 'notTheRealPassword';
$conn = mysql_connect('localhost', 'root', $password);
        if(!$conn)
        {
                die('Error connecting database');
        }
        mysql_select_db('inventory_system', $conn);


$query= "SELECT * from assigned";

$result=mysql_query($query);



 echo "<table border='0' cellpadding='100' class='table-striped'>";
         echo "<th>Asset ID</th><th>Asset</th><th>Type</th><th>Manufacturer</th><th>Model</th><th>PC
 Name</th><th>Serial Number</th><th>Purchased</th><th>Warranty
 End</th><th>OS</th><th>OS
 Bit</th><th>CPU</th><th>Memory</th><th>HDD</th><$

    while($row = mysql_fetch_array($result))
     {
      echo '<td>' . $row['asset_id'] . '</td>';
      echo '<td>' . $row['asset'] . '</td>';
      echo '<td>' . $row['type'] . '</td>';
      echo '<td>' . $row['manufacturer'] . '</td>';
      echo '<td>' . $row['model'] . '</td>';
      echo '<td>' . $row['pc_name'] . '</td>';
      echo '<td>' . $row['serial_no'] . '</td>';
      echo '<td>' . $row['purchased'] . '</td>';
      echo '<td>' . $row['warranty_end'] . '</td>';
      echo '<td>' . $row['os'] . '</td>';
      echo '<td>' . $row['os_bit'] . '</td>';
      echo '<td>' . $row['cpu'] . '</td>';
      echo '<td>' . $row['memory'] . '</td>';
      echo '<td>' . $row['hdd'] . '</td>';
      echo '<td>' . $row['plant'] . '</td>';
      echo '<td>' . $row['location'] . '</td>';
      echo '<td>' . $row['username'] . '</td>';
      echo "<td><a href=\"delete.php?id=".$row['asset_id']."\">Delete</a></td>";
          echo "</tr>";
          }
         echo "</table>";

mysql_close();

?>

Delete page:

<?php

$password = 'notTheRealPassword';    
$conn = mysql_connect('localhost', 'root', $password);
        if(!$conn)
        {
                die('Error connecting database');
       }
       mysql_select_db('inventory_system', $conn);


 $id = (int)$_GET['asset_id'];

mysqli_query($conn,"DELETE FROM assigned WHERE asset_id='".$id."'");
mysqli_close($conn);
header("Location: assigned1.php");


?>

I'm guessing it will be something simple.

Cheers,

There are a few things wrong here:

  • Your GET variable, the one you put in the link, is id , not asset_id
  • You should not use GET to modify things in your database; if your browser or a plugin decides to pre-fetch links, your database will be wiped out.
  • You can't mix mysql apis, use either mysqli or mysql (although not the latter...).
  • The mysql_* functions are deprecated, you should use PDO or mysqli in combination with prepared statements.
  • When you echo out lots of html, it is easier to just close the php section and then open it again when you need it; now you have php tags inside the echoed texts. The php / strings / html are not separated correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM