INSERT into simple problem

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Patriot
Forum Commoner
Posts: 34
Joined: Tue Jun 18, 2002 1:36 pm

INSERT into simple problem

Post by Patriot »

Code: Select all

<?
$ctitle = $_POST['subject'];
$cbody = $_POST['comment'];
$com_id = $_GET['cid'];
$cdate = date("D, M j");
$cauthor = $_GET['cauthor'];

include "config.php";
$connection=mysql_connect($location, $dbuname, $dbpass) or die("Unable to establish a connection to $location");
mysql_select_db($db, $connection) or die("Unable to select the database ");
$query="INSERT into articles_comments(ctitle, cauthor, cdate, cbody, com_id) values ($ctitle, $cauthor, $cdate, $cbody, $com_id)";
mysql_query($query,$connection) or die("Unable to run query: $query.");
mysql_close($connection);
?>
this gives me an error :( all i need to do is insert variables into the table acordingly...

i made sure all the variables contain some information, and dont need any help with that, just how to use the basic INSERT command with variables, which right now seems VERY hard :(

I get this error every time, even when i take out each variable one at a time to decifer which one is messing the result up:

Code: Select all

Unable to run query: INSERT into articles_comments(ctitle, cauthor, cdate, cbody, com_id) values (sub, theAUTHOR, Tue, May 13, com, 3).
may it be the commas in the date that is messing this up? if so, how do i fix it?

sorry if this is posted in the wrong place or has already been answered...i'm so frustrated and guess i'm not thinking straight.

Thanks in advance!
-Mike
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

string literals have to be quoted for mysql.

Code: Select all

INSERT INTO tablename (aNumericalField, aStringField) VALUES (1, 'a'), (2, 'b'), (3, 'c')
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

Or
INSERT into articles_comments(ctitle, cauthor, cdate, cbody, com_id) values ('sub','theAUTHOR','Tue', 'May 13', 'com', '3').
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

[]InTeR[] wrote:Or
INSERT into articles_comments(ctitle, cauthor, cdate, cbody, com_id) values ('sub','theAUTHOR','Tue', 'May 13', 'com', '3').
Is there something wrong with trying to allow people to work it out for themselves :roll: (plus of course numerical strings do not need to be quoted).

Mac
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

Mostly if they do, it won't work. :D

No seriues, it's better to find things out at your own then let other write the code for you. Never copy and paste code from other's, without study'ing it first.
Patriot
Forum Commoner
Posts: 34
Joined: Tue Jun 18, 2002 1:36 pm

thx

Post by Patriot »

thanks guys, this forum is really helpful

inter and twigletmac,
i 100% get you when you say its a mistake to just copy and not learn, but it does save a lot of hassle of forgeting to put 's and everything...so i thank you both, inter for saving me the hassle of frustrating errors, and twigletmac for teaching me to learn, not copy :)

AWESOME FORUM GUYS!!!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

..and gals ;)
Post Reply