Jump to content

Archived

This topic is now archived and is closed to further replies.

SephirGaine

Updating a table, using 2 filters

Recommended Posts

Whooptie. I'm back (unfortunately, again), however this time my question's a lot different. I'll start off by posting my code..

[code]<?php
$query1 = "UPDATE `WOR` SET Invoice=Yes where work_order_num='$ordernum' where Invoice=No";
$query2 = "UPDATE `WOR` SET Invoice=No where work_order_num='$ordernum' where Invoice=Yes";
?>
<?php
if (mysql_query($query1) or die(mysql_error())
else {
mysql_query($query2) or die(mysql_error();
}
?>[/code]

What I want to do is to basically switch the "Invoice" section of an entry to Yes or No, much like a toggle. However, I'm running into a few problems when I try to run the script. Unfortunately I'm not sure where I've gone wrong. I can see it being either I can't use 2 "where" statements in a single query, or it's in my else statement (syntax error somewhere). Any help would be appreciated, and if there's a simpler way for me to do this, I'm all ears. This is just what I came up with off the top of my amateur head. Thanks!

Share this post


Link to post
Share on other sites
<?php
$query = "UPDATE `WOR` SET Invoice=Yes where work_order_num='$ordernum' AND Invoice=No;";
$query .= "UPDATE `WOR` SET Invoice=No where work_order_num='$ordernum' AND Invoice=Yes;";

$qry = mysql_query($query);
if 9mysql_affected_rows() == 0)
{
// output some error warninh
}

?>

Share this post


Link to post
Share on other sites
Hm.. getting a syntax error when I try it. I get the idea though, so thanks! Changed what you gave me just a bit, came up with this..

[code]<?php
  $query = "UPDATE `WOR` SET Invoice=Yes where work_order_num='$ordernum' AND Invoice=No";
  $query .= "UPDATE `WOR` SET Invoice=No where work_order_num='$ordernum' AND Invoice=Yes";
  ?>
  <?php
mysql_query($query) or die(mysql_error());
if (mysql_affected_rows() == 0)
{
echo("An error has occured.");
}
?>[/code]

Share this post


Link to post
Share on other sites
"UPDATE `WOR` SET `Invoice`=(SELECT IF(`Invoice`='Yes','No',Yes')) where work_order_num='$ordernum'"

Share this post


Link to post
Share on other sites
Most unfortunately..

[quote]You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT IF(Invoice='Yes','No','Yes')) where work_order_num='2''[/quote]

This is what I've got..

[code]<?php
  $query = "UPDATE `WOR` SET Invoice=(SELECT IF(Invoice='Yes','No','Yes')) where work_order_num='$ordernum'"
  ?>
  <?php
mysql_query($query) or die(mysql_error());
?>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.