Concatenate 2 PHP variables in SQL statement

1

I am developing a PHP system with Oracle and to "optimize" an update, I would need to concatenate a variable (with values from 1 to 3) in the name of the variable I will pass in the statement. The variables come from a form, and I need to pass them one by one, without using array.

The following is an excerpt that follows:

$update .="CODIGO = '$produto_1' AND ";

The above example would need to pass something like this, where $passo would have fixed values (now 1 to 3):

$update .="CODIGO = '$produto_.$passo' AND ";

In this case, you should also pass $produto_1 , then $produto_2 ...

Using the point to concatenate did not work. Any suggestions?

    
asked by anonymous 09.09.2016 / 19:01

4 answers

3

This is very complex, use arrays , for this reason they exist, otherwise you will have to use eval , which would be something like (I recommend do not use > eval , is just for example):

$var1 = eval('return $produto_' . $passo);

$update .="CODIGO = '$var1' AND ";

Then do with array , which would look something like:

$produto = array();

$produto[1] = ...;
$produto[2] = ...;

$update .= "CODIGO = '" . $produto[$passo] . "' AND ";

You have quoted form, so I can assume it refers to HTML, you can do so too:

<form method="POST" action="pagina.php">
    <input type="text" name="passo[]" value="a">
    <input type="text" name="passo[]" value="b">
    <input type="text" name="passo[]" value="c">
    <button>Enviar</button>
</form>

And in PHP you will get like this:

<?php
print_r($_POST['passo']);

The output will be:

Array
(
    [0] => a
    [1] => b
    [2] => c
)
    
09.09.2016 / 19:05
4

I used extract to generate the test of the variables passo_X , but the logic applied would be for .

$arrayPassos = array(
    'passo_1' => 'teste1',
    'passo_2' => 'teste2',
    'passo_3' => 'teste3',
    'passo_4' => 'teste4',
);
extract($arrayPassos); // criada variaves passo_X

$out = '';
for ($i=1; $i <= count($arrayPassos); $i++){
    $out .= ${"passo_{$i}"};
}

Note

  • For these cases it is best to use array , not numbered variables.
09.09.2016 / 19:11
2
  

Using the point to concatenate did not work. Any suggestions?

It did not work as expected because the concatenation attempt is being quoted, . is being interpreted as any character, not as concatenation operator .

Here's another alternative:

$update .= "CODIGO = '{$produto}_{$passo}' AND ";

Editing : You can also referencing the variable in a loop :

$produto_1 = "PROD1";
$produto_2 = "PROD2";
$produto_3 = "PROD3";

$limite = 3;
$update = "";

for ($passo = 1; $passo <= $limite; $passo++) {
    $update .= "CODIGO = '" . ${"produto_$passo"} . "' AND ";
}

echo $update;
// CODIGO = 'PROD1' AND CODIGO = 'PROD2' AND CODIGO = 'PROD3' AND

See DEMO

    
09.09.2016 / 19:18
1

You can assemble the concatenation with the sprintf() function as well, the %s is changed by the variable repectives (values) in the order that they appear.

$passo = $_POST['passo']; 
$update .= sprinf("CODIGO = '%s_%s'", $produto, $passo);
    
09.09.2016 / 19:20