0
Hello! I am developing a site to buy a game that has a page to record the information of each purchase, among them, the amount of games purchased.
I wanted that, during the saving of the information, the amount of games purchased would be subtracted from the stock.
My attempt:
<?php
$nome = $_POST['NOME'];
$cpf = $_POST['CPF'];
$email = $_POST['EMAIL'];
$quantidade = $_POST['QUANTIDADE'];
$pagamento = $_POST['PAGAMENTO'];
$strcon = mysqli_connect('localhost','root','', 'ecoventura') or die('Erro ao conectar ao banco de dados');
$jogos = mysqli_query($strcon, "select JOGOS from estoque");
$novaquan = $jogos - $quantidade; //ESSA É A LINHA DO ERRO
$sql = "INSERT INTO compra (NOME, CPF, EMAIL, QUANTIDADE, PAGAMENTO) VALUES ('$nome','$cpf', '$email', '$quantidade', '$pagamento')";
$modifica = "UPDATE `ecoventura`.`estoque` SET `JOGOS` = '$novaquan'";
mysqli_query($strcon,$sql) or die("Erro ao tentar cadastrar registro" . mysqli_error($strcon));
mysqli_query($strcon,$modifica) or die("Erro ao tentar modificar estoque" . mysqli_error($strcon));
mysqli_close($strcon);
/*echo '<script type="text/javascript">
alert("Salvo com Sucesso !");
window.history.go(-1);
</script>';*/
?>
The form even saves the information, but the games column has a record and apparently $games contain the amount of records and not the content of it.
The error presented is this:
Notice: Object of class mysqli_result could not be converted to int in C: Easyphp5.2.10 www ecoventura salva-form.php on line 13
How can I take the contents of the record and not its quantity?
The return of function
mysqli_query
is not a number, so it makes no sense to do an algebraic operation with$jogos
, as it did. You selected the games column from all the stock table records, what would be the expected result for$jogos - $quantidade
?– Woss
Ah, so that’s why you’re giving it -4. Games are getting 1 because there’s only one record. How do I get the contents of this record and not the amount of records?
– Mariana Bayonetta
Wrong. You’re not taking the amount, you’re taking the content, you’re just using the return wrong. See documentation, function return
mysqli_query
is an instance ofmysqli_result
, then study the documentation to check how to work with this object.– Woss
So, I don’t know if I got it right but reading the documentation it seems that that mysqli_num_rows ( mysqli_result $result ) : int takes the contents of the record, but I tried to use it and it didn’t work. Why?
– Mariana Bayonetta
Neither. As the function name itself says,
num rows
returns the amount of records returned in the query. Returns number of Rows in the result set. What you need are the functions fetch.– Woss