theref Posted August 21, 2008 Share Posted August 21, 2008 Hi Please could anyone have a look at the code below and help me figure out why it is not working. A user should be able to tick a box at the side of a row in the results of the query, then click the submit button. The expected outcome is that the selected row is deleted from my db, the page is refreshed and the previously selected row does not appear (because it has been deleted). However the row is not deleting. Please help. Many thanks in advance. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <!-- Get Settings Info --> <?php require_once ('../includes/settings.php'); ?> <?php require_once ('../includes/language_en.php'); ?> <head> <link rel="stylesheet" type="text/css" href="<?php echo $stylesheet ?>" /> <?php //define db variables $DBhost = $dbhost; $DBuser = $dbuser; $DBpass = $dbpass; $DBName = $dbname; //define table name for sql query $table = $prefix."messages"; //DBhost connection mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database"); mysql_select_db("$DBName") or die("Unable to select database $DBName"); //define sql query $sql="SELECT *,date_format(date_from, '%d-%m-%Y') as date_from, date_format(date_to, '%d-%m-%Y') as date_to FROM $table order by message_id DESC"; //execute sql query $result=mysql_query($sql); //count rows of result of sql query $count=mysql_num_rows($result); ?> <title><?php echo $league_name ?> - <?php echo $ref_message ?></title> </head> <body> <fieldset><legend><?php echo $delete_message ?></legend> <form method="post" action=""> <table border="1"> <tr> <td> </td> <td><strong><?php echo $ref_message_id ?></strong></td> <td><strong><?php echo $messages ?></strong></td> <td><strong><?php echo $message_date_from ?></strong></td> </tr> <?php //echo result of sql query while($rows=mysql_fetch_array($result)){ ?> <tr> <td><input name="checkbox[]" type="checkbox" value="<?php echo $rows['message_id']; ?>" /></td> <td><?php echo $rows['message_id']; ?></td> <td><?php echo $rows['message']; ?></td> <td><?php echo $rows['date_from']; ?></td> </tr> <?php } ?> <tr> <td colspan="7"><input name="delete" type="submit" id="delete" value="<?php echo $delete_checked_message ?>" /></td> </tr> <? // Check if delete button active, start this if($delete){ for($i=0;$i<$count;$i++){ $del_id = $checkbox[$i]; $del_id = mysql_real_escape_string($del_id); $sql = "DELETE FROM $table WHERE message_id='$del_id'"; $result = mysql_query($sql); } //close mysql connection mysql_close(); // if successful redirect to delmess.php if($result){ echo "<meta http-equiv=\"refresh\" content=\"0;URL=", $admin , "delmess.php\">"; } } ?> </table> </form> </fieldset> <table> <tr> <td><a href="<?php echo $admin ?>index.php"><?php echo $go_to_admin ?></a></td> </tr> </table> <div id="admin_footer"> <table> <tr> <td><br /> <p> <a href="http://validator.w3.org/check?uri=referer"> <img src="<?php echo $ref ?>images/valid-xhtml10-blue.png" alt="Valid XHTML 1.0 Strict" height="31" width="88" /></a> <a href="http://jigsaw.w3.org/css-validator/check/referer/"> <img style="border:0;width:88px;height:31px" src="<?php echo $ref ?>images/valid-css-blue.png" alt="Valid CSS!" /></a> </p> </td> </tr> <tr> <td> <p> <script type="text/javascript"> <!-- today=new Date(); y0=today.getFullYear(); --> </script> Ref App - <?php echo $version ?> © <a href="mailto:"></a> 2006 - <script type="text/javascript"> <!--- document.write(y0); --> </script> </p> </td> </tr> </table> </div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/120740-solved-php-code-stopped-working-please-help/ Share on other sites More sharing options...
Mchl Posted August 21, 2008 Share Posted August 21, 2008 Are short php tags (<? ?>) enabled on your server? You should use full tags (<?php ?>) as the short tags are deprecated. Link to comment https://forums.phpfreaks.com/topic/120740-solved-php-code-stopped-working-please-help/#findComment-622268 Share on other sites More sharing options...
theref Posted August 21, 2008 Author Share Posted August 21, 2008 Thanks for that. I corrected the <?php ?> tags but unfortunately that didn't help. With ini_set('display_errors', 'On'); error_reporting(E_ALL); in my page I get the following notice: Notice: Undefined variable: delete in /home/planetca/public_html/refapp/admin/delmess.php on line 77 Does this have anything to do with the page not working? Interestingly the page worked before my web host upgraded to php/mysql 5.x Link to comment https://forums.phpfreaks.com/topic/120740-solved-php-code-stopped-working-please-help/#findComment-622272 Share on other sites More sharing options...
akitchin Posted August 21, 2008 Share Posted August 21, 2008 it's most likely because register_globals has been turned off. you'll need to change any values you're fetching from the form to $_POST['variableName'] as opposed to just $variableName. that includes $delete. also, instead of using a for() loop, you can simply use a foreach() loop to make things easier: foreach ($_POST['checkbox'] AS $del_id) { // make delete queries and whatnot } Link to comment https://forums.phpfreaks.com/topic/120740-solved-php-code-stopped-working-please-help/#findComment-622276 Share on other sites More sharing options...
theref Posted August 21, 2008 Author Share Posted August 21, 2008 Many thanks. I added the following $checkbox = $_POST['checkbox']; and changed $delete to be $_POST['delete'] and everything works as expected. Many thanks for all your replies Link to comment https://forums.phpfreaks.com/topic/120740-solved-php-code-stopped-working-please-help/#findComment-622290 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.