Page 1 of 1

Another Newbie Syntax Error Question :p

Posted: Wed Feb 19, 2003 7:12 pm
by Caped Knight
Something I can NEVER seem to get to work:

Code: Select all

<?php

/* $_POSTs attached to web form */
$date = $_POST['Date']; 
$field = $_POST['Text'];

MySQL_connect("localhost", "[user]", "[password]") or die("Unable to connect to MySQL.&nbsp;&nbsp;" . mysql_error);
MySQL_select_db("[database]") or die("Unable to connect to database.");

$data = "INSERT INTO news1 (Date, Field)
VALUES ('$date', '$field')";

$send = mysql_query($data);

if($send){
    echo("Data submitted successfully.");
}else{
    echo("An error occurred while submitting data.");
}

?>
I just don't know what I'm doing wrong. It's not a problem connecting to the database or anything. The database and table exist. I know I didn't make any mistakes in the form. I'm guessing it's a problem in the query variable, but I've tried a very broad range of syntax and it still never works. Anyone have a clue? Thanks.

Posted: Wed Feb 19, 2003 8:49 pm
by jason
after the mysql_query() call, add this line:

Code: Select all

<?php
echo mysql_error(), "<hr>\n", $data;
?>
This will tell you if an error exists as well as let you see the actual SQL query being sent to mysql.

Posted: Wed Feb 19, 2003 9:43 pm
by hob_goblin
i saw that

Code: Select all

MySQL_connect("localhost", "&#1111;user]", "&#1111;password]") or die("Unable to connect to MySQL.  " . mysql_error);

should be

Code: Select all

mysql_connect("localhost", "&#1111;user]", "&#1111;password]") or die("Unable to connect to MySQL.  " . mysql_error());

and

Code: Select all

MySQL_select_db("&#1111;database]") or die("Unable to connect to database.");
should be

Code: Select all

mysql_select_db("&#1111;database]") or die("Unable to connect to database.");
and try changing

Code: Select all

$data = "INSERT INTO news1 (Date, Field) 
VALUES ('$date', '$field')";

to

Code: Select all

$data = "INSERT INTO news1 (`Date`, `Field`) 
VALUES ('$date', '$field')";

Posted: Wed Feb 19, 2003 9:46 pm
by jason
Ooops, I didn't even see the or die() part. Honestly it's not my most favorite format (too much on one line), but you did catch the missing '()'.

Posted: Wed Feb 19, 2003 10:18 pm
by Caped Knight
Thanks, guys. I got it to work. :)

Question: does the «`» work the same as «'» in (`Date`, `Field`)? Or does that matter?

Posted: Wed Feb 19, 2003 10:27 pm
by hob_goblin
Caped Knight wrote:Thanks, guys. I got it to work. :)

Question: does the «`» work the same as «'» in (`Date`, `Field`)? Or does that matter?
you should use `