Frederick Marcoux Frederick Marcoux - 1 year ago 78
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:

$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 != "")
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');
header('Location: blog.php?id='.$blog_id.'&error#comments');

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


$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 Source

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

Now it works great!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download