简体   繁体   中英

mysql date field not updating

I have the following php code to update a date field in mysql, but the date does not update. What am I doing wrong?

$id = $_GET['id']; //id is in the URL
$mysqli_insertUpdate = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); //open db conn
    if (mysqli_connect_errno()) {
            printf("Connect failed: %s", mysqli_connect_error());
            exit();
    }
    if( ! ini_get('date.timezone') )
    {
        date_default_timezone_set('EST');
    }
    $timestamp =  $_POST['year'] . "-" .  $_POST['mo'] . "-" . $_POST['day']; //derived from html fields for year, mo and day
    $timestamp = strtotime($timestamp);//turn it into Unix time
    $mysqldatetime = date("Y-m-d", $timestamp);
    echo $mysqldatetime;//"2007-12-11"
    $q_update = "UPDATE people a, markers b
SET `b.date`='2007-12-11' WHERE a.MarkerID = b.MarkerID and a.ID=".addslashes($id); //I'm just hard coding in the date here

    $r_updatevictim = $mysqli_insertUpdate->query($q_update);
    $mysqli_insertUpdate->close();

You have backticks for

`b.date` 

which should be

`b`.`date`

And better way is to use explicit join

UPDATE  markers b
join people a on a.MarkerID = b.MarkerID
SET `b`.`date`='2007-12-11' 
WHERE a.ID=".addslashes($id);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM