• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Interfacing PHP with MySQL

I'm writing a backend interface for a website that will allow the user to update news articles and such via an HTTP form. I'm using the latest version of PHP with the latest version of MySQL. My problem is this... how do I insert PHP variables into a DB?

include('./include.php');

$newsdb = mysql_connect("localhost", $dbuser, $dbpass);
mysql_select_db($newsname, $newsdb);
mysql_query("insert INTO events (title, summary, delist, date, displaydate) VALUES ('$_POST[title]', '$_POST[summary]', '$_POST[delist]', '$_POST[date]', '$_POST[ddate]'");
echo $title;

This generates no errors, and is based on some source code I found while searching around google. But either way it doesn't work. I even echo'd the variables to the screen to make sure they weren't null, and sure enough they contained the correct strings. Any thoughts on how to get it to work?
 
I've always done it like this:

mysql_query("insert INTO events (title, summary, delist, date, displaydate) VALUES ('$title', '$summary', '$delist', '$date', '$ddate')");

You seem to be missing a close parenthesis.
 
Did you check for errors after the query?
I don't remember the syntax, but you need to check the return value. And don't you have to pass $newsdb as one of the arguments to mysql_query()?

In any case, I've switched to the PEAR interface, instead of the MySQL specific interface. Seems cleaner for most stuff (except auto_increment).
 
Well, problem solved. Thanks for your help, it may have been a mixture of things, but I think it was because I was short one ')', dunno why it didnt complain. Silly me, oh well thanks for your input.
 
Originally posted by: irl33thax0r
I'm writing a backend interface for a website that will allow the user to update news articles and such via an HTTP form. I'm using the latest version of PHP with the latest version of MySQL. My problem is this... how do I insert PHP variables into a DB?

include('./include.php');

$newsdb = mysql_connect("localhost", $dbuser, $dbpass);
mysql_select_db($newsname, $newsdb);
mysql_query("insert INTO events (title, summary, delist, date, displaydate) VALUES ('$_POST[title]', '$_POST[summary]', '$_POST[delist]', '$_POST[date]', '$_POST[ddate]'");
echo $title;

This generates no errors, and is based on some source code I found while searching around google. But either way it doesn't work. I even echo'd the variables to the screen to make sure they weren't null, and sure enough they contained the correct strings. Any thoughts on how to get it to work?

I realize you have fixed it, but I'll tell ya what was wrong in the original just in case 😉

Associated array keys need to have quotes around them. I believe back in php3, they did not, but they certainly do now. $_POST['title'], etc. To put funky variables like that directly into strings, it is usually best to put them in curly braces. So something like this would be ideal:

mysql_query("insert INTO events (title, summary, delist, date, displaydate) VALUES ('{$_POST['title']}', '{$_POST['summary']}', '{$_POST['delist']}', '{$_POST['date']}', '{$_POST['ddate']}'");
 
Back
Top