bschultz Posted March 24, 2007 Share Posted March 24, 2007 I'm having a problem getting the proper syntax to get this working. I want to display (in an html form) the results currently in the database. I then want to be able to update the records. This part of the script is working. Now, I'm trying to add a "delete row" clause within the update script. Here's what I have so far: <? putenv("TZ=US/Central"); $DBhost = "localhost"; $DBuser = "username"; $DBpass = "password"; $DBName = "cancellations"; mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database"); @mysql_select_db("$DBName") or die("Unable to select database $DBName"); $date = $_POST[date]; $type = $_POST[type]; $event = $_POST[event]; $action = $_POST[action]; $comments = $_POST[comments]; $row_number = $_POST[row_number]; $cmd = $_POST[cmd]; for ($i=0;$i<count($_POST[event]);$i++){ if('$cmd[$i]'=="delete") { $sqldelete = "DELETE FROM cancellations WHERE row_number='$row_number[$i]'"; $resultdelete = mysql_query($sqldelete); echo $sqldelete; // echo "Row deleted!"; } else $usql = "UPDATE cancellations SET date='$date[$i]', type='$type[$i]', event='$event[$i]', action='$action[$i]', comments='$comments[$i]' WHERE row_number='$row_number[$i]'"; // above compiles query $dosql = mysql_query($usql); // executes query } if ($dosql){ echo $usql; } else{ echo mysql_errno().": ".mysql_error()."<BR>"; } mysql_close (); ?> The delete clause isn't doing anything...just the update clause is working. Any ideas? thanks for your help. Brian Quote Link to comment https://forums.phpfreaks.com/topic/44139-solved-update-or-delete-row-problem/ Share on other sites More sharing options...
paul2463 Posted March 24, 2007 Share Posted March 24, 2007 try this and see if there is an error if('$cmd[$i]'=="delete") { $sqldelete = "DELETE FROM cancellations WHERE row_number='$row_number[$i]'"; $resultdelete = mysql_query($sqldelete) or die ("Error in Delete Query" . mysql_error()); echo $sqldelete; // echo "Row deleted!"; } Quote Link to comment https://forums.phpfreaks.com/topic/44139-solved-update-or-delete-row-problem/#findComment-214334 Share on other sites More sharing options...
bschultz Posted March 24, 2007 Author Share Posted March 24, 2007 No error. This is the result echoed by this line: if ($dosql){ echo $usql; UPDATE cancellations SET date='2007-03-24', type='business', event='bagley library', action='Closed', comments='' WHERE row_number='17' So, it appears that the code isn't doing anything for the delete if statement. Quote Link to comment https://forums.phpfreaks.com/topic/44139-solved-update-or-delete-row-problem/#findComment-214335 Share on other sites More sharing options...
paul2463 Posted March 24, 2007 Share Posted March 24, 2007 that has echoed out the update query, i thought the problem is with the delete query? if it got into the "if('$cmd[$i]'=="delete")" then it would have echoed out the delete query Quote Link to comment https://forums.phpfreaks.com/topic/44139-solved-update-or-delete-row-problem/#findComment-214336 Share on other sites More sharing options...
bschultz Posted March 24, 2007 Author Share Posted March 24, 2007 That's why I said that it doesn't appear to be doing anything on the delete if statement. I'm starting to think I have a logic problem here anyway. This script will display EVERY record in the database matching today's date. If the code I posted actually worked, it would stop at the first delete statement. I want to be able to echo ALL records into the html form, edit ALL records that need to be edited, and delete all the records that need to be deleted ALL AT ONCE. The way that I wrote this code, that won't happen. Any ideas on how to lay out this code to accomplish what I want? Quote Link to comment https://forums.phpfreaks.com/topic/44139-solved-update-or-delete-row-problem/#findComment-214343 Share on other sites More sharing options...
paul2463 Posted March 24, 2007 Share Posted March 24, 2007 sorry misunderstood, try this $cmd = $_POST[cmd];//because you have this line and its not an array if( $cmd =="delete") // you just need this line changed to read this without the [$i] { $sqldelete = "DELETE FROM cancellations WHERE row_number='$row_number[$i]'"; $resultdelete = mysql_query($sqldelete) or die ("Error in Delete Query" . mysql_error()); echo $sqldelete; // echo "Row deleted!"; } Quote Link to comment https://forums.phpfreaks.com/topic/44139-solved-update-or-delete-row-problem/#findComment-214346 Share on other sites More sharing options...
bschultz Posted March 24, 2007 Author Share Posted March 24, 2007 I tried that, but it still didn't work. By the way, why wouldn't $cmd be an array? If there are 20 records matching today's date, I'll need $cmd[2] to delete the second row...otherwise, it deletes everything, wouldn't it? Quote Link to comment https://forums.phpfreaks.com/topic/44139-solved-update-or-delete-row-problem/#findComment-214352 Share on other sites More sharing options...
paul2463 Posted March 24, 2007 Share Posted March 24, 2007 ok i made an assumption that it wasnt an array - my fault... echo $cmd[$i]; // just to see what it says if($cmd[$i] =="delete") //removed the single ticks from around the variable...is it "delete" or "Delete" that will be sent?? { $sqldelete = "DELETE FROM cancellations WHERE row_number='$row_number[$i]'"; $resultdelete = mysql_query($sqldelete) or die ("Error in Delete Query" . mysql_error()); echo $sqldelete; // echo "Row deleted!"; } Quote Link to comment https://forums.phpfreaks.com/topic/44139-solved-update-or-delete-row-problem/#findComment-214355 Share on other sites More sharing options...
bschultz Posted March 24, 2007 Author Share Posted March 24, 2007 that was it (single quotes)...THANK YOU! Quote Link to comment https://forums.phpfreaks.com/topic/44139-solved-update-or-delete-row-problem/#findComment-214356 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.