从php MySQL数据库获取数据不起作用

时间:2013-04-11 13:13:14

标签: php mysql database

我让我的表单完美运行并从我的数据库中获取数据。但是最近我从登录页面更改了一些东西,以便为不同的用户分配不同的页面,一旦他们成功登录,现在我没有在studentloggedin.php中显示他们的数据(结果)。谢谢提前

我认为问题来自我假设的变量。

studentloggedin.php

<?php
//code for display marks

$con=mysqli_connect("localhost","kurtfarrugia","1234","kurt_farrugia");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM tblexamresults WHERE admin_usr_name ='$username'");

while($row = mysqli_fetch_array($result))
{
echo "English: " . $row['english'] . " marks";
echo "<br />";
echo "<br />";
echo "Maltese: " . $row['maltese'] . " marks";
echo "<br />";
echo "<br />";
echo "Italian: " . $row['italian'] . " marks";
echo "<br />";
echo "<br />";
echo "PSE: " . $row['pse'] . " marks";
echo "<br />";
echo "<br />";
echo "Social Studies: " . $row['social_studies'] . " marks";
echo "<br />";
echo "<br />";
echo "Physical Education: " . $row['physical_education'] . " marks";
echo "<br />";
echo "<br />";
echo "Drama: " . $row['drama'] . " marks";
echo "<br />";
echo "<br />";
echo "Graphical Communication: " . $row['graphical_communication'] . " marks";
echo "<br />";
echo "<br />";
echo "Computer Studies: " . $row['computer_studies'] . " marks";
echo "<br />";
echo "<br />";
echo "Mathematics: " . $row['mathematics'] . " marks";
echo "<br />";
echo "<br />";
echo "Integrated Science: " . $row['integrated_science'] . " marks";
echo "<br />";
echo "<br />";
echo "Physics: " . $row['physics'] . " marks";
echo "<br />";
echo "<br />";
echo "<br />";

$total = $row['english'] + $row['maltese'] + $row['italian'] + $row['pse'] +       $row['social_studies'] + $row['physical_education'] + $row['drama'] + $row['graphical_communication'] + $row['computer_studies'] + $row['mathematics'] + $row['integrated_science'] + $row['physics'];

$average= $total / 11;

echo "Statistics:"; 
echo "<br />";
echo "<br />";  
echo "Your average mark is: " . round($average, PHP_ROUND_HALF_UP) . " marks";
}

mysqli_close($con);
?>  

的login.php

<?php 
session_start();
//*********Server Information to establish a connection ******

$host        =    'localhost';
$user        =    'kurtfarrugia';
$password    =    '1234';
$database    =    'kurt_farrugia';

//Open connection   
$mysqli = new mysqli($host, $user, $password, $database);
if (mysqli_connect_errno()) 
{
printf("Connection failed: %s\n", mysqli_connect_error());
exit();
}

//***************End Connection Establishment***************************************
//*******Form Information********

$userName = mysql_real_escape_string($_POST['username']); //User Name sent from Form
$password = mysql_real_escape_string($_POST['password']); // Password sent from Form


//*********retrieving data from Database**********


$result = $mysqli->query("SELECT * FROM tbladmin WHERE admin_usr_name = '".$userName."'     and admin_pwd = '".$password."'");
if($row = $result->fetch_assoc())
{
if($row['occupation'] == 'student')
{
    header( "Location: studentloggedin.php" );
}
 else if($row['occupation'] == 'lecturer') 
{
    echo "you are a lecturer";
}
} 
else
{
 header( "Location: login_fail2.php" );
}

//**********it ensures that the script does not continue unless the username varaible is not null and not empty
if (!empty($_POST['username'])) {
$username = mysql_real_escape_string(strip_tags(trim($_POST['username'])));
} else {
//die('Hey turd, go back and fill in your username!');
    header( "Location: login_fail2.php" ); die;
}
?> 

这是我以前登录的页面,它与登录页面的学生一起工作。所以我在新登录页面上缺少什么?

PREVIOUS LOGIN页面,效果很好

<?php 
session_start();
//*********Server Information to establish a connection ******

$host        =    'localhost';
$user        =    'kurtfarrugia';
$password    =    '1234';
$database    =    'kurt_farrugia';

$link = mysql_connect($host,$user,$password) or die('Error in Server information');
mysql_select_db($database,$link) or die('Can not Select Databasse');

//***************End Connection Establishment***************************************
//*******Form Information********

$userName = mysql_real_escape_string($_POST['username']); //User Name sent from Form
$password = mysql_real_escape_string($_POST['password']); // Password sent from Form

$rememberMe = strip_tags($_POST['rememberMe']);
setcookie("username", $_POST['username']);

//*********retrieving data from Database**********

$query = "select * from tbladmin where admin_usr_name='$userName' and          
admin_pwd='$password'";

$res = mysql_query($query);

$rows = mysql_num_rows($res);

//**********it ensures that the script does not continue unless the username varaible     is not null and not empty
if (!empty($_POST['username'])) {
$username = mysql_real_escape_string(strip_tags(trim($_POST['username'])));
} else {
//die('Hey turd, go back and fill in your username!');
header( "Location: login_fail2.php" ); die;
}

/*if ($rememberMe) {
setcookie("loggedIn", "yes", time()+3600);

header ("Location: studentloggedin.php" ); die;
         }
else {
 echo "Username and/or password is incorrect.";
}


if ($_COOKIE['loggedIn'] == "yes") {
header ("Location: studentloggedin.php" );
die();
}
*/
// HERE is the query 

//$result2 = mysqli_query($con,"SELECT occupation FROM tbladmin WHERE admin_usr_name        ='$username'" == 'student');
//$row == mysqli_fetch_array($result)

//**********if $userName and $password will match database, The above function will        return 1 row
if($rows==1)
//{
{
 //***if the userName and password matches then register a session and redrect user to        the studentloggedin.php

 //$_SESSION['userName'];
 //$_SESSION['password'];

header("location:studentloggedin.php");
}
//}

else
{
header( "Location: login_fail2.php" ); die;
}

?>  

1 个答案:

答案 0 :(得分:1)

从未设置SQL查询中的变量$username

$result = mysqli_query($con,"SELECT * FROM tblexamresults WHERE admin_usr_name ='$username'");

也未在URL中传递

header( "Location: studentloggedin.php" );

您应该将用户名放入您的网址并获取值以执行您的查询。