arunpatal Posted February 11, 2014 Share Posted February 11, 2014 I am getting error while inserting data... This is the error 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 'VALUES('','aaa','','','','2','2014-02-11')' at line 1 and here is code function add_page(){ global $content; if (isset ($_POST["title"])){ $title = mysql_real_escape_string($_POST["title"]); $heading = mysql_real_escape_string($_POST["heading"]); $content = mysql_real_escape_string($_POST["content"]); $date = date("Y-m-d"); $id = htmlspecialchars($_GET["cat_id"]); $target = "images/"; $target = $target . basename( $_FILES['page_img']['name']); move_uploaded_file($_FILES['page_img']['tmp_name'], $target); //$pic variable is the name of image which can be save into database $pic =($_FILES['page_img']['name']); mysql_query("INSERT INTO $content VALUES('','$title','$heading','$content','$pic','$id','$date')") or die (mysql_error()); $msg = "<style>#msg{display:block;} #form{display:none;}</style>"; $msg .= "<div id='msg'>Page $title has been added</div>"; return $msg; } }; Quote Link to comment https://forums.phpfreaks.com/topic/286108-insert-data-to-mysql/ Share on other sites More sharing options...
Solution mac_gyver Posted February 11, 2014 Solution Share Posted February 11, 2014 you have two different $content variables. the one from the $_POST variable, which is empty, is overwriting the one holding the table name, so there is an empty table name in the query. Quote Link to comment https://forums.phpfreaks.com/topic/286108-insert-data-to-mysql/#findComment-1468476 Share on other sites More sharing options...
arunpatal Posted February 11, 2014 Author Share Posted February 11, 2014 you have two different $content variables. the one from the $_POST variable, which is empty, is overwriting the one holding the table name, so there is an empty table name in the query. Yes... That was the problem... Thanks Quote Link to comment https://forums.phpfreaks.com/topic/286108-insert-data-to-mysql/#findComment-1468479 Share on other sites More sharing options...
jazzman1 Posted February 11, 2014 Share Posted February 11, 2014 A few recommendations when you use again an INSERT statement in the future! It is always a good practice to name all columns into which you are inserting values because: 1) The INSERT statement is much more descriptive 2) You can verify that you are providing the values in the proper order based on the column names 3) You have better data independence. The order in which the columns are defined in the table does not affect (no waste memory) your INSERT statement. So, I recommend to start with examples providing by MySQL into its source page. Quote Link to comment https://forums.phpfreaks.com/topic/286108-insert-data-to-mysql/#findComment-1468484 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.