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

时间:2016-05-09 07:48:18

标签: php mysql pdo

我正在尝试从特定行或基于id_code people表中的id_code = 456获取电子邮件...也就是说,如果我输入id_code,则输入ID代码存在必须检索特定<?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'; } } ?> 的电子邮件,并生成令牌号并将令牌号插入人员表。生成并插入令牌后,必须使用令牌和ID发送URL链接。

我怎么会这样做,因为我是初学者,有人可以告诉我哪里出错了吗?

这是我到目前为止所做的:

sl.RemoveFilter();

2 个答案:

答案 0 :(得分:1)

据我所知,$id_code未定义。您可能想要使用的值存储在$_POST['id_code']中,因此您应该在IF条件前执行$id_code = $_POST['id_code'];之类的操作,否则$id_code未定义。

更新:您已经使用$ccode执行了此操作,将其用于绑定,它应该可以正常工作。

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

替换为

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

更新

请尝试以下代码并发布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';
        }
    }
?>

你从脚本中得到任何输出吗?

答案 1 :(得分:0)

也为第二个查询使用正确的绑定(您已分配$ 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);