I am trying to enter 5 variables into a MySQL database. Here is the code:
foreach ($avail_t as $row) {
$ava = explode("+",$row);
$day = $ava[0];
$from = $ava[1];
$to = $ava[2];
//echo $day." ".$from." ".$to;
$query = "INSERT INTO availability (username, login_value, day, from, to) VALUES ('{$_SESSION['username']}', '{$_SESSION['login_value']}', '{$day}', '{$from}', '{$to}')";
mysql_query($query);
}
When I uncomment that echo statement all variables print out just fine, but when I process the query it doesn't enter into the database. Weirdly, if I cut out $from and $to and just enter $day it will enter the day. When I put back the $from and $to nothing gets entered.
Do you see any problems with this code? Data type is integer in military time format 2100, 1300 etc. for $from and $to.
echo $query;, to make sure it contains what you think it does.mysql_*functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.