On my 000webhost website I had created this SQL file, named test.sql
Here is it’s coding:
CREATE TABLE `members` (
`id` int(4) NOT NULL auto_increment,
`username` varchar(65) NOT NULL default '',
`password` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=2 ;
--
-- Dumping data for table `members`
--
INSERT INTO `members` VALUES (1, 'john', '1234');
And after that I created another php file named main_login.php
Here is coding:
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
After that I prepared another php file named checklogin.php
Here is coding:
$host="I INSERTED MY LOCAL HOST HERE"; // Host name
$username="I INSERTED MY USERNAME HERE"; // Mysql username
$password="I TYPED MY PASSWORD HERE"; // Mysql password
$db_name="[B]I AM CONFUSED THAT WHICH DATABASE SHOULD I NAME HERE[/B] :eek: "; // Database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
In checklogin.php, 4th line, I am confused that which database name should I provide. I tried to write test.sql as I had created a table with SQL file, but while logging in it’s showing error and login form is not able to fetch the SQL table data.I had already created a mySQL file in service provided by 000webhost.com and get MySQL username, MySQL database and host site (let username be SQL_user and site), but currently don’t know how to use MySQL. So which file should I name in $db_name above? And also want to know that how can I add data in MySQL in 000webhost.com so that login form can fetch the login info?
Thank You in advance…
Above I had asked 2 Questions:
Que 1. So which file should I name in
$db_nameabove?Que 2. How can I add data in MySQL in 000webhost.com so that login form can fetch the login info?
Here are answers:
Ans 1. Here in the checklogin.php I had to write this:
Now in above at
$db_namewe will be your MySQL database name, and I will have to create another table at that database.Ans 2. After opening my site Control Panel at 000webhost I will have to go on phpMyAdmin and create a database first and after putting the username, password, local host etc., I will click on “Enter phpMyAdmin” at the right side of the table it will take us to another window and there we have to create the database and this database will be the place from where the login form will fetch data.