Jump to content

[SOLVED] Deleting row of table based on auto-increment number


Recommended Posts

i am really new to php, this is actually my first project.

 

So i am creating a inventory database, and i need to delete rows based on the auto-increment number on a table

 

here is my code

 

$id = $_POST['id'];

mysql_query("DELETE FROM `laptop` WHERE `laptop`.`id` = 1 LIMIT 1");

echo "Success! Your item has been added to the inventory!";

 

now if i execute this it deletes the row with the id of 1.  what i want to do is delete the row based on the record they delete so the number would be a variable based on what row they click the delete button on. 

 

any help would be sooo appreciative

 

if my description is confusing please tell me where to elaborate

 

 

$id = $_POST['id'];
mysql_query("DELETE FROM `laptop` WHERE `laptop`.`id` = '$id' LIMIT 1");
echo "Success! Your item has been added to the inventory!";

change the 1 to '$id' so the query will delete the id that is posted to the $id variable

Yes and make sure you check that $_POST['id'] is_numeric!

 

Personally I use a regular expression like

$id = preg_replace ("/[^0-9]/", "", $id);
if ($id) {
# do sql query
}

Always check your post vars (and any user input) for the right data or people WILL do nasty things to your database!

i tried that and still nothing, so i will post everything i can

 

 

here is my database viewing page that has a delete link that will run the query on the page laptop_remove.php

index.html

$data = mysql_query("SELECT * FROM laptop")

or die(mysql_error());

$current_row = 0;

Print "<center>Laptops</center>";

Print "<table border cellpadding=2 width=700px align=center>";

 

Print "<th width=20%>Model:</th> <th width=20%>Serial:</th> <th width=20%>Service Request:</th> <th width=20%>Pic:</th><th width=20%><a href=\"laptopadd.html\">ADD</a></th></td> ";

while($info = mysql_fetch_array( $data ))

{

 

Print "<tr>";

 

Print "<tr><td>".$info['model'] . " </td>";

Print "<td>".$info['serial'] . " </td>";

Print "<td>".$info['svreq'] . "</td> ";

Print "<td>".$info['pic'] . " </td>";

Print "<td><a href=laptop_remove.php>".delete." </a></td>";

Print "</tr>";

}

Print "</table>";

 

 

here is my laptop_remove.php page

 

 

$id = $_POST['id'];

mysql_query("DELETE FROM `laptop` WHERE `laptop`.`id` = '$id' LIMIT 1");

echo "Success! Your item has been deleted in the inventory!";

 

 

 

here is my database from phpMyAdmin

 

id model       serial     svreq       pic

6 dell gs eerr     sr44221

9 IBM laptop  23T5cs sr66321

10    HP Laptop    jj44121 sr44211

 

i just need to know what my issue is, i believe it is not picking up the id

i am totally new at this so the any help would be appreciated

Yes and make sure you check that $_POST['id'] is_numeric!

 

Personally I use a regular expression like

$id = preg_replace ("/[^0-9]/", "", $id);
if ($id) {
# do sql query
}

Always check your post vars (and any user input) for the right data or people WILL do nasty things to your database!

 

Or you can just do if(is_numeric($id) { blabla }

i tried that and still nothing, so i will post everything i can

 

 

here is my database viewing page that has a delete link that will run the query on the page laptop_remove.php

index.html

$data = mysql_query("SELECT * FROM laptop")

or die(mysql_error());

$current_row = 0;

Print "<center>Laptops</center>";

Print "<table border cellpadding=2 width=700px align=center>";

 

Print "<th width=20%>Model:</th> <th width=20%>Serial:</th> <th width=20%>Service Request:</th> <th width=20%>Pic:</th><th width=20%><a href=\"laptopadd.html\">ADD</a></th></td> ";

while($info = mysql_fetch_array( $data ))

{

 

Print "<tr>";

 

Print "<tr><td>".$info['model'] . " </td>";

Print "<td>".$info['serial'] . " </td>";

Print "<td>".$info['svreq'] . "</td> ";

Print "<td>".$info['pic'] . " </td>";

Print "<td><a href=laptop_remove.php>".delete." </a></td>";

Print "</tr>";

}

Print "</table>";

 

 

here is my laptop_remove.php page

 

 

$id = $_POST['id'];

mysql_query("DELETE FROM `laptop` WHERE `laptop`.`id` = '$id' LIMIT 1");

echo "Success! Your item has been deleted in the inventory!";

 

 

 

here is my database from phpMyAdmin

 

id model        serial      svreq       pic

6 dell gs eerr      sr44221

9 IBM laptop   23T5cs sr66321

10     HP Laptop    jj44121 sr44211

 

i just need to know what my issue is, i believe it is not picking up the id

i am totally new at this so the any help would be appreciated

 

As for this ...MAKE SURE(not yelling at you) your index.html is index.php because the .html wont read php syntax.

 

As for this ...MAKE SURE(not yelling at you) your index.html is index.php because the .html wont read php syntax.

 

In most cases this is true, but I could easily use a .htaccess file to setup php as the handler for .html files.

hey im pretty new to this myself..had a similar problem few days ago!

the guys came up with this!

<?php
$texto = $_POST['texto'];
$db = mysql_connect("localhost", "username", "password"); 
mysql_select_db("phone",$db); 
$query= "DELETE FROM phone_details WHERE phone_id='$texto'";
$result = mysql_query( $query );
?> 

works fine for me..dunno if it will be of any use!

i tried that and still nothing, so i will post everything i can

 

 

here is my database viewing page that has a delete link that will run the query on the page laptop_remove.php

index.html

$data = mysql_query("SELECT * FROM laptop")

or die(mysql_error());

$current_row = 0;

Print "<center>Laptops</center>";

Print "<table border cellpadding=2 width=700px align=center>";

 

Print "<th width=20%>Model:</th> <th width=20%>Serial:</th> <th width=20%>Service Request:</th> <th width=20%>Pic:</th><th width=20%><a href=\"laptopadd.html\">ADD</a></th></td> ";

while($info = mysql_fetch_array( $data ))

{

 

Print "<tr>";

 

Print "<tr><td>".$info['model'] . " </td>";

Print "<td>".$info['serial'] . " </td>";

Print "<td>".$info['svreq'] . "</td> ";

Print "<td>".$info['pic'] . " </td>";

Print "<td><a href=laptop_remove.php>".delete." </a></td>";

Print "</tr>";

}

Print "</table>";

 

 

here is my laptop_remove.php page

 

 

$id = $_POST['id'];

mysql_query("DELETE FROM `laptop` WHERE `laptop`.`id` = '$id' LIMIT 1");

echo "Success! Your item has been deleted in the inventory!";

 

 

 

here is my database from phpMyAdmin

 

id model        serial      svreq       pic

6 dell gs eerr      sr44221

9 IBM laptop   23T5cs sr66321

10     HP Laptop    jj44121 sr44211

 

i just need to know what my issue is, i believe it is not picking up the id

i am totally new at this so the any help would be appreciated

 

 

  Re: Deleting row of table based on auto-increment number

« Reply #5 on: Today at 02:21:43 PM » Quote Modify 

 

--------------------------------------------------------------------------------

Quote from: draftermath on Today at 02:13:48 PM

i tried that and still nothing, so i will post everything i can

 

 

here is my database viewing page that has a delete link that will run the query on the page laptop_remove.php

 

index.html

$data = mysql_query("SELECT * FROM laptop")

or die(mysql_error());

$current_row = 0;

Print "<center>Laptops</center>";

Print "<table border cellpadding=2 width=700px align=center>";

 

Print "<th width=20%>Model:</th> <th width=20%>Serial:</th> <th width=20%>Service Request:</th> <th width=20%>Pic:</th><th width=20%><a href=\"laptopadd.html\">ADD[/url]</th></td> ";

while($info = mysql_fetch_array( $data ))

{

 

Print "<tr>";

 

Print "<tr><td>".$info['model'] . " </td>";

Print "<td>".$info['serial'] . " </td>";

Print "<td>".$info['svreq'] . "</td> ";

Print "<td>".$info['pic'] . " </td>";

Print "<td><a href=laptop_remove.php>".delete." [/url]</td>";

Print "</tr>";

}

Print "</table>";

 

 

here is my laptop_remove.php page

 

 

$id = $_POST['id'];

mysql_query("DELETE FROM `laptop` WHERE `laptop`.`id` = '$id' LIMIT 1");

echo "Success! Your item has been deleted in the inventory!";

 

 

 

here is my database from phpMyAdmin

 

id    model           serial         svreq          pic

6    dell gs    eerr         sr44221     

9    IBM laptop   23T5cs    sr66321     

10     HP Laptop    jj44121    sr44211     

 

i just need to know what my issue is, i believe it is not picking up the id

i am totally new at this so the any help would be appreciated

 

 

As for this ...MAKE SURE(not yelling at you) your index.html is index.php because the .html wont read php syntax.

 

//connect to database

//make a whole new file called connect.php and put the following

$servername = ""; <--fill in.

$username = ""; <--fill in.

$password = ""; <--fill in.

$dbname = ""; <--fill in.

 

function connect () {

    global $servername, $username, $password, $dbname;

    $dbconn = mysql_connect($servername, $username, $password) or die(mysql_error());

    $select = mysql_select_db($dbname, $dbconn) or die(mysql_error());

} //end connect

 

//now go back to the index.php(i hope you renamed it that) and at the top place the following(within php brackets<?php  ?>

 

include "connect.php";

connect();

 

//here we bring up the data

$data = "SELECT * FROM laptop";

$result = mysql_query($data) or die(mysql_error);

$info = mysql_fetch_array($result) or die(mysql_error());

 

Change this Print "<td><a href=laptop_remove.php>".delete." [/url]</td>"; to this Print "<td><a href='laptop_remove.php?id=$info[id]'>".delete."</td>";

 

then use $info[model] and the rest tobring up each piece of data.

 

Oh and use $id = $_GET[id] instead of $_POST[id]

 

Now you should be set.

this is what i currently have

 

$id = $_POST['id'];

mysql_query("DELETE FROM `laptop` WHERE `laptop`.`id` = '$id' LIMIT 1");

echo "Success! Your item has been deleted in the inventory!";

 

 

but it still does not work, i am stumped.

 

if i run this, it will delete the record with the id 9

 

$id = $_POST['id'];

mysql_query("DELETE FROM `laptop` WHERE `laptop`.`id` = '9' LIMIT 1");

echo "Success! Your item has been deleted in the inventory!";

 

 

 

here is my phpMyAdmin sql table for laptop

 

id model           serial     svreq pic

9 IBM laptop 23T5cs         sr66321

10 HP Laptop jj44121 sr44211

 

 

my problem still is that i need it to delete whatever row i click on based on row id.  if you need more info from my files please tell me and i will post them

change this

 

Print "<td><a href='laptop_remove.php?id=".$info['id']."'>delete</td>";

 

Then on the laptop_remove.php

$id = $_GET['id'];
mysql_query("DELETE FROM laptop WHERE laptop.id = '$id' LIMIT 1");

 

Ray

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.