failchicken Posted August 22, 2013 Share Posted August 22, 2013 <form action="cat.php" method="post">Cat name: <input type="text" name="cat" /><input type="submit" name="submit"></form><?phpif (isset($_POST['submit'])){$con = mysql_connect("localhost", "root", "pass");if (!$con){die("can not connect: " . mysql_error()); }mysql_select_db("database",$con);$sql = "INSERT INTO gallery_category (category_id,category_name) VALUES (''',$_POST[cat]')";mysql_query($sql,$con);mysql_close($con);}?> Quote Link to comment https://forums.phpfreaks.com/topic/281478-why-isnt-this-working/ Share on other sites More sharing options...
DavidAM Posted August 22, 2013 Share Posted August 22, 2013 The query is invalid: if (! mysql_query($sql, $con)) die('Query Failed: ' . mysql_error($con) . ' -- ' . $sql); to find out why Quote Link to comment https://forums.phpfreaks.com/topic/281478-why-isnt-this-working/#findComment-1446360 Share on other sites More sharing options...
failchicken Posted August 22, 2013 Author Share Posted August 22, 2013 Query Failed: 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 -- INSERT INTO gallery_category (category_id, category_name) VALUES ('', ) Quote Link to comment https://forums.phpfreaks.com/topic/281478-why-isnt-this-working/#findComment-1446369 Share on other sites More sharing options...
AbraCadaver Posted August 22, 2013 Share Posted August 22, 2013 VALUES ('', ) Quote Link to comment https://forums.phpfreaks.com/topic/281478-why-isnt-this-working/#findComment-1446370 Share on other sites More sharing options...
fastsol Posted August 22, 2013 Share Posted August 22, 2013 You need to give the submit button a name="submit" attribute. A big issue is that you are only connecting to the db if the $_POST['submit'] isset which it isn't cause you haven't given it a name, hence why the query fails too. Quote Link to comment https://forums.phpfreaks.com/topic/281478-why-isnt-this-working/#findComment-1446388 Share on other sites More sharing options...
DavidAM Posted August 22, 2013 Share Posted August 22, 2013 @fatsol What code are you reading? The submit button does have a name, it is "submit". The problem is the single quotes inside the query: @failchicken $sql = "INSERT INTO gallery_category (category_id,category_name) VALUES (''',$_POST[cat]')"; If you look close you see three quotes before the comma and no quote after the comma before the category value. Of course, the debug message is showing nothing after the comma and only two single quotes before it, so somthing was changed before that run. Quote Link to comment https://forums.phpfreaks.com/topic/281478-why-isnt-this-working/#findComment-1446394 Share on other sites More sharing options...
fastsol Posted August 23, 2013 Share Posted August 23, 2013 My bad, i swear i read it as value instead of name, no other comments from me at this point. Quote Link to comment https://forums.phpfreaks.com/topic/281478-why-isnt-this-working/#findComment-1446415 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.