Jump to content
Sign in to follow this  
WesleyA

PHP/MYSQL use of backticks (double) quotes ?

Recommended Posts

which query should be used when you have this

        INSERT INTO `$variable` (col1, col2) VALUES .....

The question is regarding $variable. is it " ", ' ' ,` ` .

 

?

Share this post


Link to post
Share on other sites

Backticks are used around table identifiers and field identifiers in the query. Single or double quotes are used around string values.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

It's still a table identifier, not the value of a field. MySQL doesn't know it's a variable, only PHP does.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

These are VALUES, as the query indicates:

VALUES (`$varc`, `$vard`)

String values must be wrapped in single quotes or double quotes, not backticks.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 by WesleyA

Share this post


Link to post
Share on other sites

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}

Share this post


Link to post
Share on other sites

 

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 ?

Share this post


Link to post
Share on other sites
&& ($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.

Share this post


Link to post
Share on other sites
&& ($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.

Share this post


Link to post
Share on other sites

 

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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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);

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...