WesleyA Posted July 11, 2015 Share Posted July 11, 2015 I get an error code 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 I found this is error 1149 ? is that right? I'm looking for the meaning of this error. What is the reason I'm having this? Does it mean the mysq code in my php script is wrong? Is a solution changing the MYSQL line or can the error also be made in the php part of the script? Link to comment Share on other sites More sharing options...
dsonesuk Posted July 11, 2015 Share Posted July 11, 2015 You have a SQL syntax error, showing us your SQL would help. Link to comment Share on other sites More sharing options...
WesleyA Posted July 12, 2015 Author Share Posted July 12, 2015 (edited) I made query with a variable $sql = "INSERT INTO $tab (var1, var2) VALUES ('$var4', '$var3')" ; Is it true that if $tab doesnt have a value, the error code mentioned above is given? Edited July 12, 2015 by WesleyA Link to comment Share on other sites More sharing options...
dsonesuk Posted July 12, 2015 Share Posted July 12, 2015 Well yea, place the code for inserting values in table within a if condition that checks to make sure $tab is not empty. if(!empty($tab)){Insert into table code...} Link to comment Share on other sites More sharing options...
dauruk0512 Posted July 13, 2015 Share Posted July 13, 2015 Just asking Why you put table name inside variable ? if $tab doesnt have a value <--what this mean ? Link to comment Share on other sites More sharing options...
WesleyA Posted July 27, 2015 Author Share Posted July 27, 2015 Just asking Why you put table name inside variable ? if $tab doesnt have a value <--what this mean ? 1) I want to know it. It is for my script. The users are allowed to make a table 2) I asked that to find out if there could be an error report IF $tab was empty. Link to comment Share on other sites More sharing options...
WesleyA Posted July 27, 2015 Author Share Posted July 27, 2015 To read data from an input field yoiu use post (method post) and it can be used to put it on screen in the browser with ech or with var_dump. Using this variable again and using PHP to instruct MYSQL in this way $sql = "INSERT INTO $tab (var1, var2) VALUES ('$var4', '$var3')" ; doesnt seem to work. Is it necessary to use other PHP commands to put a variable like $tab into a MYSQl query? Link to comment Share on other sites More sharing options...
dsonesuk Posted July 27, 2015 Share Posted July 27, 2015 Try $sql = "INSERT INTO `$tab` (var1, var2) VALUES ('$var4', '$var3')"; //using backticks for table nameOR$sql = "INSERT INTO `".$tab."` (var1, var2) VALUES ('".$var4."', '".$var3."')"; //using backticks for table name Link to comment Share on other sites More sharing options...
WesleyA Posted July 29, 2015 Author Share Posted July 29, 2015 (edited) I think the mistake is that the $tab is empty when I embed in in a condition. I can check that with var_dump or echo. I was not 100% certain about that but it might be. So I asked this question what the errorcode could be if $tab would be empty in the php/mysql query I posted. Edited July 29, 2015 by WesleyA Link to comment Share on other sites More sharing options...
dsonesuk Posted July 29, 2015 Share Posted July 29, 2015 That is why you should check if any these variables are empty, because the SQL is expecting these, without any of these missing the sql syntax we be wrong, and a error is produced. basic setup, need code for sanitization to prevent sql injection $tab="";$var3="";$var4="";$tab=$_POST['tab'];$var3=$_POST['var3'];$var4=$_POST['var4'];if(!empty($tab) && !empty($var3) && !empty($var4)){$sql = "INSERT INTO `$tab` (var1, var2) VALUES ('$var4', '$var3')"; //using backticks for table name}else{echo "Error Not All the variables have a value"; } Link to comment Share on other sites More sharing options...
WesleyA Posted July 31, 2015 Author Share Posted July 31, 2015 Now I got an errormessage saying Undefined index: forminput $tab = $_POST['forminput'] when is this error given? Can I use it only once? (as i used it before wih succes) Link to comment Share on other sites More sharing options...
dsonesuk Posted July 31, 2015 Share Posted July 31, 2015 Ok try $tab=""; $var3=""; $var4=""; if(isset($_POST['tab'])){$tab=$_POST['tab']; }if(isset($_POST['var3'])){$var3=$_POST['var3']; }if(isset($_POST['var4'])){$var4=$_POST['var4']; }if(!empty($tab) && !empty($var3) && !empty($var4)) { $sql = "INSERT INTO `$tab` (var1, var2) VALUES ('$var4', '$var3')"; //using backticks for table name } else { echo "Error Not All the variables have a value"; }The problem was if 'forminput' is not set on submission, $_POST['forminput'] using index of 'forminput' would not exist and no index error is shown, it now checks if it exist and then assigns it to a variable, it then check if value is not empty as in "" before allowing to insert into table else it will show missing variable value error. Link to comment Share on other sites More sharing options...
WesleyA Posted August 17, 2015 Author Share Posted August 17, 2015 Ok try $tab=""; $var3=""; $var4=""; if(isset($_POST['tab'])){$tab=$_POST['tab']; }if(isset($_POST['var3'])){$var3=$_POST['var3']; }if(isset($_POST['var4'])){$var4=$_POST['var4']; }if(!empty($tab) && !empty($var3) && !empty($var4)) { $sql = "INSERT INTO `$tab` (var1, var2) VALUES ('$var4', '$var3')"; //using backticks for table name } else { echo "Error Not All the variables have a value"; }The problem was if 'forminput' is not set on submission, $_POST['forminput'] using index of 'forminput' would not exist and no index error is shown, it now checks if it exist and then assigns it to a variable, it then check if value is not empty as in "" before allowing to insert into table else it will show missing variable value error. Yeah But do I have to set it twice then? As I told before. The form input is another script. First I use this form input and make the user choose the table. Now I want the choice the user made first printed on the screen. Then this choice should be used in the SQL code. But on the new screen the user can also choose five values. Here it stucks, when I use an if condition. It is the same if condition as when I echo it on the screen. But embedded or wrapped around the existing if condition concerning SQL code for adding the values to the table seems to demand another if condition. I don't understand for what reason PHP works this way? Link to comment Share on other sites More sharing options...
Ingolme Posted August 17, 2015 Share Posted August 17, 2015 That's not PHP's fault. If you didn't actually give it any data to work with, it's not going to know what to do. Link to comment Share on other sites More sharing options...
WesleyA Posted August 17, 2015 Author Share Posted August 17, 2015 No! forget the word fault. It has to do with wanting to understand how PHP works. But back to the issue: how should I instruct PHP how to process the data that I have retrieved once for echoing, for the second time in an SQL command? Link to comment Share on other sites More sharing options...
justsomeguy Posted August 17, 2015 Share Posted August 17, 2015 PHP does exactly what you tell it to do. It does very little by itself, apart from basic things like automatically processing form data to allow you to access it in $_GET or $_POST. Everything else, you're telling it what to do.If you're talking about code on different pages then you have to write it twice. PHP code in one page is not connected with another page unless you do things yourself to move that data or state to the other page, like store data in the session. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now