Straight to the point...:)
I have problem connect to database. when I click Login button, the code can't read the username in my database.
I have checked the codes but i think there are some mistakes, but i don't know where it will be. Please help me. Here with I attach my login form and connection form...
Please help me soon. Thanks...
bintang 0 Newbie Poster
<html>
<head>
<title>AUTOMASI BISNIS PROSES SARANA dan UTILITAS - POLITKNIK BATAM</title>
<style type="text/css">
<p align="center"><b><font size="5">AUTOMASI BISNIS PROSES SARANA dan UTILITAS</font></b></p>>
<!--
INPUT
{
background: #ffffff;
border-color: #777777;
border-style: solid;
border-width: 1px;
color: #333333;
font-size: 13px;
}
-->
</style>
</head>
<body alink="#000000" bgcolor="gray" link="#000000" text="#000000" vLink="#000000">
<!-- start title -->
<table border="1" borderColor="#000000" cellpadding="0" cellspacing="0" height="11%" width="100%">
<tr>
<td align="left" bgcolor="#e5e5e5"><br>
</td>
</tr>
</table>
<b><? print(Date("l F d, Y")); ?></b>
<!-- end title -->
<!-- start main -->
<table border="1" bordercolor="#000000" cellpadding="0" cellspacing="0" height="86%" width="100%">
<tr>
<td bgcolor="#e5e5e5" valign="top">
<p align="center"> </p>
<p align="center"> </p>
<p align="center"><b><font size="5" face="Georgia">AUTOMASI BISNIS PROSES
SARANA DAN UTILITAS</font></b></p>
<p align="center"> </p>
<p align="center"> </p>
<table border="1" bordercolor="#000000" cellpadding="0" cellspacing="0" height="150" width="375" align="center">
<tr>
<td height="20" align="center" bgcolor="#000000">
<font size="4" color="#ffffff">Admin Login</font><font size="4"> </font>
</td>
</tr>
<tr>
<td valign="top">
<form action="submit.php" method="post"><br>
<table border="0" cellpadding="3" cellspacing="0" align="center" width="275">
<tr>
<td>
<b><font size="4">Username :</font></b><font size="4"> </font>
</td>
<td>
<font size="4">
<input type="text" name="username" size="21">
</font>
</td>
</tr>
<tr>
<td>
<b><font size="4">Password :</font></b><font size="4"> </font>
</td>
<td>
<font size="4">
<input type="password" name="password" size="21">
</font>
</td>
</tr>
<tr>
<td colspan="2">
<center><font size="4"><input type="submit" name=login value="Submit "></font>
<font face="Arial"><left><font size="4"><input type="reset" name="reset" value="Reset ">
</font></font></center>
<font face="Arial">
<left></center>
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td bgcolor="#e5e5e5" valign="top"> </td>
</tr>
</table>
</body>
</html> <?php
$connected = mysql_connect('localhost', 'root');
if( !connected ) {
die ("Connect to MySQL Server failed");
}
$retval = mysql_select_db("sarana", $connected);
if (!retval) {
die ("Database doesn't connect");
}
?> <html>
<head>
<title>AUTOMASI BISNIS PROSES SARANA dan UTILITAS - POLITKNIK BATAM</title>
<style type="text/css">
<p align="center"><b><font size="5">AUTOMASI BISNIS PROSES SARANA dan UTILITAS</font></b></p>
<!--
INPUT
{
background: #ffffff;
border-color: #777777;
border-style: solid;
border-width: 1px;
color: #333333;
font-size: 13px;
}
-->
</style>
</head>
<body alink="#000000" bgcolor="gray" link="#000000" text="#000000" vLink="#000000">
<!-- start title -->
<table border="1" borderColor="#000000" cellpadding="0" cellspacing="0" height="11%" width="100%">
<tr>
<td align="left" bgcolor="#e5e5e5"><br>
</td>
</tr>
</table>
<b><? print(Date("l F d, Y")); ?></b>
<!-- end title -->
<!-- start main -->
<br>
<br>
<br>
<table border="1" bordercolor="#000000" cellpadding="0" cellspacing="0" height="86%" width="100%">
<body>
<?php
include ("koneksi.php");
$username = $HTTP_POST_VARS['username'];
$password = $HTTP_POST_VARS['password'];
$query = "SELECT username FROM sarana WHERE
username = '$username' AND password = '$password'";
$DEBUG = false;
if($DEBUG)
{
print $query . "<br>\n";
}
else{
$rs = mysql_query($query);
gettype($rs);
if($rs)
{
$num = mysql_num_rows($rs);
if($num > 0)
{
$row = mysql_fetch_object($rs);
$nr = $row->username;
print "<SCRIPT LANGUAGE=JAVASCRIPT>\n";
print "window.location = 'menu.php'\n";
print "</SCRIPT>";
}
else
{
print "Your Username does not exist<br>";
print "<a href=form_login.php>Try again, please</a>";
}
}
else
{
print "Data Failed to be read\n<br><br><br>";
print "<a href=form_login.php>Try again, please</a>";
}
}
mysql_close();
?>
</table>
</body>
</html> Dani AI
Generated
Short summary and extra checks based on this thread: reported that the login form could not read the username; diagnosed the connection call as the immediate issue and that resolved the problem. If similar symptoms appear again, the following targeted checks and a modern login pattern will help avoid the same trap and make the code safer for later PHP versions.
- Turn on error reporting while debugging so connection errors are visible (temporarily).
- Verify the MySQL server is reachable with the same credentials from the command line or a DB client.
- Confirm the database and table names match exactly (case matters on some systems) and that the DB user has SELECT rights.
- Check the HTML form: ensure input
nameattributes match the keys used in the script and the formmethod(POST vs GET) is correct. - Ensure
session_start()is called before using session variables and trim inputs (empty spaces often hide “missing” usernames). - Be aware of character-set mismatches: store/compare values using the same encoding (utf8mb4 recommended).
Prefer using PDO or MySQLi with prepared statements and PHP’s password hashing functions instead of the old mysql_* calls. A minimal PDO pattern looks like this:
try {
$pdo = new PDO('mysql:host=localhost;dbname=sarana;charset=utf8mb4','appuser','secret');
$stmt = $pdo->prepare('SELECT password FROM users WHERE username = ? LIMIT 1');
$stmt->execute([$username]);
$hash = $stmt->fetchColumn();
if ($hash && password_verify($password, $hash)) {
// authenticated
} else {
// reject
}
} catch (PDOException $e) {
error_log($e->getMessage());
} Avoid using the database root account in application code; create a least-privilege user instead. For reference on prepared statements and password handling see the PHP manual: PDO prepared statements and password_hash()/password_verify().
Recommended Answers
Jump to Post— effu 13original koneksi.php
$connected = mysql_connect('localhost', 'root');
if( !connected ) {
die ("Connect to MySQL Server failed");
}$retval = mysql_select_db("sarana", $connected);
if (!retval) {
die ("Database doesn't connect");
}A couple changes are needed in your koneksi.php:
$connected = mysql_connect('localhost', 'root', 'password'); if( !$connected ) { die …
All 2 Replies
effu 13 Junior Poster in Training
original koneksi.php
$connected = mysql_connect('localhost', 'root');
if( !connected ) {
die ("Connect to MySQL Server failed");
}$retval = mysql_select_db("sarana", $connected);
if (!retval) {
die ("Database doesn't connect");
}
A couple changes are needed in your koneksi.php:
$connected = mysql_connect('localhost', 'root', 'password');
if( !$connected ) {
die ("Connect to MySQL Server failed");
}
$retval = mysql_select_db("sarana", $connected);
if (!$retval) {
die ("Database doesn't connect");
} bintang 0 Newbie Poster
Thanks a lot for yor kind reply. I am satisfied. Once again, thank you...:)
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.