draftermath Posted February 15, 2007 Share Posted February 15, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/38643-solved-deleting-row-of-table-based-on-auto-increment-number/ Share on other sites More sharing options...
pikemsu28 Posted February 15, 2007 Share Posted February 15, 2007 $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 Quote Link to comment https://forums.phpfreaks.com/topic/38643-solved-deleting-row-of-table-based-on-auto-increment-number/#findComment-185538 Share on other sites More sharing options...
mbtaylor Posted February 15, 2007 Share Posted February 15, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/38643-solved-deleting-row-of-table-based-on-auto-increment-number/#findComment-185550 Share on other sites More sharing options...
draftermath Posted February 15, 2007 Author Share Posted February 15, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/38643-solved-deleting-row-of-table-based-on-auto-increment-number/#findComment-185646 Share on other sites More sharing options...
Neptunus Maris Posted February 15, 2007 Share Posted February 15, 2007 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 } Quote Link to comment https://forums.phpfreaks.com/topic/38643-solved-deleting-row-of-table-based-on-auto-increment-number/#findComment-185653 Share on other sites More sharing options...
Neptunus Maris Posted February 15, 2007 Share Posted February 15, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/38643-solved-deleting-row-of-table-based-on-auto-increment-number/#findComment-185654 Share on other sites More sharing options...
Balmung-San Posted February 15, 2007 Share Posted February 15, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/38643-solved-deleting-row-of-table-based-on-auto-increment-number/#findComment-185658 Share on other sites More sharing options...
draftermath Posted February 15, 2007 Author Share Posted February 15, 2007 Neptunus, it is a .php file. Quote Link to comment https://forums.phpfreaks.com/topic/38643-solved-deleting-row-of-table-based-on-auto-increment-number/#findComment-185666 Share on other sites More sharing options...
dre Posted February 15, 2007 Share Posted February 15, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/38643-solved-deleting-row-of-table-based-on-auto-increment-number/#findComment-185670 Share on other sites More sharing options...
Neptunus Maris Posted February 15, 2007 Share Posted February 15, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/38643-solved-deleting-row-of-table-based-on-auto-increment-number/#findComment-185673 Share on other sites More sharing options...
draftermath Posted February 15, 2007 Author Share Posted February 15, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/38643-solved-deleting-row-of-table-based-on-auto-increment-number/#findComment-185738 Share on other sites More sharing options...
craygo Posted February 15, 2007 Share Posted February 15, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/38643-solved-deleting-row-of-table-based-on-auto-increment-number/#findComment-185756 Share on other sites More sharing options...
draftermath Posted February 16, 2007 Author Share Posted February 16, 2007 craygo, you r the shizz, thanks Quote Link to comment https://forums.phpfreaks.com/topic/38643-solved-deleting-row-of-table-based-on-auto-increment-number/#findComment-186225 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.