mysql_query Problem

[php]if(isset($_POST[“button”]))
{
$title = $_POST[“title”];
$color = $_POST[“color”];
$message = $_POST[“message”];
$date = date(“m/d/Y”);
$by = $_SESSION[“admin”];
mysql_query(“INSERT INTO Blog (title, date, by, content, color)
VALUES (’$title’, ‘$date’, ‘$by’, ‘$message’, ‘$color’)”) or mysql_error();
$content = ‘Thank you for posting this message.

[Go Back]’;
}
[/php]

It’s not adding to my database, I have connected to it with:

[php]mysql_connect(localhost,“root”,“my password is here”);
mysql_select_db(“Final Resurrection”) or die(“Could not connect to the database”);[/php]

the mysql_error() is doing nothing, but if I put die(“This post has not been added to the database”), it just outputs the die function.

Hi there,

Try putting speech marks around localhost in your connect().

Tried that, it’s still not working.

I’ve put:
[php]if(isset($_POST[“button”]))
{
$title = $_POST[“title”];
$color = $_POST[“color”];
$message = $_POST[“message”];
$date = date(“m/d/Y”);
$by = $_SESSION[“admin”];
mysql_query(“INSERT INTO Blog (title, date, by, content, color)
VALUES (’$title’, ‘$date’, ‘$by’, ‘$message’, ‘$color’)”) or mysql_error();
$content = ‘Thank you for posting this message.

[Go Back]

Title:’ . $title . ‘
color:’ . $color . ‘
Message:’ . $message . ‘
Date:’ . $date . ‘
By:’ . $by . ‘.’;
}
[/php]

which outputs:

Thank you for posting this message.

[Go Back]

Title:Test Post
color:Blue
Message:This is a test post.
Date:01/20/2011
By:Liam Howe.

Still a problem, it’s not adding to the database.

[php]<?php

mysql_connect(“localhost”,“root”,“my password goes here”);
mysql_select_db(“Final Resurrection”) or die(“Could not connect to the database”);

if(isset($_POST[“button”]))
{
$tit = $_POST[“title”];
$col = $_POST[“color”];
$mess = $_POST[“message”];
$dat = date(“m/d/Y”);
$byhu = $_SESSION[“admin”];

$sql = ("SELECT * FROM Blog WHERE id = (SELECT MAX(id) FROM Blog)");
$result = mysql_query($sql);
while($last_id = mysql_fetch_array($result)) {
	$autoid = $last_id[id];
	$newid = $last_id[id] +1;
}
mysql_query("INSERT INTO 'Blog' (id, title, date, by, content, color)
	VALUES('$newid','$tit','$dat','$byhu','$mess','$col')");
echo "Thank you for making a post. <br /> <br />[<a href='index.php'>Go Back</a>]<br /><br />";

}
else
{
header(‘Location: index.php’);
}
?> [/php]

This is the current code.

Try changing the insert query to:

[php]mysql_query(“INSERT INTO Blog (id, title, date, by, content, color) VALUES(’”.$newid."’,’".$tit."’,’".$dat."’,’".$byhu."’,’".$mess."’,’".$col."’)");
[/php]

try this:
[php]

if(isset($_POST[“button”]))
{
$tit = $_POST[“title”];
$col = $_POST[“color”];
$mess = $_POST[“message”];
$dat = date(“m/d/Y”);
$byhu = $_SESSION[“admin”];

$sql = ("SELECT * FROM Blog WHERE id = (SELECT MAX(id) FROM Blog)");
$result = mysql_query($sql);
while($last_id = mysql_fetch_array($result)) {
	$autoid = $last_id[id];
	$newid = $last_id[id] +1;
}
    $sql = "INSERT INTO 'Blog' (id, title, date, by, content, color)
	VALUES('$newid','$tit','$dat','$byhu','$mess','$col')"
mysql_query($sql) or die("Error: " . mysql_error() . ".<br />SQL: " . $sql);
echo "Thank you for making a post. <br /> <br />[<a href='index.php'>Go Back</a>]<br /><br />";

}
else
{
header(‘Location: index.php’);
}
?>
[/php]

I put your sql query in a variable so we could output it (remove this in your production code, never output sql queries, even in errors, to anyone). I also put “or die()” after the sql query which displays both the error message and the sql query… This should help you debug.

Let me know how it goes.

[tt]Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘by, content, color) VALUES(‘2’,‘fdgjhkh’,‘02/23/2011’,’’,‘dsfghjghfhgjhghgf’,‘Re’ at line 1.
SQL: INSERT INTO Blog (id, title, date, by, content, color) VALUES(‘2’,‘fdgjhkh’,‘02/23/2011’,’’,‘dsfghjghfhgjhghgf’,‘Red’)[/tt]

I checked the mysql version and it said:

[tt]mysql Ver 14.12 Distrib 5.0.51a, for debian-linux-gnu (i486) using readline 5.2[/tt]

a quick look through your code i can see you have a semi-colon missing here:
[php]
$sql = “INSERT INTO ‘Blog’ (id, title, date, by, content, color)
VALUES(’$newid’,’$tit’,’$dat’,’$byhu’,’$mess’,’$col’)” //<-------- ; missing here!
[/php]

It still didn’t work :frowning: Showing the same thing.

Sponsor our Newsletter | Privacy Policy | Terms of Service