2013-03-20 58 views
0

我正在訪問我的數據庫上的PhpMyAdmin,我所有的名字等都是正確的,但由於某種原因用戶ID不起作用。有人能指引我朝着正確的方向嗎?PHP初學者。試圖顯示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 "; } 
?> 
+0

'UserId'是數據庫中的一列嗎? – Kermit 2013-03-20 16:25:37

+5

嘗試使用'var_dump($ row)'來查看yoe在該變量中的真實含義 – 2013-03-20 16:26:31

+4

您確實需要通過散列來閱讀sql注入和安全密碼存儲。 – jeroen 2013-03-20 16:26:34

回答

0

用var_dump($ VAR),看看有什麼是變量

1支票使用它 之前,而檢查後,如果$用戶ID設置什麼是在$結果後檢查$用戶ID返回(如果條件爲假,歐VAR沒有設置..),你應該檢查$ ROW_COUNT太

你應該縮進代碼
這裏是你的代碼reindented:

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

它在結束時關閉 user1944305 2013-03-20 16:57:03

+0

啊好吧,對不起程序沒有正確縮進的代碼,並具有1片多(也許是因爲沒有回報)...你看到了良好的縮進是必不可少的,而編碼....怎麼樣變種轉儲? 我編輯了帖子,並給出了縮進代碼的核心版本 – Acuao 2013-03-20 17:20:01