Frederick Marcoux Frederick Marcoux - 7 months ago 14
SQL Question

MySQL INSERT query doesn't work

I'm trying to do a simple MySQL INSERT query but it seems to not work... I entered it directly in PHPmyAdmin and it works, but not in PHP...

There's my code:

<?php
$id = rand(0000,9999);
$user_id = mysql_real_escape_string($_POST['user_id']);
$content = mysql_real_escape_string($_POST['content']);
$blog_id = mysql_real_escape_string($_POST['blog_id']);
$moderated = 1;
$date = date("Y\-m\-d");
$time = date("H\:i\:s");

if ($user_id != "" && $content != "" && $blog_id != "")
{
include('php/connect_db.php');
mysql_query("INSERT INTO `comments` (`id`,`user_id`,`content`,`blog_id`,`moderated`,`date`,`time`) VALUES ('".$id."','".$user_id."','".$content."','".$blog_id."','".$moderated."','".$date."','".$time."')");
header('Location: blog.php?id='.$blog_id.'#comments');
}
else
{
header('Location: blog.php?id='.$blog_id.'&error#comments');
}
?>


It doesn't work but I don't know why...

connect_db.php


<?php
$con = mysql_connect($db_host, $db_user, $db_pass);
if (!$con)
{
die('Could not connect to database: ' . mysql_error());
}

mysql_select_db($db_name, $con);
?>

Answer

I was not including the config.cms.php that contains the connexion info to database. FAIL!

Now it works great!