illusivefing Posted November 23, 2007 Share Posted November 23, 2007 trying to get a website up and running, in the process of learning php and mysql for a few months now. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ';, 'asdf')' at line 1 ^ this is the message that i get when trying to run this script: <?php require_once("database.php"); //check for required fields from the form if ((!$_POST["topic_owner"]) || (!$_POST["topic_title"]) || (!$_POST["post_text"])) { header("Location: forum.html"); exit; } $mysqli = mysqli_connect($host, $user, $pass, $database); //create and issue the first query $add_topic = "INSERT INTO forum_topics (topic_title, topic_create_time, topic_owner) VALUES ('".$_POST['topic_title']."', now(),'".$_POST['topic_owner']."')"; $add_topic_res = mysqli_query($mysqli, $add_topic) or die(mysqli_error($mysqli)); //get id of last query $topic_id = mysqli_insert_id($mysqli); //create and issue the second query $add_post_sql = "INSERT INTO forum_posts (topic_id, post_text, post_create_time, post_owner) VALUES ('".$topic_id."', '".$_POST["post_text"]."', now();, '".$_POST["topic_owner"]."')"; $add_post_res = mysqli_query($mysqli, $add_post_sql) or die (mysqli_error($mysqli)); //close db connection $mysqli_close($mysqli); $display_block = "<p>The <strong>".$_POST["topic_title"]."</strong> topic has been created.</p>"; ?> <html> <head> <title>New Topic Added</title> </head> <body> <h1>New Topic Added</h1> <?php echo $display_block; ?> </body> </html> the input of it is run from this html form: <html> <head> <title>Add a Topic</title> </head> <body> <h1>Add a Topic</h1> <form method="post" action="do_addtopic.php"> <p><strong>Your E-Mail Address:</strong><br/> <input type="text"" name="topic_owner" size="40" maxlength="150"/></p> <p><strong>Topic Title:</strong><br/> <input type="text"" name="topic_title" size="40" maxlength="150"/></p> <textarea name="post_text" rows="8" cols="40" wrap="virtual"></textarea></p> <p><input type="submit" name="submit" value="Add Topic"/></p> </form> </body> </html> and here is the sql for the topic: CREATE TABLE forum_topics ( topic_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, topic_title VARCHAR(150), topic_create_time DATETIME, topic_owner VARCHAR (150) ); i'm running php5 on a 1and1 linux shared server, i've tried this with both mysql 4.0 and 5.0 databases, both of which produced the same error message. i copied the majority of this code straight out of one of the books i'm using to learn, "sam's teach yourself mysql, php, and apache all-in-one." any clues as to what might be going on? maybe even a second set of eyes will catch some stupid little mistake i made. all help appreciated, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/78491-solved-mysql-syntax-problem/ Share on other sites More sharing options...
revraz Posted November 23, 2007 Share Posted November 23, 2007 Post the code to database.php also, the error is probably coming from there. Quote Link to comment https://forums.phpfreaks.com/topic/78491-solved-mysql-syntax-problem/#findComment-397193 Share on other sites More sharing options...
illusivefing Posted November 23, 2007 Author Share Posted November 23, 2007 <?php $host = "db999.perfora.net"; $user = "*************; $pass = "*************"; $database = "db208165055"; ?> is the code to database.php. just contains information that i wouldn't like to repeat every time i wanted to connect to the same database using a different script. by the way, asdf was the input for "topic_title" in the original form Quote Link to comment https://forums.phpfreaks.com/topic/78491-solved-mysql-syntax-problem/#findComment-397201 Share on other sites More sharing options...
revraz Posted November 23, 2007 Share Posted November 23, 2007 edit out the actual name/pw in your post. Quote Link to comment https://forums.phpfreaks.com/topic/78491-solved-mysql-syntax-problem/#findComment-397207 Share on other sites More sharing options...
Aureole Posted November 23, 2007 Share Posted November 23, 2007 He left his password there and went offline? That's hilarious... a moderator should probably edit that for him... Quote Link to comment https://forums.phpfreaks.com/topic/78491-solved-mysql-syntax-problem/#findComment-397241 Share on other sites More sharing options...
illusivefing Posted November 23, 2007 Author Share Posted November 23, 2007 yes, i've already changed the password. though, i can't seem to find how to edit my post. maybe an administrator could change that information just in case of a curious mind? anybody have any idea about how to fix the problem in my script? Quote Link to comment https://forums.phpfreaks.com/topic/78491-solved-mysql-syntax-problem/#findComment-397255 Share on other sites More sharing options...
PFMaBiSmAd Posted November 23, 2007 Share Posted November 23, 2007 Remove the ; after the now() in the following line of code - $add_post_sql = "INSERT INTO forum_posts (topic_id, post_text, post_create_time, post_owner) VALUES ('".$topic_id."', '".$_POST["post_text"]."', now();, '".$_POST["topic_owner"]."')"; Quote Link to comment https://forums.phpfreaks.com/topic/78491-solved-mysql-syntax-problem/#findComment-397259 Share on other sites More sharing options...
illusivefing Posted November 23, 2007 Author Share Posted November 23, 2007 ah. that totally worked. guess i was used to terminating with a semicolon immediately after funtions. thanks! Quote Link to comment https://forums.phpfreaks.com/topic/78491-solved-mysql-syntax-problem/#findComment-397265 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.