Bronius Kusas Bronius Kusas - 1 month ago 5
MySQL Question

UPDATE to current date (PHP)

im trying to update date on the table. YYYY-MM-DD HH-MM-SS.
There is the code i have.
It takes information from table and after that I want it to set date in that table to current time

<?php
$username = "root";
$password = "sawasq";
$hostname = "localhost";

$dbhandle = mysql_connect($hostname, $username, $password) or die("Could not connect to database");

$selected = mysql_select_db("login", $dbhandle);

$code = $_POST['kodas'];
$code = stripslashes($code);

$sql = mysql_query("SELECT * FROM dviraciai WHERE ID='$code'");

$Pavadinimas = 'Pavadinimas';
$Metai = 'Metai';
$Status = 'Status';
$rows = mysql_fetch_assoc($sql);

echo 'Pavadinimas: ' . $rows[$Pavadinimas] . '<br>';
echo 'Metai: ' . $rows[$Metai] . '<br>';
echo 'Status: ' . $rows[$Status] . '<br>';

$sql2 = mysql_query("UPDATE Dviraciai WHERE ID='$code' SET date=CONCAT(CURDATE(),' ',time(mytime))");

mysql_close();
?>


I get $code from input.
Dviraciai is my table.

I dont get any error. But when i enter my $code it shows the info but doesnt change time in table after I restart phpMyAdmin

Answer

Your query is totally wrong, and since you never bother checking for errors and simply ASSUME nothing could ever go wrong...

Update syntax is

UPDATE ... SET ... WHERE...

You have the set/where reversed. And note that restarting phpmyadmin is beyond pointless. It's a MANAGEMENT INTERFACE. It's not the database itself. It's like trying to change the outcome of a tv show by turning your tv on/off.... the show's going to end up broadcasting the same ending no matter what you to do with your TV.

Never assume success with DB operations. Even if your SQL is 100% syntactically perfect (and yours definitely isn't), there's far too many OTHER reasons for a query to fail. Assuming success is, frankly, just plain stupid. Always assume failure, check for failure, and treat success as a pleasant surprise. At bare minimum, have something like this:

$result = mysql_query(...) or die(mysql_error());
Comments