Jump to content

[SOLVED] MySQL Syntax Problem


illusivefing

Recommended Posts

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!

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.