【问题标题】:Passing variable values传递变量值
【发布时间】:2016-08-17 05:43:05
【问题描述】:

如果其中有值,我可以知道如何从 $query 传递值。如果它为空,我仍然必须传递变量。尽管变量确实存在于 sql 数据库中,但我不断收到未定义变量的错误。

<?php
include("dbconnect.php");
include("header.php");

if (isset($_POST['btn'])) {
    $uname        = $MySQLi_CON->real_escape_string(trim($_POST['user_name']));
    $email        = $MySQLi_CON->real_escape_string(trim($_POST['user_email']));
    $upass        = $MySQLi_CON->real_escape_string(trim($_POST['password']));
    $enroller_id_n = $MySQLi_CON->real_escape_string(trim($_POST['enroller_id_n']));        
    $enrolled_id_n=  $MySQLi_CON->real_escape_string(trim($_POST['enrolled_id_n'])); 
    $direction = $MySQLi_CON->real_escape_string(trim($_POST['direction'])) ;
    $new_password = password_hash($upass, PASSWORD_DEFAULT);
    $query = $MySQLi_CON->query("select * from personal where enroller_id='".$enroller_id_n."'");
    if($query){
        while ($row = $query->fetch_array()) {
            $enroller_id3 = $row['enroller_id'];
            $left_mem     = $row['left_mem'];
            $right_mem    = $row['right_mem'];
            $test         = "left_mem";
            $test2        = "right_mem";
            $direc        = $direction;
        }
    }
}
?>

【问题讨论】:

  • 将值从$query 传递到where?
  • 你在 $query 上试过 print_r 吗?结果如何?
  • 传递 $query 的变量,例如 $left mem 和 $right mem。这里的问题是我得到未定义的变量,因为我在个人中的登记者 ID 是空的
  • 了解准备语句。
  • 每次您在 meow 代码 a Kitten is strangled somewhere in the world 中使用 the mysql_ 数据库扩展时,它都会被弃用,并且已经存在多年并且永远消失了在 PHP7 中。如果您只是学习 PHP,请花精力学习 PDOmysqli 数据库扩展。 Start here

标签: php mysql mysqli


【解决方案1】:

您需要在这里尝试一些调试。例如,

var_dump($enroller_id_n); // Check if the variable is not empty

$query = $MySQLi_CON->query("select * from personal where enroller_id='".$enroller_id_n."'") or die($MySQLi_CON->error);

$row = $query->fetch_array(MYSQLI_ASSOC);

echo "<pre>";
print_r($row);  // Check your result array

根据 cmets,您需要从查询中删除 WHERE 条件并将其更改为简单:

$query = $MySQLi_CON->query("select * from personal");

【讨论】:

  • 但是里面有空数据。页面不会显示:(
  • 你的意思是 $enroller_id_n 是空的?
  • 我的enroller_id 数据在个人表中为空。我只想让下面的查询运行 if ($left_mem != $test && $right_mem != $test2 && $enroller_id3 != $enroller_id_n && $direc == $test) { } 即使没有数据
  • 然后您需要从查询中删除 where 条件并将其更改为简单: $query = $MySQLi_CON->query("select * from personal");
猜你喜欢
  • 2019-03-23
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多