WesleyA Posted July 1, 2015 Share Posted July 1, 2015 which query should be used when you have this INSERT INTO `$variable` (col1, col2) VALUES ..... The question is regarding $variable. is it " ", ' ' ,` ` . ? Link to comment Share on other sites More sharing options...
Ingolme Posted July 1, 2015 Share Posted July 1, 2015 Backticks are used around table identifiers and field identifiers in the query. Single or double quotes are used around string values. Link to comment Share on other sites More sharing options...
WesleyA Posted July 1, 2015 Author Share Posted July 1, 2015 Backticks are used around table identifiers and field identifiers in the query. Single or double quotes are used around string values. OK but in this case the table is a variable. Link to comment Share on other sites More sharing options...
Ingolme Posted July 1, 2015 Share Posted July 1, 2015 It's still a table identifier, not the value of a field. MySQL doesn't know it's a variable, only PHP does. Link to comment Share on other sites More sharing options...
WesleyA Posted July 1, 2015 Author Share Posted July 1, 2015 This code doesnt work for instance $sql = "INSERT INTO " . $col . " (vara, varb) VALUES (`$varc`, `$vard`)" ; Link to comment Share on other sites More sharing options...
justsomeguy Posted July 1, 2015 Share Posted July 1, 2015 Correct, that code will not work, at least not if those 2 variables don't contain valid column identifiers.If you want to see what MySQL sees, then print out the query in PHP. There's a difference between the PHP code to build the query and the actual query that goes to MySQL. Print the query if you want to see what you're sending to MySQL. Link to comment Share on other sites More sharing options...
Ingolme Posted July 1, 2015 Share Posted July 1, 2015 These are VALUES, as the query indicates: VALUES (`$varc`, `$vard`) String values must be wrapped in single quotes or double quotes, not backticks. Link to comment Share on other sites More sharing options...
WesleyA Posted July 13, 2015 Author Share Posted July 13, 2015 The scripts I wrote have 2 form fields. It are 2 different php files. The first form fields gets data from the database and lets the user choose from a checkbox form. The choice is transferred to the second script. I'm able to print it there on screen. Now in the second file a new form file is offered. After that I have a second form field to put data in the database with mysql. Here I use if ($_SERVER['REQUEST_METHOD'] == 'POST') and then I check with if isset all the values. ( I actually dont want to echo the checkbox choice on screen at all, I want this choice in an mysql query). So I have for this one request method 'Post' actually data from two different form fields. Is it possible to do that with PHP? Or should another script language be used? Because I F# dont understand why an if isset works in the second file to grab the data from the first checkbox form field, but not a second time in exactly the same way. Why sould I use another if /isset while its the same? Link to comment Share on other sites More sharing options...
justsomeguy Posted July 13, 2015 Share Posted July 13, 2015 If you're talking about sending the data from one form through on another form, you can use hidden form inputs to write out the data from the previous form inside the new form. You can also save that data in the session then access it later. Link to comment Share on other sites More sharing options...
WesleyA Posted July 13, 2015 Author Share Posted July 13, 2015 If you're talking about sending the data from one form through on another form, you can use hidden form inputs to write out the data from the previous form inside the new form. You can also save that data in the session then access it later. No I'm not sending data from one form in another form. I have two different form fields The first is to determine the table variable (for a mysql query). The second separate file is reading the values (also for the same mysql query) in a loop. But my problem is that I can not use the same if/!isset condition twice. The first works, the second with the same code doesnt work. Link to comment Share on other sites More sharing options...
justsomeguy Posted July 13, 2015 Share Posted July 13, 2015 You're going to need to show code. I don't know what you mean by a form field that reads values in a loop, form fields don't do that. Just show the code you're using. Link to comment Share on other sites More sharing options...
WesleyA Posted July 14, 2015 Author Share Posted July 14, 2015 (edited) Maybe at the moment the form fields are distracting from the core of our problem. The problem is that I cant get this part fixed. $tab = $_POST['kolom2']; var_dump($tab); // dump gives null both isset as !isset. But maybe I'm thinking wrong. If I get the data from the form field which I am able to put on screen in an earlier part of the script then have set them or not? Or are these data only available once? I would like to know how to use data from any form inputs throughout the whole script (or even other php files). My problem is that if I use isset the variable is not set so the code after the { is not executed. So I use !isset, then it is executed, but then unfortuantely the variable is not set. here is a greater part of the script: If (isset($_POST['linkoms']) && (is_array($_POST['linkoms'])) && ($linkoms = " ") && isset($_POST['linkadd']) && (is_array($_POST['linkadd'])) && ($linkadd = " ") ) { echo "test"; // is echoed if ($_SERVER['REQUEST_METHOD'] == 'POST') { echo "test"; // is echoed if (!isset($_POST['kolom2']) && !isset($tab) ) { echo "test"; // not echoed at isset, is echoed at !isset $tab = $_POST['kolom2']; var_dump($tab); // dump gives null both isset as !isset. foreach($_POST['linkoms'] as $key => $dummy) { $oms = $conn->real_escape_string($_POST['linkoms'][$key]); $add = $conn->real_escape_string($_POST['linkadd'][$key]); $sql = "INSERT INTO $tab (linkoms, linkadd) VALUES ('$oms', '$add')" ; // the error is because $tab doesnt have a value So I was thinking maybe I overlook how a variable is set. Could you explain more about that? Edited July 14, 2015 by WesleyA Link to comment Share on other sites More sharing options...
Ingolme Posted July 14, 2015 Share Posted July 14, 2015 The problem is that you're running the script only if $_POST['kolom2'] does not exist. It should be the other way around: if(isset($_POST['kolom2']) { $tab = $_POST['kolom2']; // Run a database query} Link to comment Share on other sites More sharing options...
WesleyA Posted July 14, 2015 Author Share Posted July 14, 2015 The problem is that you're running the script only if $_POST['kolom2'] does not exist. It should be the other way around: if(isset($_POST['kolom2']) { $tab = $_POST['kolom2']; // Run a database query} But one way or another is the code after { not executed. The echo between the { } also not. What other options are there for if ? Link to comment Share on other sites More sharing options...
justsomeguy Posted July 14, 2015 Share Posted July 14, 2015 && ($linkoms = " ") &&That is changing the value of $linkoms, not checking if it is a space character. If you want to compare values you use == or ===, you use = to set values. Link to comment Share on other sites More sharing options...
WesleyA Posted July 14, 2015 Author Share Posted July 14, 2015 && ($linkoms = " ") &&That is changing the value of $linkoms, not checking if it is a space character. If you want to compare values you use == or ===, you use = to set values. My problem is not in the first part of the code. This code worked. I guess it wont change when I embed another if statement. Link to comment Share on other sites More sharing options...
Ingolme Posted July 14, 2015 Share Posted July 14, 2015 But one way or another is the code after { not executed. The echo between the { } also not. What other options are there for if ? If the table name isn't specified, you can't run the query. You're not supposed to, it won't work. Link to comment Share on other sites More sharing options...
WesleyA Posted July 14, 2015 Author Share Posted July 14, 2015 If the table name isn't specified, you can't run the query. You're not supposed to, it won't work. What exactly do you mean with specified? If I place var_dump($tab); outside the condition then it is printed. So after the first or second echo "test"; in the part of the script I uploaded here. Does that mean it is specified? Or is that done in another way? Link to comment Share on other sites More sharing options...
justsomeguy Posted July 14, 2015 Share Posted July 14, 2015 You should post the code you're using now, because this code is obviously wrong: if (!isset($_POST['kolom2']) && !isset($tab) ) { echo "test"; // not echoed at isset, is echoed at !isset $tab = $_POST['kolom2'];You're saying that if $_POST['kolom2'] is not set, and $tab is not set, then set $tab to $_POST['kolom2']. Obviously that doesn't make sense, you're only doing that if $_POST['kolom2'] is not set.In case you have error reporting turned off, add this to the top of your code:ini_set('display_errors', 1);error_reporting(E_ALL); 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