Problem saving result of an SQL query to a PHP variable

Asked

Viewed 1,057 times

0

I have a SELECT to return the value of a table field funcionarionew, my idea is to save the result of SELECT into a variable and then insert the value of that variable into another table (terminal_cartao).

In my code, the variable $cod always has value 0, which is not correct. What will be the problem?

include('connectdb.php');

$cli = $_POST['cliente'];
$car = $_POST['descricao'];
$loc = $_POST['local'];
$vend = $_POST['codigoVend'];

$sqlcode = mysql_query("UPDATE terminal_cartao SET cliente='$cli', local='$loc' WHERE descricao='$car'");

$sql="SELECT codigo FROM funcionarionew WHERE nome='$vend'";

while($ex=mysql_fetch_array($sql)){

$cod=$ex['codigo'];
}

$sqlcode1 = mysql_query("UPDATE terminal_cartao SET vendedor='$cod' WHERE descricao='$car'");
  • a doubt your $Vend is a code or name ? vi that is catching $_POST['codeVend'] ?

  • @Lucianoazevedo is a name, string

  • Have you tried running this sql directly in the database ? returned ? checked if $_POST is not coming null ? pq whille ?

  • 1

    But you are not doing the $SQL query

  • Running straight into the bank is "ok", the $_POST also checked, is correct.

  • as @Daniel Gregatto said, missing $sql = mysql_query("SELECT code....")

  • All the error for a silly distraction! the problem was that I didn’t put mysql_query in $sql. : ( Thank you guys

Show 2 more comments

1 answer

3


See if the code below works: I checked that you are not doing the SQL query and you are also browsing an array when you actually want to bring only 1 record.

include('connectdb.php');

$cli = $_POST['cliente'];
$car = $_POST['descricao'];
$loc = $_POST['local'];
$vend = $_POST['codigoVend'];

$sqlcode = mysql_query("UPDATE terminal_cartao SET cliente='$cli', local='$loc' WHERE descricao='$car'");

$sql= mysql_query("SELECT codigo FROM funcionarionew WHERE nome='$vend' LIMIT 1");
$sql = mysql_fetch_array($sql);
$cod = $sql['codigo'];

$sqlcode1 = mysql_query("UPDATE terminal_cartao SET vendedor='$cod' WHERE descricao='$car'");
  • All the mistake for a silly distraction! The problem was I didn’t put mysql_query in the $sql. :(

Browser other questions tagged

You are not signed in. Login or sign up in order to post.