【问题标题】:Echo database user Name with Posted emailEcho 数据库用户名和已发布的电子邮件
【发布时间】:2017-11-14 01:29:14
【问题描述】:

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

<?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.";
}

?>

【问题讨论】:

  • 您的participantes 表是否有name 列?我建议你将它添加到脚本的顶部~error_reporting(E_ALL); ini_set('display_errors', 'On');
  • 另外,您在这里很容易受到 SQL 注入攻击。您应该使用准备好的语句并绑定 email 参数。见php.net/manual/mysqli.quickstart.prepared-statements.php

标签: php mysql post get echo


【解决方案1】:

如果我不得不猜测,我会说您的表没有name 列。该记录或匹配记录的 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-25
    • 2018-02-13
    • 2022-12-16
    • 2016-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多