Asday Posted November 10, 2008 Share Posted November 10, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/132074-incorrect-syntax-and-a-side-order-of-_post-being-weird/ Share on other sites More sharing options...
xtopolis Posted November 10, 2008 Share Posted November 10, 2008 $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 } Quote Link to comment https://forums.phpfreaks.com/topic/132074-incorrect-syntax-and-a-side-order-of-_post-being-weird/#findComment-686350 Share on other sites More sharing options...
JasonLewis Posted November 10, 2008 Share Posted November 10, 2008 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'])) Quote Link to comment https://forums.phpfreaks.com/topic/132074-incorrect-syntax-and-a-side-order-of-_post-being-weird/#findComment-686351 Share on other sites More sharing options...
Asday Posted November 10, 2008 Author Share Posted November 10, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/132074-incorrect-syntax-and-a-side-order-of-_post-being-weird/#findComment-686395 Share on other sites More sharing options...
xtopolis Posted November 10, 2008 Share Posted November 10, 2008 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(); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/132074-incorrect-syntax-and-a-side-order-of-_post-being-weird/#findComment-686436 Share on other sites More sharing options...
Asday Posted November 10, 2008 Author Share Posted November 10, 2008 That's fairly weird. Now I get "Query failed: " and nothing else. The table now has this in it. No idea what it means, though. Quote Link to comment https://forums.phpfreaks.com/topic/132074-incorrect-syntax-and-a-side-order-of-_post-being-weird/#findComment-686493 Share on other sites More sharing options...
xtopolis Posted November 10, 2008 Share Posted November 10, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/132074-incorrect-syntax-and-a-side-order-of-_post-being-weird/#findComment-686519 Share on other sites More sharing options...
Asday Posted November 10, 2008 Author Share Posted November 10, 2008 I set them to "text" and "longtext" under the pretence I'd be having multiple entries in a single day. I got rid of the two blank rows, and modified the PHP to your suggestions, same result, only no change in the table this time. Quote Link to comment https://forums.phpfreaks.com/topic/132074-incorrect-syntax-and-a-side-order-of-_post-being-weird/#findComment-686522 Share on other sites More sharing options...
Asday Posted November 10, 2008 Author Share Posted November 10, 2008 (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? Quote Link to comment https://forums.phpfreaks.com/topic/132074-incorrect-syntax-and-a-side-order-of-_post-being-weird/#findComment-686571 Share on other sites More sharing options...
xtopolis Posted November 10, 2008 Share Posted November 10, 2008 I don't know.. try remaking the table. Also if one color is going to be a date, there is no reason you shouldn't use a date or datetime type for it. Quote Link to comment https://forums.phpfreaks.com/topic/132074-incorrect-syntax-and-a-side-order-of-_post-being-weird/#findComment-686676 Share on other sites More sharing options...
Asday Posted November 10, 2008 Author Share Posted November 10, 2008 Because I'll be just pulling the raw text straight from the table, and echoing it, so if I want to output "03/11/91 - 2 (Hellsyeah, awesomeness.)" I can. Quote Link to comment https://forums.phpfreaks.com/topic/132074-incorrect-syntax-and-a-side-order-of-_post-being-weird/#findComment-686738 Share on other sites More sharing options...
xtopolis Posted November 10, 2008 Share Posted November 10, 2008 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']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/132074-incorrect-syntax-and-a-side-order-of-_post-being-weird/#findComment-686819 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.