homepage Welcome to WebmasterWorld Guest from 54.211.219.178
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Update query not working
LinusIT




msg:4384777
 9:45 am on Nov 8, 2011 (gmt 0)

This should be a real easy one but I can't for the life of me see why it isn't working.

Basically I've got the following link to complete a job on our system:

<?php if ($row['diary_status'] == 0) echo "<tr><td colspan=\"4\"><a href=\"job.php?id=$id&amp;action=complete\">Complete</a></td></tr>";
}} ?>


This generates a link such as job.php?id=475&action=complete

Then within job.php I've got the following code which used to work but no doesn't:

if (isset($_REQUEST['action'])) {
switch ($_REQUEST['action']) {
case 'delete':
$sql_delete1="DELETE FROM diary WHERE diary_id=$id";
$res_delete1 = mysql_query($sql_delete1) or die(mysql_error());
if($res_delete1) {
header("location: index.php?job=deleted");
exit();
}}
switch ($_REQUEST['action']) {
case 'complete':
$sql_complete="UPDATE diary SET diary_status=1 WHERE diary_id=$id";
$res_complete = mysql_query($sql_complete) or die(mysql_error());
if($res_complete) {
header("location: job.php?id=$id");
exit(); }
}}


The delete query works perfectly.

Thanks in advance

 

Wittner




msg:4384828
 12:03 pm on Nov 8, 2011 (gmt 0)

<?php if ($row['diary_status'] == 0)
// You forgot this opening brace to start the 'if' block
{
echo "<tr><td colspan=\"4\"><a href=\"job.php?id=$id amp;action=complete\">Complete</a></td></tr>";
}
// And you had an extra closing brace

?>

LinusIT




msg:4384833
 12:13 pm on Nov 8, 2011 (gmt 0)

Those extra closing brackets are from other bits in the page, I've added the missing opening brace now but it's still doing the same thing.

When you click on complete it doesn't process the code, it just goes to that url:

http://intranet/diary/job.php?id=475&action=complete

jatar_k




msg:4384851
 12:59 pm on Nov 8, 2011 (gmt 0)

so the link is fine then.

do you have display_errors on?
Is there any output at all?

try pinpointing where in the script it's dying by putting an echo or die at the top and slowly moving it down until you don't see the output and your error should be right before it.

Once you've got where it's misbehaving you can figure out why

LinusIT




msg:4384855
 1:19 pm on Nov 8, 2011 (gmt 0)

The link is fine yeah. I've got error reporting turned on using:

error_reporting(E_ALL);
ini_set('display_errors', '1');


It does output page of the page yeah, basically the header part with the menu.

Could you suggest where I can put an echo please? I've tried placing it after $res_complete = mysql_query($sql_complete) or die(mysql_error()); but it didn't show up anything.

It seems as though it doesn't know it should be doing anything. Maybe I'll try removing the "delete" part out and see if it works then.

jatar_k




msg:4384868
 1:55 pm on Nov 8, 2011 (gmt 0)

but it didn't show up anything.


then you know your error is before the line where you put it

move it up line by line until it shows, then you pinpoint the exact line

I also suggest putting a bit of static content with your or die's as if mysql_error() returns empty for some reason you would still know the error was there

or die('<p>this query: <br />' . $sql_complete . '<br /> died: <br />' . mysql_error())

maybe a bit verbose but errors need to help you figure out what happened, displaying the query is also helpful as most queries are dynamic in some way and there may be many in a given script.

LinusIT




msg:4384902
 3:06 pm on Nov 8, 2011 (gmt 0)

If I remove the delete code then it works, does this help at all?

Working Code:

if (isset($_REQUEST['action'])) {
switch ($_REQUEST['action']) {
case 'complete':
$sql_complete="UPDATE diary SET diary_status=1 WHERE diary_id=$id";
$res_complete = mysql_query($sql_complete) or die(mysql_error());
if($res_complete) {
header("location: job.php?id=$id");
exit(); }
}}

rocknbil




msg:4384942
 4:43 pm on Nov 8, 2011 (gmt 0)

First, your original code has a case with only one switch in each if. It should be

if (isset($_REQUEST['action'])) {
switch ($_REQUEST['action']) {
case 'delete':
// blah
case 'complete':
//blah
case default:
// This is very important if you use switch
// and helps debugging
echo <p>"Neither delete or complete"</p>; exit;

} // end switch
}


Did you do a var dump to see if the variables are even being posted properly? (they should be, by the looks of it . . .)

var_dump($_REQUEST);

The only other thing that comes to mind is you're only testing for isset and have no error trap. A string can be blank too and still be set. :-)

if (isset($_REQUEST['action'])) {
// code as above
}
else { echo "OOPS!" var_dump($_REQUEST); }

LinusIT




msg:4385181
 8:27 am on Nov 9, 2011 (gmt 0)

Thank you for that Rocknbill, I wasn't sure of the correct way to use switch & case. It's working fine now and I've implemented this across my site. I did have to add break; in some places though.

Thanks again for everyones help.

rocknbil




msg:4385306
 4:47 pm on Nov 9, 2011 (gmt 0)

Err, sorry, yeah. That's pretty important and I missed them. :-/

LinusIT




msg:4385366
 9:28 pm on Nov 9, 2011 (gmt 0)

Do I need to use break after every switch change?

Hope that makes sense.

jatar_k




msg:4385405
 12:06 am on Nov 10, 2011 (gmt 0)

yes, each case needs a break, otherwise they all get executed

if (isset($_REQUEST['action'])) {
switch ($_REQUEST['action']) {
case 'delete':
// blah
break;
case 'complete':
//blah
break;
case default:
// This is very important if you use switch
// and helps debugging
echo <p>"Neither delete or complete"</p>; exit;
} // end switch
}

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved