Problem saving result of a SQL query to a PHP variable

0

I have SELECT to return the value of a field of table funcionarionew , my idea is to save the result of SELECT on 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'");
    
asked by anonymous 26.01.2015 / 18:37

1 answer

3

See if the code below works: I checked that you are not doing the SQL query and you are also traversing an array when you actually want to only bring 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'");
    
26.01.2015 / 19:09