0

I am accessing my database on PhpMyAdmin, all my names etc are correct, but for some reason UserId is not working. Can someone point me in the right direction?

I have tried printing it but nothing displays.

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

1 に答える 1

0

var_dump($var) を使用して、変数の内容を確認します

最初に $result で何が返されるかを確認し、$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 ";
}
?>
于 2013-03-20T16:41:17.317 に答える