-1

I am trying to delete an item from my database but it isnt working. I thought I had it working but it was deleting the first item in the database but not the item selected.

Here is what I have.

A link to delete.php then I have this for delete.php

<?php
ob_start();
include_once('../mysql_connect.php');

// contact to database


$host = "localhost";
$username   = "admin";
$password   = "password";
$database="database";
$tbl_name="new_equip";

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$database")or die("cannot select DB");

// get value of id that sent from address bar 
$id=$_GET['id'];

// Delete data in mysql from row that has this id 
$sql="DELETE FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);

// if successfully deleted
if($result){
echo "Deleted Successfully";
echo "<BR>";
echo "<a href='inventory.php'>Back to main page</a>";
}

else {
echo "ERROR";
}
?>

I know this is probably something simple and I have been searching and trying everything I can find, but I cannot seem to get it working. I believe the delete.php link needs to have the item number in it. Here is what the link is

"delete.php?id=<?php echo $eid; ?>"

I also have this on the top of the delete.php

<?php
$eid = (int) $_GET['id'];
if ($eid < 1)
?>
WebbieWorks
  • 163
  • 1
  • 17
  • 3
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – tyteen4a03 Jan 19 '15 at 17:06
  • have you checked if it is sending the right id for deletion? – Solano Jan 19 '15 at 17:07
  • You probably have no need for `ob_start();` here (lookup what functions do before just copy and pasting them), and you need to stop using `mysql_` because its deprecated (again, lookup what functions do in the docs before just copy and pasting them). – developerwjk Jan 19 '15 at 17:07
  • 1
    typo? echo $eid should be $id? – dan983 Jan 19 '15 at 17:08
  • id is an integer, so it should be without quotes in your Delete query. – Amit Verma Jan 19 '15 at 17:13
  • How hard is it to change everything from mysql to PDO or mysqli? I have seen that being mention quite a few places. – WebbieWorks Jan 19 '15 at 17:31
  • After doing some looking around on the site, mysqli would be the way to go seeing I dont need the 12 drivers that PDO uses. I will look into changing my entire site, bad thing is, it is a pretty big site so a lot of changes need to be made. But better to learn it now rather than keep going the old outdated ways. – WebbieWorks Jan 19 '15 at 17:37

1 Answers1

0
  1. Do not put the GET directly in your variable that might cause SQL Injections
  2. Do not use mysql... use mysqli instead!
  3. Format your code more

Than back to the main problem: Echo your $id to see if it is the correct one ;) I could not see some other problem at your code.

petritz
  • 182
  • 1
  • 9
  • I am looking to convert all the mysql to mysqli. I got a converter based off a post here but I cannot get it to see any files. I think it is because I am on a mac and it is showing how to do it based off windows. Here is the link I got the converter from. http://stackoverflow.com/questions/12020227/updating-from-mysql-to-mysqli – WebbieWorks Jan 19 '15 at 18:11
  • Please do not use converters for this little code. Here is a cool tutorial that covers basic things: http://codular.com/php-mysqli – petritz Jan 19 '15 at 18:13
  • It is just not for this little bit of code. I have an entire site that I need to convert. Theres about 10 pages that I need to redo everything and just now learning it, I think it will take me a long time to get it all changed over. – WebbieWorks Jan 19 '15 at 18:33