types - php comparing POST['integer string'] with an integer from database

612

I have an integer coming from the form POST. I know that all POST variables are strings.

Suppose I want to compare this POST['integer string'] with an integer from the database as: my_page_action.php =>

$query = "SELECT amount FROM table where id=:id";
$stmt = $conn_p->prepare($query);
$stmt->execute(array(':id' => $id));
$data = $stmt->fetch();

if ( ! empty($data)) {

    if ($data['amount'] != $_POST['amount']) {
        header("location:my_page.php?msg=Invalid Amount");
        exit();
    }
} else {
    header("location:my_page.php?msg=Invalid Record");
    exit();
}
947

Answer

Solution:

It is likely the types of the variables are different, in this case I'm using float, but you could use intval, stringval, etc:

if (floatval($data['amount']) != floatval($_POST['amount'])) {
379

Answer

Solution:

You can useintval($_POST['amount'] instead of just$_POST['amount'], see http://php.net/manual/en/function.intval.php

You also have the float equivalent, floatval (http://php.net/manual/en/function.floatval.php)

People are also looking for solutions to the problem: php - query string with laravel

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.