Update database within specific date

Hi,

I have a table which store date and emailStatus. The email status will change from true to false after 13 weeks automatically.

What should i do?

Thank you in advance :slight_smile:

What is your real question? Are you asking for us to create code for you? Show some code and we can help you fix it. If you need to start from scratch look here to get started: MySQL UPDATE Example

1 Like

Yeah i nees to start from scratch. Thanks for the link

You are welcome. That site is a beginner’s site. Quite often the code is not perfect, but, it has a vast amount of info on it for most examples you will need to start out. When stuck, just post back here and we can help you out further. Good luck!

Hi, i tried to run this code. The code display “Success” but no data in database updated.

$sql = “SELECT * FROM platform WHERE EmailDate IS NOT NULL”;
$result = sqlsrv_query($conn, $sql);

$emailDate = array();
$ph_no = array();

while ($row = sqlsrv_fetch_array($result))
{
$emailDate = $row[‘EmailDate’]-> format('Y-m-d ‘);
$ph_no = $row[‘ph_no’];
$uncheckDate = date(‘Y-m-d’, strtotime($emailDate. ’ + 1 days’));

if($emailDate != $uncheckDate)
{
	$query = "UPDATE platform
			  SET isSelected = 1
			  WHERE ph_no = '$ph_no'";
			 
	$stmt_uncheck = sqlsrv_query($conn, $query);
	
	if($stmt_uncheck == false)
	{
		die( print_r( sqlsrv_errors(), true));
	}
	else{ echo "Success"; }
	}
else
{
	$query = "UPDATE platform
			  SET isSelected = 0,
			  EmailDate = null
			  WHERE ph_no = '$ph_no'";
			  echo $query;
	$stmt_uncheck = sqlsrv_query($conn, $query);
	echo $stmt_uncheck;
	echo "</br>";
	if($stmt_uncheck == false)
	{
		die( print_r( sqlsrv_errors(), true));
	}
	else{ echo "Success"; }
}

}

Well, it is hard to read your code. You should indent it in a different manner to make it easier to see the breakdown of the nested if-else’s. Keeping it in your current format but, moving the ELSE parts…

while ($row = sqlsrv_fetch_array($result))
{
     $emailDate = $row['EmailDate']-> format('Y-m-d');
     $ph_no = $row[‘ph_no’];
     $uncheckDate = date(‘Y-m-d’, strtotime($emailDate. ’ + 1 days’));

     if($emailDate != $uncheckDate)
     {
	   $query = "UPDATE platform
			  SET isSelected = 1
			  WHERE ph_no = '$ph_no'";
			 
	   $stmt_uncheck = sqlsrv_query($conn, $query);
	
	    if($stmt_uncheck == false)
	     {
		 die( print_r( sqlsrv_errors(), true));
	     }
	     else
             {
                  echo "Success";
             }
     }
     else
     {
	   $query = "UPDATE platform
			  SET isSelected = 0,
			  EmailDate = null
			  WHERE ph_no = '$ph_no'";
			  echo $query;
	   $stmt_uncheck = sqlsrv_query($conn, $query);
 	   echo $stmt_uncheck;
           echo "</br>";
	   if($stmt_uncheck == false)
	   {
		die( print_r( sqlsrv_errors(), true));
	   }
	   else
          {
                echo "Success"; 
           }
}

After looking at the code, I can not determine what data you entered into the script. So, I am not sure which SUCCESS line was displayed. Looking further at your code, it is a waste of extra lines. I would make the else lines like this: } else { instead of using three lines. But, the real logic issue is why you have two separate updates that use the exact same code. Logically, I would do it something like this:

while ($row = sqlsrv_fetch_array($result))
{
    $emailDate = $row[‘EmailDate’]-> format('Y-m-d ‘);
    $ph_no = $row[‘ph_no’];
    $uncheckDate = date(‘Y-m-d’, strtotime($emailDate. ’ + 1 days’));

    if($emailDate != $uncheckDate)
    {
            $query = "UPDATE platform
                              SET isSelected = 1
                              WHERE ph_no = '$ph_no'";
    } else {
            $query = "UPDATE platform
                              SET isSelected = 0,
                              EmailDate = null
                              WHERE ph_no = '$ph_no'";
                              echo $query;
    }
    $stmt_uncheck = sqlsrv_query($conn, $query);
    echo $stmt_uncheck;
    echo "</br>";
    if($stmt_uncheck == false)
    {
            die( print_r( sqlsrv_errors(), true));
    } else {
            echo "Success";
    }
}

Try it again and if it fails, have it also display the $ph_no, $emailDate and $uncheckDate so you can debug it further. Hard to follow the code not know what these are. Quite often, knowing what is going into the script let’s you sort out the “flow” of the nested if-else’s. Good luck!

Sponsor our Newsletter | Privacy Policy | Terms of Service