jaya
jaya

Reputation: 327

date inserted in wrong format in db after update using PHP?

I am using date time picker to get date and have one datetime type field in db which I am trying to update.

Following jquery code makes a request to edit.php

var nextdate=  $("#"+date).attr('value');
$('#div').load('edit.php?date='+nextdate);

In edit.php, I'm getting the correct date 2013-04-18 18:18:18

But in database it is inserted in this format: 2013-04-18 00:00:00, where the actual date is 2013-04-18 18:18:18

I've also tried

echo $d =$_GET['date'];
echo $date=date("Y-m-d H:i:s",strtotime($d));

Note: update query directly on db is working fine.

PHP code is

update call_track  set  next_call_time='$date'   where  id='$id'

and the database query is

update call_track set next_call_time='2013-04-18 18:18:18' where id='118' 

why this is not working with PHP code.

this is the code on update.php

function leaveChanger(id)
{
    var tpid ='display_'+id;
    var date='date_'+id;
    var nextdate=  $("#"+date).attr('value');

    $('#'+tpid).load('edit.php?id='+id+'&date='+nextdate);


}
</script>
<input type='button' name='' id='' value='Change'  class='button green' Onclick='leaveChanger(118);return false;'/> <div id='dispaly_$id' style='color:red;'></div> 
<input type='Text' value='$date' id='date_118' maxlength='25' size='19'/>
<img src='images2/cal.gif' onclick=\"javascript:NewCssCal ('date_118','yyyyMMdd','arrow',true,'24',true)\"/> <div id='display_118'></div>

edit.php

include("../../dashboard/Includes/db_connect.php");
 echo $id=$_GET['id']; 

echo $d =$_GET['date'];
echo $date=date("Y-m-d H:i:s",strtotime($d)); 

echo $query="update  call_track set next_call_time='$date' where id='$id'";

if(mysql_query($query))
echo "Updated"; 
else
echo "Not Updated";

Upvotes: 0

Views: 276

Answers (2)

jaya
jaya

Reputation: 327

Thanks all of you. Problem was solved by placing

    var nextdate=  $("#"+date).attr('value');
    var date =encodeURIComponent(nextdate);

    $('#'+tpid).load('edit.php?id='+id+'&date='+date);

encodeURIComponent was solved my problem.

Upvotes: 1

Brewal
Brewal

Reputation: 8189

You are difining $pid variable, not $id. Replace this line :

$query="update call_track next_call_time='$date' where id='$id'";

by this one :

$query="update call_track next_call_time='$date' where id='$pid'";

Next time, try to var_dump() the entire query : var_dump($query); You will find that the generated query was :

update call_track set next_call_time='2013-04-18 18:18:18' where id=''

This affects 0 rows as there is no record matching an empty id.

Upvotes: 0

Related Questions