Jump to content

Why is this code not working


l3rodey

Recommended Posts



<?php
require '../../scripts/conn.php';
//open db here
$date = ('Y-m-d 00:00:00');
date_sub($date, date_interval_create_from_date_string('14 days'));

$getJobs = "SELECT * FROM jobs WHERE sent='0' and dateofenquiry<='$date'";
$result = mysql_query($getJobs);
while ($row = mysql_fetch_assoc($result)){
//send email
$updateJob = mysql_query("UPDATE jobs SET sent='0'", $conn);

if ($updateJob = true){
echo "Successful<br>";
}else {
echo "Nope<br>";
}
}
?>


 

That is my code not work? When I run this it will update every single row, not just ones which are 14 days after...

This is my table maybe that will help

 

id     |            dateofenquiry            |    sent

1      |       2014-01-08 13:53:29     |       0

2      |       2013-12-03 00:00:00     |       0

3      |       2013-12-03 00:00:00     |       1

 

It updates 1 and 2 and leave 3 as sent is 1. But it updates both 1 and 2... it should only update 2 as the timestamp is older then 14 days... and the first date stamp is 2 days? 

 

Help : I'm crying to why this is not working.

Link to comment
Share on other sites

Shouldn't be because that should only run from the last clause I ran? I think... If 

$getJobs = "SELECT * FROM jobs WHERE sent='0' and dateofenquiry<='$date'";

 shouldn't that only run only where sent then I run a universal update? if not what about this?

$updateJob = mysql_query("UPDATE jobs SET sent='1' WHERE sent='0' and dateofenquiry<='$date'", $conn);

That still doesn't work that still updates every row... 

Link to comment
Share on other sites

$updateJob = mysql_query("UPDATE jobs SET sent='1' WHERE sent='0' and dateofenquiry<='$date'", $conn);

That still doesn't work that still updates every row... 

 

Well, it would update any row where the conditions sent='0' and dateofenquiry<='$date' are both true. Looking at your example data, rows with id 1 and 2 both meet that criteria and would be updated to have sent = 1. The row with ID = 3 does not meet that criteria - but the sent value for that row is already a 1.

 

The issue is this

 

    $date = ('Y-m-d 00:00:00');
    date_sub($date, date_interval_create_from_date_string('14 days'));

 

On the second line, the function date_sub() returns a datetime object. It is not modifying the value of $date. Plus, you can't use a datetime object in your query. Try

 

$date = date('Y-m-d 00:00:00', strtotime('-14 days'));
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.