PHP初学者。试图显示UserId

时间:2013-03-20 16:24:12

标签: php phpmyadmin

我在PhpMyAdmin上访问我的数据库,我的所有名字等都是正确的,但由于某种原因, UserId 无效。有人能指出我正确的方向吗?

我尝试过打印但没有显示。

<?php session_start(); 
$username = $_GET['username'];
$password = $_GET['password'];


// Create connection


$con=mysqli_connect("localhost","root","","test");

// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}



$result = mysqli_query($con,"SELECT * FROM user2 where username='$username' and   password='$password'");

$row_cnt = mysqli_num_rows($result);
if($row_cnt >0){

while($row = mysqli_fetch_array($result)){
$UserId = $row['UserId'];
}
$sqlQuery2 = "SELECT ProductID, Name, Price, Description FROM product";

echo "Hello ".$username."<br>" .$UserId.   "<br> This is a list of products";

$result2 = mysqli_query($con,$sqlQuery2);

echo "<table border='1'>
<tr>
<th>ProductID</th>
<th>Name</th>
<th>Price</th>
<th>Description</th>
<th>View</th>
 </tr>";

while($row = mysqli_fetch_array($result2))
 {
 echo "<tr>";
 echo "<td>" . $row['ProductID'] . "</td>";
 echo "<td>" . $row['Name'] . "</td>";
 echo "<td>" . $row['Price'] . "</td>";
 echo "<td>" . $row['Description'] . "</td>";
 echo "<td><a href=\"detailview.php?ProductID=".$row['ProductID']."\"'>Detailed View</a></td>";
 echo "</tr>";
 }
echo "</table>";

mysqli_close($con);
?>
<a href="userupdatedetails.php?UserId=<?php echo $UserId ?>">Update My Details</a>
<?php } else{
echo "invalid login "; }
?>

1 个答案:

答案 0 :(得分:0)

使用var_dump($ var)查看变量

中的内容

首先检查$ result中返回的内容,然后在使用之前检查$ UserId 在检查$ UserId是否设置后(如果条件为假,ou var未设置..)你应该检查$ row_count

你应该缩进你的代码
这是你的代码重新缩进:

<?php session_start(); 
$username = $_GET['username'];
$password = $_GET['password'];


// Create connection


$con=mysqli_connect("localhost","root","","test");

// Check connection
if (mysqli_connect_errno($con))
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}



$result = mysqli_query($con,"SELECT * FROM user2 where username='$username' and   password='$password'");

$row_cnt = mysqli_num_rows($result);
if($row_cnt >0){

    while($row = mysqli_fetch_array($result)){
        $UserId = $row['UserId'];
    }
    $sqlQuery2 = "SELECT ProductID, Name, Price, Description FROM product";

    echo "Hello ".$username."<br>" .$UserId.   "<br> This is a list of products";

    $result2 = mysqli_query($con,$sqlQuery2);

    echo "<table border='1'>
    <tr>
    <th>ProductID</th>
    <th>Name</th>
    <th>Price</th>
    <th>Description</th>
    <th>View</th>
    </tr>";

    while($row = mysqli_fetch_array($result2))
    {
        echo "<tr>";
        echo "<td>" . $row['ProductID'] . "</td>";
        echo "<td>" . $row['Name'] . "</td>";
        echo "<td>" . $row['Price'] . "</td>";
        echo "<td>" . $row['Description'] . "</td>";
        echo "<td><a href=\"detailview.php?ProductID=".$row['ProductID']."\"'>Detailed View</a></td>";
        echo "</tr>";
    }
    echo "</table>";

    mysqli_close($con);
    ?>
    <a href="userupdatedetails.php?UserId=<?php echo $UserId ?>">Update My Details</a>
    <?php
}
else
{
    echo "invalid login ";
}
?>