Jump to content

Incorrect syntax, and a side order of $_POST being weird


Asday

Recommended Posts

MySQL version: 5.0.51b-community-nt

Error given:  "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 ')' at line 1"

 

At the moment, I've been wrestling with this code for quite a while.  I want it to show a simple form when $_POST isn't set (and for some reason, it's always set, so I've used $_POST[date]) and when it is set, write $_POST[date] and $_POST[content] to the relevant places in the sql table.  For some reason, it's failing now, and I'm well and truly stuck.

 

<?php
$html = "<html><head></head><body><p><form action=\"newspost.php\" method=\"post\">Datemarker: <input type=\"text\" name=\"date\" />Content: <input type=\"text\" name=\"content\" /><input type=\"submit\" /></form></p></body></html>";
if($_POST[date] == "")
{
echo $html;
}

if(isset($_POST))
{
echo $_POST[date];
$con = mysql_connect("localhost","user","pass");
mysql_select_db("sg194", $con);
$date194 = $_POST[date];
$content194 = $_POST[content];
$query = "INSERT INTO frontpage_news (date,content) VALUES (".$date194.",".$content194.")";
mysql_query($sql, $con);
mysql_close($con);
}
?>

 

Apologies if this is in the wrong section.

Link to comment
Share on other sites

$query = "INSERT INTO frontpage_news (date,content) VALUES ('".$date194."','".$content194."')";

or I think even this would work:

$query = "INSERT INTO frontpage_news (date,content) VALUES ('$date194','$content194')";

 

 

I added single quotes to the VALUES.

 

As for $_POST, ...

if(isset($_POST['date'])
{
  //sql stuff
}else{
  //show form
}

Link to comment
Share on other sites

The keys of an array should be wrapped in quotes, otherwise PHP thinks it is a constant. Of course, if it cannot find a defined constant by that name it assumes it as a variable. None the less, this:

 

$_POST[date]

 

Should be:

 

$_POST['date']

 

And so on...

 

Now try changing your query to this:

$query = "INSERT INTO frontpage_news (date,content) VALUES ('".$date194."','".$content194."')";

 

Also! I assume you have a submit button, yes. Well, use that in the if() statement at the start:

 

if(isset($_POST['submit_button_name']))

Link to comment
Share on other sites

I thought that $_POST[date] needed to be $_POST["date"], but things weren't working, so I changed it, and things stayed the same (and when asked to echo $_POST[date] after I'd set it, it echoed the right thing.)

 

Anyway, now I'm left with a blank screen.  It appears to be attempting to execute the second block of code.

Link to comment
Share on other sites

Assuming you named this file "newspost", try this:

<?php
if(!isset($_POST['addNews']))
{
?>
<html>
<head></head>
<body>
<p><form action="newspost.php" method="post">
Datemarker: <input type="text" name="date" />
Content: <input type="text" name="content" />
<input type="submit" name="addNews" value="Submit" />
</form></p>
</body>
</html>
<?php
}else{//form was submitted
   $con = mysql_connect("localhost","user","pass");
   mysql_select_db("sg194", $con);
   $date194 = $_POST['date'];
   $content194 = $_POST['content'];
   $query = "INSERT INTO frontpage_news (date,content) VALUES ('$date194','$content194')";
   $result = mysql_query($sql, $con);
   mysql_close($con);
   
   if($result){
    echo "Inserted date: $date194, $content194 into the database.";
   }else{
    echo "Query failed: ".mysql_error();
   }
}
?>

Link to comment
Share on other sites

Well, it may be because you're using a reserved word (date) for your column.  could try making the query string like this:

$query = "INSERT INTO frontpage_news (`date`,`content`) VALUES ('$date194','$content194')";

I added backticks to the columns

 

Another thing is, it appears your columns are set to type blob.. I don't know if you did that on purpose, but I would change them to: datetime for date(use a different column name as well), and text for content.. [though it depends what you're storing]

 

It also appears you currently have two rows of blank data as far as I can tell.

Link to comment
Share on other sites

(Sorry for the doublepost, the edit button's gone AWOL.)

 

Ok, I got it to submit the data now, but it's still appearing as blobs in the database.  I now have one BLOB - 8B as the date, and one BLOB - 6B as the content.  What's going on, and how do I fix it?

Link to comment
Share on other sites

Sorry, I have no experience with BLOB data types.  I assume it would be the same as pulling any other data from mysql..

<?php
$sql = "SELECT columnname FROM table WHERE 1";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
  echo $row['columnname'];
}
?>

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.