I'm a newbie in PHP; please guide me slowly and carefully.
I have a login page (login.php) and a user page (useracc-test.php). The user page (useracc-test.php) displays all personal data after a user logs in using his own username and password. I can display other data such as (name), (username) and (telno) in php script when I echo it.
But the problem is, when I tried to echo the data in html, just below the php script, only (username) shows on the screen. The other two (name) and (telno) show nothing. No errors displayed on browser also, and also no syntax error. Below is my user page (useracc-test.php).
<?php
//useracc-test.php
/**
* Start the session.
*/
session_start();
/**
* Include our MySQL connection.
*/
// require 'lib/password.php';
require 'connect-test.php';
$userName=$_POST['username'];
//$sql = "SELECT name, username FROM users WHERE username = '" . $_POST['username'] . "'";
//$result = $conn->query($sql);
$query = sprintf("select name, username, telno FROM users WHERE username='%s'", mysql_real_escape_string($userName));
$result = $conn->query($query); ?>
<?php while($row=$result->fetch_assoc()): ?>
<div id="satu"><?= $row['name'] ?></div>
<div id="dua"><?= $row['username'] ?></div>
<div id="tiga"><?= $row['telno'] ?></div>
<?php endwhile; ?>
<html>
<head>
<style type="text/css">
#apDiv2 {
position: absolute;
left: 51px;
top: 238px;
width: 237px;
height: 93px;
z-index: 1;
}
#apDiv1 {
position: absolute;
left: 50px;
top: 344px;
width: 234px;
height: 104px;
z-index: 2;
}
</style>
</head>
<body>
<div id="apDiv2"><span class="error"><?php echo $userName; ?></span></div>
<div id="apDiv1"><span class="error"><?php echo $row['telno'] ?></span></div>
</body>
</html>
Last html code should be
<html>
<head>
.....
</head>
<body>
...
</body>
</html>
You have put </head>
in <body>
section.
If the database connection is OK, also try changing
while($row=$result->fetch_assoc())
To
while($row=mysql_fetch_assoc($result))
And add
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
at above in php file.
However, this doesn't make PHP show parse errors - the only way to show those errors is to modify your php.ini with this line:
display_errors = on
And check for errors.
Problem solved by member of Stack Overflow just now. Regarding div tags which I posted previously, and the problem was a 2 in 1 coincidently. The person managed to solve everything, the person's name was Svdb.
Below is the code solved by him:
<body>
<div id="apDiv2"><span class="error"><?php echo $userName; ?></span></div>
<div id="apDiv1"><span class="error"><?php echo $row['telno'] ?></span></div>
<?php while($row=$result->fetch_assoc()): ?>
<div id="satu"><?= $row['name'] ?></div>
<div id="dua"><?= $row['username'] ?></div>
<div id="tiga"><?= $row['telno'] ?></div>
<?php endwhile; ?>
</body>
</html>