Account with received data and mysql data

Asked

Viewed 108 times

1

Good afternoon, it is the following in my project I need to do an account so at the end have the result for the customer know how much to pay, but I am not able to do this account.
In my case, I ask for the days and then the room will have a price (which in this case is already registered in mysql) and I want to fetch this data from mysql and do the account for example (amount = price * days).

<html>

Data sent

<div id="middle">
   <br><br><p>Obrigado pela sua preferência!</p>
           <p>A partir deste momento, tem 24 horas para efetuar o pagamento e completar a sua estadia.</p>


Entity: 00000

      <tr>
        <td>Referência:</td>
        <td>000 000 000</td>
      </tr>

      <tr>
        <td>Montante:</td>
        <td><?php $montante; ?>€</td>
      </tr>
    </table>



$sql = "select preco from casa Where num_casa = 1"; $result = mysqli_query($con, $sql); $Row = mysqli_fetch_array($result);

//Form variables $name=$_POST['name']; $surname=$_POST['surname']; $email=$_POST['email']; $phone=$_POST['phone']; $contributor=$_POST['contributor']; $cc=$_POST['cc']; $address=$_POST['address']; $data_input=$_POST['data_input']; $data_saida=$_POST['data_saida'];

$price = $Row["price"];

$difference = strtotime($data_output) - strtotime($data_input); $num_days = floor($difference / (60*60*24));

$amount = ($num_days * $price);

echo "Name: ".$name."
"; echo "Surname: ".$surname."
"; echo "E-mail: ".$email."
"; echo "Telephone: ".$telephone."
"; echo "Taxpayer: ".$taxpayer."
"; echo "Citizen Card: ".$cc."
"; echo "Address: ".$address."
"; echo "Date entered: ".$data_input."
"; echo "Date left: ".$dating."
"; echo "Number of days: ".$in a few days."
";

//It will record the data entered by the user $sql = "INSERT INTO hospede (first name, last name, email, phone, contributor, cc, address, incoming date, outgoing date, num_days) VALUES ('$first name', '$last name', '$email', '$phone', '$contributor', '$cc', '$address', '$new date', '$data_exit', '$num_days')"; $result = mysqli_query($con, $sql); if ($result) echo ""; Else echo "Error trying to record data in the database!"; ?>
Come back

Someone can edit, I don’t know how to format.

  • Please provide a snippet of code in the question instead of an image.

  • https://pt.meta.stackoverflow.com/questions/5483/manual-de-como-n%C3%83o-fazer-perguntas/5485#5485

  • That’s it, I’m waiting for someone to format my text.

2 answers

0

Hello,
If the $row["preco"] is returning the correct value, tries to change the assignment order of:
$row["preco"] = $preco;
for:
$preco = $row["preco"];
If the $row["preco"] is not returning, there is something wrong with sql.

  • That’s right, just reverse that works.

  • Thank you, at the moment you are not showing any error. But I am not able to show the variable here (<td>Amount:</td><td><? php $amount; ? >€</td>).

  • $preco = 2.50;&#xA;$data_saida = date('Y-m-d');//peguei data atual&#xA;$data_entrada = date('Y-m-d', strtotime("-3 day", strtotime($data_saida)));//peguei data a data de saida - 3 dias&#xA;$diferenca = strtotime($data_saida) - strtotime($data_entrada); &#xA;$num_dias = floor($diferenca / (60*60*24));&#xA;$montante = ($num_dias * $preco);&#xA;&#xA;var_dump($montante); According to the code snippet, quoted above is a copy of your, setting the values, and this working, check if all values are being filled.

  • Why did you assign (2.5) to the $price variable? Supposedly this variable was to be retrieved from the database... I think the only thing I really lack is getting the result of $num_days * $price on the table.

  • These values were set, manually to test, the code, your preco or num_dias must be valueless or value-aged. Valide, if filled.

  • I’m going to test, by the way "<td><? php $amount; >€</td>" this is right?

  • No, to print can do <?=$montante?>€ or <?php echo $montante; ?>€ I prefer to use the first way.

  • This gives me this error: Notice: Undefined variable: montante in C: xampp htdocs PAP admin hospederegisto.php on line 33

  • You are putting to "print" the variable after the PHP code ?? if you have git for the project and can provide it, I can see better what is happening.... But the error is that the variable has not been defined yet.

  • Ahhhhh, my own mistake, the code was after so he should not be associating, thank you Felipe and all who helped.

Show 5 more comments

-1

For this situation to work, replace this line:

mysqli_fetch_array($result);

for

mysqli_fetch_assoc($result);

This way the array key will be returned by the value of the field name (price) as it came it returns only a numeric index of the position in the array.

Browser other questions tagged

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