简体   繁体   中英

how to fetch data from table using php and mysql

I am trying to fetch an email from a particular row or based on the id_code from people table... That is, say I enter a id_code = 456 , if the id code exists the email of that specific id_code has to be retrieved, and generate a token number and insert the token number into the people table. After the token is generated and inserted, a URL link has to be sent with the token and an id.

How would i do that since i am a beginner, can someone tell me where I am going wrong?

Here is what I did so far:

<?php
    error_reporting(1);
    session_start();

    include 'includes/db.php';
    include 'includes/token.php';

    //global
    $id_code = strtoupper(trim($_POST['id_code']));

    if ($_POST["Submit"] == "Submit") {
        $sql = "SELECT * FROM people WHERE id_code = :id_code";
        $stmt = $pdo->prepare($sql);
        $stmt->bindValue(':id_code', $id_code);
        $stmt->execute();
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
        if (!empty($_POST['id_code'])) {
            $sql = "SELECT email FROM people WHERE id_code = $id_code";

            $stmt = $pdo->prepare($sql);
            $stmt->bindValue(':id_code', $id_code);
            $stmt->execute();
            $result2 = $stmt->fetch(PDO::FETCH_ASSOC);
        } else {
            //echo "<br/>Validated: FALSE<br/>"; die();
            echo 'You are not Registered..Please Contact support';
        }
    }
?>

As far as I can see $id_code is not defined. The value you might want to use is stored in $_POST['id_code'] so you should do something like $id_code = $_POST['id_code']; in front of your IF condition, otherwise $id_code is undefined.

Update: you already did it with $ccode , use this for the binding and it should work.

$stmt->bindValue(':id_code', $id_code);

replaced by

$stmt->bindValue(':id_code', $ccode);

UPDATE

please try the following code and post the result of the var_dump():

<?php
    error_reporting(1);
    session_start();

    include 'includes/db.php';
    include 'includes/token.php';

    //global
    $id_code = strtoupper(trim($_POST['id_code']));

    var_dump("ID: ".$id_code);

    if ($_POST["Submit"] == "Submit") {
        $sql = "SELECT * FROM people WHERE id_code = :id_code";
        $stmt = $pdo->prepare($sql);
        $stmt->bindValue(':id_code', $id_code);
        $stmt->execute();
        $result = $stmt->fetch(PDO::FETCH_ASSOC);

        var_dump("Result: ".$result);

        if (!empty($_POST['id_code'])) {
            $sql = "SELECT email FROM people WHERE id_code = $id_code";

            $stmt = $pdo->prepare($sql);
            $stmt->bindValue(':id_code', $id_code);
            $stmt->execute();
            $result2 = $stmt->fetch(PDO::FETCH_ASSOC);

            var_dump("Result2: ".$result2);

        } else {
            //echo "<br/>Validated: FALSE<br/>"; die();
            echo 'You are not Registered..Please Contact support';
        }
    }
?>

Do you get any output from your script?

Use correct binding for second query too (you have assigned $id_code)

 if (!empty($_POST['id_code'])) {
        $sql = "SELECT email FROM people WHERE id_code = :id_code";

        $stmt = $pdo->prepare($sql);
        $stmt->bindValue(':id_code', $id_code);
        echo $email;
        $stmt = $pdo->prepare($sql);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM