will808 Posted December 30, 2008 Share Posted December 30, 2008 Sorry guys, another quick question, but hopefully an easier one this time. I'm trying to use the script below to delete rows based on a link of the unique ID. For some reason, the last row is always deleted rather than the one I click on - I guess for some reason the ID is not being passed correctly to the query. Anyone have any suggestions? <?php include 'connect.php'; ?> <? //connect to mysql //then connect as user mysql_connect($host,$user,$pass); //select which database you want to edit mysql_select_db($db_name); //If cmd has not been initialized if(!isset($cmd)) { //display all the members $result = mysql_query("select id, Forename, Surname, CurrentGrade, LicenseExpiry, Phone, Mobile, Email from $table order by id"); //print table headers $fields_num = mysql_num_fields($result); echo "<h5>Table: {$table}</h5>"; echo "<div id=table><table border='1'><tr>"; // printing table headers for($i=0; $i<$fields_num; $i++) { $field = mysql_fetch_field($result); echo "<td>{$field->name}</td>"; } echo "</tr>"; //run the while loop that grabs all the Member Details while($r=mysql_fetch_array($result)) { $id=$r["id"];//take out the id $Forename=$r["Forename"];//take out the forename $Surname=$r["Surname"];//take out the surname $CurrentGrade=$r['CurrentGrade']; $LicenseExpiry=$r['LicenseExpiry']; $Phone=$r['Phone']; $Mobile=$r['Mobile']; $Email=$r['Email']; //make the ID & email a link echo "<tr><td><a href='delete.php?cmd=delete&id=$id'>$id</a></td>"; echo "<td>$Forename</td>"; echo "<td>$Surname</td>"; echo "<td>$CurrentGrade</td>"; echo "<td>$LicenseExpiry</td>"; echo "<td>$Phone</td>"; echo "<td>$Mobile</td>"; echo "<td><a href='mailto:$Email'>$Email</a></td>"; echo "</tr>"; } echo "</table></div>"; } ?> <? if($_GET["cmd"]=="delete") { $sql = "DELETE FROM $table WHERE id='$id'"; $result = mysql_query($sql); header("Location: delete.php"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/138899-solved-deleting-a-row-with-php-script/ Share on other sites More sharing options...
Maq Posted December 30, 2008 Share Posted December 30, 2008 Please never use short tags to start PHP. Your problem lies here. You never retrieve the ID from the URL so $id is always that last one extracted from the database. You need to change this portion: if($_GET["cmd"]=="delete") { $id = $_GET['id']; $sql = "DELETE FROM $table WHERE id='$id'"; $result = mysql_query($sql); header("Location: delete.php"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/138899-solved-deleting-a-row-with-php-script/#findComment-726324 Share on other sites More sharing options...
will808 Posted December 30, 2008 Author Share Posted December 30, 2008 Brilliant, thanks Maq - that worked. I've not done any PHP before, so I guess by short tags you mean using <? instead of <?php? If so, taken on board - and thanks again for the help PS - is there a "Solved" button here anywhere I can click? Quote Link to comment https://forums.phpfreaks.com/topic/138899-solved-deleting-a-row-with-php-script/#findComment-726327 Share on other sites More sharing options...
Maq Posted December 30, 2008 Share Posted December 30, 2008 Yes I mean use <?php, as the short tags could be enabled/disabled. So for portability reasons you should use the recommended way. Welcome Quote Link to comment https://forums.phpfreaks.com/topic/138899-solved-deleting-a-row-with-php-script/#findComment-726328 Share on other sites More sharing options...
PFMaBiSmAd Posted December 30, 2008 Share Posted December 30, 2008 Also related to your use (overuse) of php tags. Any white-space on a line before an opening <?php tag or any new-lines between a closing ?> tag and the next opening <?php tag will be output to the browser and will alter your page layout (spaces) and interfere with things like headers (new-lines). The spaces in the following before the <?php and <? will be output to the browser - <?php include 'connect.php'; ?> <? and the one or two new-lines in the following will be output to the browser - ?> <? You should use a minimum number of opening and closing php tags (to avoid problems with content outside of the tags and to make your code look more professional.) Since the code you posted has no in-line/static HTML, one <?php tag at the start of the file and one ?> tag at the end of the file would be the best usage. Quote Link to comment https://forums.phpfreaks.com/topic/138899-solved-deleting-a-row-with-php-script/#findComment-726334 Share on other sites More sharing options...
will808 Posted December 30, 2008 Author Share Posted December 30, 2008 Cool, thanks for the tip - there is static html in the page itself, but I just posted the PHP above - however, there is definitely white space that I can remove. Also related to your use (overuse) of php tags. Any white-space on a line before an opening <?php tag or any new-lines between a closing ?> tag and the next opening <?php tag will be output to the browser and will alter your page layout (spaces) and interfere with things like headers (new-lines). The spaces in the following before the <?php and <? will be output to the browser - <?php include 'connect.php'; ?> <? and the one or two new-lines in the following will be output to the browser - ?> <? You should use a minimum number of opening and closing php tags (to avoid problems with content outside of the tags and to make your code look more professional.) Since the code you posted has no in-line/static HTML, one <?php tag at the start of the file and one ?> tag at the end of the file would be the best usage. Quote Link to comment https://forums.phpfreaks.com/topic/138899-solved-deleting-a-row-with-php-script/#findComment-726346 Share on other sites More sharing options...
Maq Posted December 30, 2008 Share Posted December 30, 2008 He's saying instead of this (which is not consistent anyway because you use <?php and tags): Use: Now you don't have to close PHP after the include and re-open which is pointless... Quote Link to comment https://forums.phpfreaks.com/topic/138899-solved-deleting-a-row-with-php-script/#findComment-726349 Share on other sites More sharing options...
will808 Posted December 30, 2008 Author Share Posted December 30, 2008 Ah OK, gotcha - bit of cleaning up to do then... He's saying instead of this (which is not consistent anyway because you use <?php and <? tags): <?php include 'connect.php'; ?> <? Use: <?php include 'connect.php'; Now you don't have to close PHP after the include and re-open which is pointless... Quote Link to comment https://forums.phpfreaks.com/topic/138899-solved-deleting-a-row-with-php-script/#findComment-726356 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.