The online racing simulator
Unable to Insert data via a SQL Query Unless it's done manualy in PHPMyAdmin?
Sorry, I don't know where else to post this but I thought most of the smart guys hang around here so I have a better chance of getting good help, I started a Racing team site and I made a news system (very simple) that orders news by id and posts them etc.

So to make it easier for myself i made a Page that uses the PHP $_POST to get info for the db then run a query to insert it, This is the PHP code that I use:


<?php 
$query 
"'INSERT INTO `impulser_ProStreet`.`News` (`ID`, `Title`, `Text`, `Date`) VALUES (\'\', \'".$title."\', \'".$text."\', \'(TestDate)\');";
mysql_query($query) or die('Error, Insert query failed. :(');
?>

and I always get "Error, Insert query failed. "
#2 - Ian.H
Following code is untested, but is how I'd write it.

I've assumed your 'ID' field is an auto-incremental field, thus didn't need to supply any data for it.

$query = '
INSERT INTO impulser_ProStreet.News
(
Title,
Text,
Date
)
VALUES (
\'' . $title . '\',
\'' . $text . '\',
\'(TestDate)\'
)';

$ret = mysql_query($query);

if (!$ret) {
die(mysql_error());
}

mysql_error() should give you a more detailed error report if it goes tits up than a generic home-crafted msg

I suspect your original error may be due to you double-quoting the query, ie: you've used single quotes within double quotes around the whole query and in fact, looks like you've left the closing single quote out anyway thus creating a weird string rather than a proper SQL query.

I've also assumed you've performed the required input validation from the form to prevent parsing issues and SQL injection attacks.



Regards,

Ian
Hey Ian, Your Code worked Thanks Alot you have saved me from having to scrap 4 hours worth of work! Mucho Gracias!
Your problem in the original code was that you used the double quotes ("), and then you used signal quotes ('). You only needed the signal style.

Bad:

<?php 
$query 
"'INSERT INTO `impulser_ProStreet`.`News` (`ID`, `Title`, `Text`, `Date`) VALUES (\'\', \'".$title."\', \'".$text."\', \'(TestDate)\');";
?>

This is your issue:
= "'INSERT INTO

Good1:

<?php 
$query 
'INSERT INTO `impulser_ProStreet`.`News` (`ID`, `Title`, `Text`, `Date`) VALUES (\'\', \'".$title."\', \'".$text."\', \'(TestDate)\');'
?>

Good2:

<?php 
$query 
"INSERT INTO `impulser_ProStreet`.`News` (`ID`, `Title`, `Text`, `Date`) VALUES (NULL, '{$title}', '{$text}', '(TestDate)');"
?>


FGED GREDG RDFGDR GSFDG