Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HELP! Problem with updating to a database using PHP...

Status
Not open for further replies.

MadCatter101

Programmer
Nov 16, 2002
7
US
Hey all. I'm trying to write the code for a web site that updates users short stories etc. onto a database. The only page that I have even vaguely working right now is the "submit" page. After finally debugging the code (or so I thought), I still keep receiving the following error message:

1064: You have an error in your SQL syntax near ' ‘, ‘, '16.11.2002 10:16', ‘)' at line 2

Now, Line 2 of the code for the page (submit.php) is merely my <head> tag. My question: What am I doing wrong here? The relevant lines of my php code looks like such: (the data here is requested from a form, except for &quot;date&quot;, which is requested using: $time = time();
$date = strftime(&quot;%d.%m.%Y %H:%M&quot;); )


if ($enter_data) {

// build query string
$insert = &quot;INSERT INTO stories (authorname, title, authoremail, date, story)
VALUES(‘$authorname’, ‘$title’, ‘$email’, '$date', ‘$story’)&quot;;

// execute a query and store the result
$res = mysql_query($insert,$dbh);

// check to make sure the query actually ran
if (!$res) {
echo mysql_errno().&quot;: &quot;.mysql_error ().&quot;&quot;;
return 0;
}

I've noticed that in the error message, theres a &quot;' ,&quot; for every entry there--and the third blank area is, not surprisingly, the timestamped date. What am I doing wrong here? Thanks in advance for reading this and I hope you guys can help me. --MadCatter101--
 
Post the whoe code, its easier that way :) ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Ohhh boy... ok, here we go....


<html>
<head><title>DATABASE TESTS</title>
</head>

<body>

<form name=”user_info” method=”post” action=&quot;<? $PHP_SELF ?>&quot;>

Author: <input type=”text” name=”authorname”>
&nbsp;<br>
Title: <input type=”text” name=”title”>
&nbsp;<br>
Email address: <input type=”text” name=&quot;authoremail&quot;>
&nbsp;<p>
<?
$date = strftime(&quot;%Y.%m.%d&quot;);
?>
Story:<br><textarea rows=&quot;10&quot; name=&quot;S1&quot; cols=&quot;50&quot; name=&quot;story&quot;></textarea>&nbsp;<br>
<input type=&quot;submit&quot; value=&quot;Submit&quot; name=&quot;enter_data&quot;>
</form>

<?
// set a variable with the database name
$database_name = &quot;aimfreak_writersright&quot;;

// attempt to connect to database and store result
$dbh=mysql_connect (&quot;localhost&quot;, &quot;****&quot;, &quot;****&quot;) or die ('I cannot connect to the
database.');
mysql_select_db (&quot;aimfreak_writersright&quot;);

// check the result of the connection attempt
if (!mysql_select_db($database_name)) {
echo &quot;Can't Select $database_name&quot;;
}

// Conditional test to see if submit button was pressed
if ($enter_data) {

// build query string
$insert = &quot;INSERT INTO stories (authorname, title, authoremail, date, story)
VALUES(‘$authorname’, ‘$title’, ‘$email’, '$date', ‘$story’)&quot;;

// execute a query and store the result
$res = mysql_query($insert,$dbh);

// check to make sure the query actually ran
if (!$res) {
echo mysql_errno().&quot;: &quot;.mysql_error ().&quot;&quot;;
return 0;
}

echo '<p>Record successfully added' ;
}
?>

</body>
</html>
 
....People on other forums have suggested that the DATE field in the MySQL DB should be of types: &quot;date&quot;, &quot;text&quot;, &quot;varchar&quot;, &quot;timestamp&quot;... I've tried them all, and none of them work. The other thing about this that I'm wondering is, I have a uid field set to be a unique varchar(8) field. Should I try to manually input data to this field, or will it do it on it's own?

Thanks for the help,

MadCatter101
 
I see a couple of problems just from this error.

It looks like you're using back-ticks in your queries where they do not belong. Back-ticks (on the same key as the tilde on a U.S. keyboard) are used to escape words in MySQL queries that use keywords as column names. Apostrophes (or single quotes) are used to delineate strings.


Also, MySQL expects dates in the form &quot;YYYY-MM-DD hh:mm:ss&quot;. You're giving it &quot;DD.MM.YYYY&quot;. Although MySQL is not picky about the separator used in a date, it is about the order in which you insert the date data.


As to your question about unique fields. The only field MySQL will fill in automatically is and int field that is auto_increment. All others must be input in the query.
______________________________________________________________________
TANSTAAFL!
 
THANK YOU!!! Turns out the problem was with the ` items, NOT with the actual wording of the code itself! Thank you so very much; this has been driving me bonkers for about 24 hours now. THANK YOU!!! --MadCatter101--
 
Test your date insertion by both inserting and retrieving a date. MySQL will sometimes act like it's storing a date correctly, but it's just mis-interpreating what is given it. When you go to make the retrieval, you do not get back the data you entered. ______________________________________________________________________
TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top