如何获得特定价值?

时间:2016-04-06 16:03:45

标签: php html sql

我想在数据库表(create_acc)中显示相应的用户名及其电子邮件到个人的个人资料页面,但每当我执行代码时,它都会给出错误undefined index:uname并且相同电子邮件undefined index:email_id

问题出在哪里?

<?php

$a1 = $_GET["uname"];
$a2 = $_GET["email_id"];

$con = mysqli_connect('localhost', 'root', '');
if (!$con)
{
    die("could not connect to the server" . mysqli_error());
}

mysqli_select_db($con, 'forum');



$q = "select * from create_acc where username = '" . $a1 . "' and user_email = '" . $a2 . "'";

$result = mysqli_query($con, $q);
echo '<div class="user">';
while ($row = mysqli_fetch_assoc($result))
{
    echo'<div class="rows">';

    echo $row["username"];
    echo $row["user_email"];

    echo '</div>';
}

echo '</div>';
mysqli_close($con);

?>

3 个答案:

答案 0 :(得分:1)

  

您在数据库连接时出错了。

试试这个。

从网址获取数据。

$a1 = $_GET["uname"];
$a2 = $_GET["email_id"];

建立数据库连接

$con = mysqli_connect('localhost','root','', 'forum');
if(!$con){
  die("could not connect to the server".mysqli_error());
}

查询和结果

$q = "SELECT * FROM `create_acc` where `username`='".$a1."' AND `user_email`='".$a2."'";
$result = mysqli_query($con, $q);
?>
<div class="user">
<?php while($row = mysqli_fetch_object($result)){?>
    <div class="rows">
    <?php
    echo $row->username;
    echo $row->user_email;
    ?>
  </div>
    <?php }?>
</div>

答案 1 :(得分:0)

确保您的方法属性与您正在调用的预定义变量相对应。例如,在form

<form action=".php" method="get">    
    Username:<input type="text" name="uname" value=""><br />
    Email:<input type="text" name=email_id" value="">
</form>

确保输入的名称参数与$_GET索引

匹配

不要误会,您可以在php文件

中使用此功能
if(isset($_SERVER["REQUEST_METHOD"] == "POST")) {
    foreach($_POST as $key => $value) {
       item[$key] = $value;
    }
} elseif(isset($_SERVER["REQUEST_METHOD"] == "GET") {
    foreach($_GET as $key => $value) {
        item[$key] = $value;
    }
}
$username = item["uname"];
$email = item["email_id"];

答案 2 :(得分:0)

请替换你的mysqli_select_db($ con,'forum');用我的代码mysqli_select_db($ con,“forum”);然后$ result = $ con&gt;查询($ q);