简体   繁体   English

如何使用php和mysql从表中获取数据

[英]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.我正在尝试从特定行或基于people表中的id_code获取电子邮件......也就是说,假设我输入id_code = 456 ,如果 id 代码存在,则必须检索该特定id_code的电子邮件,并且生成令牌号并将令牌号插入到 people 表中。 After the token is generated and inserted, a URL link has to be sent with the token and an id.生成并插入令牌后,必须发送带有令牌和 id 的 URL 链接。

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.据我$id_code没有定义。 The value you might want to use is stored in $_POST['id_code'] so you should do something like $id_code = $_POST['id_code'];您可能想要使用的值存储在$_POST['id_code']因此您应该执行类似$id_code = $_POST['id_code']; in front of your IF condition, otherwise $id_code is undefined.在您的 IF 条件之前,否则$id_code未定义。

Update: you already did it with $ccode , use this for the binding and it should work.更新:您已经用$ccode ,将它用于绑定,它应该可以工作。

$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():请尝试以下代码并发布 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)对第二个查询也使用正确的绑定(您已分配 $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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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