代码之家  ›  专栏  ›  技术社区  ›  Rodrigo Campos

Echo数据库用户名和已发布的电子邮件

  •  0
  • Rodrigo Campos  · 技术社区  · 8 年前

    我试图在表单上发布电子邮件后从数据库中获取用户的姓名,但它没有显示姓名,如果我添加成功消息,它会显示它,因此它工作正常,但它没有显示姓名:(

    <?php
    
    $conn = mysqli_connect("xxx", "xxx", "xxxxxxxx", "xxxxxxx");
    
    
    $email = $_POST['email_r'];
    
    
    $sqlr = "SELECT * FROM participantes WHERE email='$email'";
    $result = $conn->query($sqlr);
    
    
    if(!$row = mysqli_fetch_assoc($result)) {
        echo "Email Incorrecto: No se a registrado.";
    } else {
        echo "your name is: ->" . $row['name'] . " <- that is it.";
    }
    
    ?>
    1 回复  |  直到 8 年前
        1
  •  0
  •   Phil    8 年前

    如果让我猜的话,我会说你的桌子没有 name 名称 价值

    这至少可以帮助您查明任何潜在问题,并解决SQL注入漏洞。。。

    <?php
    // show any errors
    ini_set('display_errors', 'On');
    
    // show all errors
    error_reporting(E_ALL);
    
    // make MySQLi throw exceptions
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    
    $conn = mysqli_connect("xxx", "xxx", "xxxxxxxx", "xxxxxxx");
    
    // safely get the request parameter
    $email = isset($_POST['email_r']) ? $_POST['email_r'] : null;
    
    // Prepare a statement with a placeholder for the "email" parameter
    $stmt = $conn->prepare('SELECT `name` FROM `participantes` WHERE `email` = ?');
    
    // bind the parameter
    $stmt->bind_param('s', $email);
    $stmt->execute();
    
    // bind results. This seems easier than fetch_assoc IMHO
    $stmt->bind_result($name);
    
    // fetch records, if any
    if ($stmt->fetch()) {
        echo 'your name is: ->', $name, ' <- that is it.';
    } else {
        echo "Email Incorrecto: No se a registrado.";
    }
    
    $stmt->close();