Birdmansplace Posted December 28, 2009 Share Posted December 28, 2009 I found this in tutorials http://www.phpfreaks.com/tutorial/simple-sql-search and i liked the set up of that (thanks to who wrote it). I am making a "request off" db for work and i am looking for how to delete row buy row using sid I have searched for it here and cant figure it out. heres the page code i am usein to display it and would like to know how to get delete to work let alone code it. <? ini_set("display_errors", "1"); error_reporting(E_ALL); include("dbinfo.php"); mysql_connect('localhost',$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM simple_search ORDER BY sid"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); $i=0; while ($i < $num) { $sid=mysql_result($result,$i,'sid'); $stitle=mysql_result($result,$i,'stitle'); $sdescription=mysql_result($result,$i,'sdescription'); $sbody=mysql_result($result,$i,'sbody'); ?> <div align="center"> <br /> <br /> <? echo $sid; ?> <?php echo $stitle; ?> <?php echo $sdescription; ?> <?php echo $sbody; ?> <? echo "<a href='link.for.delete.code.will.go.here'><input name='' type='button' value='delete'/></a>"; ?> <? $i++;} ?> this is code that i have found and was trying to use for delete: <?php include "dbinfo.php"; // if id provided, then delete that record $sid=$_GET['sid'] ; // create query to delete record $query = "DELETE FROM simple_search WHERE id = '$sid' "; $result = mysql_query($query) or die ("epic FAIL!") ?> Quote Link to comment Share on other sites More sharing options...
Buddski Posted December 28, 2009 Share Posted December 28, 2009 <? ini_set("display_errors", "1"); error_reporting(E_ALL); include("dbinfo.php"); mysql_connect('localhost',$username,$password); @mysql_select_db($database) or die( "Unable to select database"); if (isset($_GET['delete'])) { $query = "DELETE FROM simple_search WHERE id = '".$_GET['delete']."'"; $result = mysql_query($query) or die ("epic FAIL!") } $query="SELECT * FROM simple_search ORDER BY sid"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); $i=0; while ($i < $num) { $sid=mysql_result($result,$i,'sid'); $stitle=mysql_result($result,$i,'stitle'); $sdescription=mysql_result($result,$i,'sdescription'); $sbody=mysql_result($result,$i,'sbody'); ?> <div align="center"> <br /> <br /> <? echo $sid; ?> <?php echo $stitle; ?> <?php echo $sdescription; ?> <?php echo $sbody; ?> <? echo "<a href=nameofthisscript.php?delete=$sid'><input name='' type='button' value='delete'/></a>"; ?> <? $i++;} ?> may work. its not tested tho Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 28, 2009 Share Posted December 28, 2009 There are a few different issues. 1. That code is way more complex than it needs to be. There is a lot of "logic" in there to extract and display the records that is not needed. A simple while loop with mysql_fetch_assoc(), or similar function will provide all the logic for stepping through the records and providing variables to display. 2. You are putting a button withing anchor tags. That doesn't work. You would either need to a) create a standard text link or b) create mini forms with the buttons as submit buttons. I'll provide an example with just text links. There is a third option of using JavaScript onthe buttons, but that again introduces unneeded complexity. 3. That code is using short tags (i.e. <?). That's not good programming standard as not all servers will support that. 4. There are problems with the HTML. For instance, each loop is opening a new DIV but never closing them. <?php ini_set("display_errors", "1"); error_reporting(E_ALL); include("dbinfo.php"); mysql_connect('localhost',$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = "SELECT `stitle`, `sdescription`, `sbody`, `sid` FROM simple_search ORDER BY sid"; $result = mysql_query($query); if (!$result) { //Temporary error handling echo "There was a problem:<br />".mysql_error(); } while($record = mysql_fetch_assoc($result)) { echo "<br /><br />"; echo "{$record['sid']} {$record['stitle']} {$record['sdescription']} {$record['sbody']} "; echo "<a href=\"deletePage.php?sid={{$record['sid']}}\">Delete</a>\n"; } ?> Quote Link to comment Share on other sites More sharing options...
Birdmansplace Posted December 28, 2009 Author Share Posted December 28, 2009 how about the deletepage.php code. mine is the second "code" in first post. could you provide me with code that will work? Thanks Quote Link to comment Share on other sites More sharing options...
Buddski Posted December 28, 2009 Share Posted December 28, 2009 The delete code you provided in conjunction with mjdamato's version of the main script should work fine.. Quote Link to comment Share on other sites More sharing options...
Birdmansplace Posted December 28, 2009 Author Share Posted December 28, 2009 it doesnt work. Quote Link to comment Share on other sites More sharing options...
Buddski Posted December 28, 2009 Share Posted December 28, 2009 A little more detail that 'It doesnt work' might help us fix it.. Error messages etc.. Quote Link to comment Share on other sites More sharing options...
Birdmansplace Posted December 28, 2009 Author Share Posted December 28, 2009 code for display: <?php ini_set("display_errors", "1"); error_reporting(E_ALL); include("dbinfo.php"); mysql_connect('localhost',$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = "SELECT `stitle`, `sdescription`, `sbody`, `sid` FROM simple_search ORDER BY sid"; $result = mysql_query($query); if (!$result) { //Temporary error handling echo "There was a problem:<br />".mysql_error(); } while($record = mysql_fetch_assoc($result)) { echo "<br /><br />"; echo "{$record['sid']} {$record['stitle']} {$record['sdescription']} {$record['sbody']} "; echo "<a href=\"deletepage.phpl?sid={{$record['sid']}}\">Delete</a>\n"; } ?> deletepage.php <?php include "dbinfo.php"; // if id provided, then delete that record $sid=$_GET['sid'] ; // create query to delete record $query = "DELETE FROM simple_search WHERE id = '$sid' "; $result = mysql_query($query) or die ("epic FAIL!") ?> Quote Link to comment Share on other sites More sharing options...
Birdmansplace Posted December 28, 2009 Author Share Posted December 28, 2009 not getting any error messages eather. Quote Link to comment Share on other sites More sharing options...
Buddski Posted December 28, 2009 Share Posted December 28, 2009 $query = "DELETE FROM simple_search WHERE sid = '$sid' "; Quote Link to comment Share on other sites More sharing options...
Birdmansplace Posted December 28, 2009 Author Share Posted December 28, 2009 just added that to the deletepage.php i get: epic FAIL Quote Link to comment Share on other sites More sharing options...
Buddski Posted December 28, 2009 Share Posted December 28, 2009 Read my updated post. $query = "DELETE FROM simple_search WHERE sid = '$sid' "; And epic FAIL is an error message but mysql_error() would have been nice too Quote Link to comment Share on other sites More sharing options...
Birdmansplace Posted December 28, 2009 Author Share Posted December 28, 2009 i see i missed the s in sid. still get same error. Quote Link to comment Share on other sites More sharing options...
Buddski Posted December 28, 2009 Share Posted December 28, 2009 $query = "DELETE FROM simple_search WHERE sid = '$sid' "; $result = mysql_query($query) or die ("epic FAIL because: ".mysql_error()); Quote Link to comment Share on other sites More sharing options...
Birdmansplace Posted December 28, 2009 Author Share Posted December 28, 2009 epic FAIL because: No database selected Quote Link to comment Share on other sites More sharing options...
Buddski Posted December 28, 2009 Share Posted December 28, 2009 mysql_connect('localhost',$username,$password); @mysql_select_db($database) or die( "Unable to select database"); at the top of your delete page under your dbinfo include Quote Link to comment Share on other sites More sharing options...
Birdmansplace Posted December 28, 2009 Author Share Posted December 28, 2009 yay no more errors but still doesnt work. Quote Link to comment Share on other sites More sharing options...
Buddski Posted December 28, 2009 Share Posted December 28, 2009 Can you post the entire delete code here for us.. Quote Link to comment Share on other sites More sharing options...
Birdmansplace Posted December 28, 2009 Author Share Posted December 28, 2009 <?php ini_set("display_errors", "10"); error_reporting(E_ALL); include "dbinfo.php"; mysql_connect('localhost',$username,$password); @mysql_select_db($database) or die( "Unable to select database"); // if id provided, then delete that record $sid=$_GET['sid'] ; // create query to delete record $query = "DELETE FROM simple_search WHERE sid = '$sid' "; $result = mysql_query($query) or die ("epic FAIL because: ".mysql_error()); ?> once you click delete from the "display" page it takes you to the page this code is on. Quote Link to comment Share on other sites More sharing options...
Buddski Posted December 28, 2009 Share Posted December 28, 2009 change your $result line to this.. $result = mysql_query($query); if ($result) { echo 'Query didnt fail - Affected rows: '.mysql_affected_rows($result); } else { echo 'Query failed: '.mysql_error(); } Quote Link to comment Share on other sites More sharing options...
Birdmansplace Posted December 28, 2009 Author Share Posted December 28, 2009 Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource in ......delete1.php on line 13 Query didnt fail - Affected rows: Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 28, 2009 Share Posted December 28, 2009 mysql_affected_rows does not take the result resource (it optionally takes the link identifier.) Quote Link to comment Share on other sites More sharing options...
Buddski Posted December 28, 2009 Share Posted December 28, 2009 Sorry. echo 'Query didnt fail - Affected rows: '.mysql_affected_rows(); Quote Link to comment Share on other sites More sharing options...
Birdmansplace Posted December 28, 2009 Author Share Posted December 28, 2009 np Query didnt fail - Affected rows: 0 Quote Link to comment Share on other sites More sharing options...
Buddski Posted December 28, 2009 Share Posted December 28, 2009 That means the query ran but it didnt find anything to delete.. Quote Link to comment 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.