Jump to content

not sure re. WHERE clause


ckdoublenecks

Recommended Posts

I could use some help with this code.  I am trying to update this database table directly (no form). It said "5 records updated". There are 5 records in the database and I checked and all 5 had been changed. Problem is that only 3 should have been changed. The WHERE clause in the SELECT statement must have been ignored. Also how do I SET the datepaid to " " instead of 0?

 

<?php
if (!$conn = @mysql_connect(localhost,root,"")) {
echo 'could not connect to MYSQL: ' .mysql_error();
exit;
}
if(!@mysql_select_db('test')) {
   echo 'Unable to select database: ' .mysql_error();
   exit;
}

// mysql_query returns false on failure
  $sql = "SELECT amtpaid, rentdue, prevbal, misc, tentpay, hudpay, datepaid FROM  testtable WHERE misc + prevbal + rentdue = amtpaid";

$result = @mysql_query($sql);
if(!$result) {
    echo "could not run query (sql); " .mysql_error();
   exit;
}
// if we expect rows, let's make sure some exist
if(mysql_num_rows($result) < 1) {
   echo "no rowsmatch your query, Please try again";
   exit;
}
$query="UPDATE testtable SET amtpaid=0, prevbal=0, misc=0,tentpay=0, hudpay=0, datepaid=0 ";

// Run query (with verbose error message)...
mysql_query($query) or die(mysql_error()."<br />\n".$query);

// Get number of affected rows (if any)...
echo mysql_affected_rows($conn)." Records Updated";
mysql_close();
?>

 

 

 

 

Link to comment
Share on other sites

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("test", $con);
// for every tenant record, if the rentdue, prevbal & misc equals the 
// amtpaid, clear the misc, prevbal, amtpaid, tentpay and datepaid
mysql_query("UPDATE testtable SET amtpaid = '0',  prevbal = '0', misc = '0', tentpay = '0', datepaid = ' '
WHERE misc + prevbal + rentdue = amtpaid");
mysql_close($con);
?> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.