Page 1 of 1
INSERT into simple problem
Posted: Tue May 13, 2003 3:50 pm
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
?>
Posted: Tue May 13, 2003 6:27 pm
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')
Posted: Wed May 14, 2003 2:10 am
by []InTeR[]
Or
INSERT into articles_comments(ctitle, cauthor, cdate, cbody, com_id) values ('sub','theAUTHOR','Tue', 'May 13', 'com', '3').
Posted: Wed May 14, 2003 2:54 am
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

(plus of course numerical strings do not need to be quoted).
Mac
Posted: Wed May 14, 2003 3:00 am
by []InTeR[]
Mostly if they do, it won't work.
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.
thx
Posted: Wed May 14, 2003 3:02 pm
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!!!
Posted: Thu May 15, 2003 10:28 am
by volka
..and gals
