Jump to content

MySQL with PHP...


JP128

Recommended Posts

I need some help getting a script that connects to a database(I can get that). But then searches a table for a name and password.

SELECT * FROM table_name WHERE user="$username" AND pass="$password";

I have that part. But now I do not know how to integrate PHP in with it to see if the usrename and pass exists in there.
Link to comment
Share on other sites

if you have a form and you have 2 input elements one named username and one named password, and the user clicks submit, you would do for example:

[code]
//connect to database here

if ($_POST['username'] and $_POST['password']) {
   $username = $_POST['username'];
   $password = $_POST['password'];
   $sql = "select * from table_name where user = '$username' and pass = '$password'";
   $result = mysql_query($sql);
   $is_user = mysql_num_rows ($result);
   if ($is_user > 0) {
     echo "welcome $username";
   } else {
     echo "invalid username or password";
   }
[/code]

that is an extremely simplified working example.
Link to comment
Share on other sites

I know. But I filled out the rest of the code.... See if you can find the error...


<?php
mysql_connect("localhost", "***", "***", "***");
//connect to database here

if ($_POST['username'] and $_POST['password']) {
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "select * from table_name where user = '$username' and password = '$password'";
$result = mysql_query($sql);
$is_user = mysql_num_rows ($result);
if ($is_user > 0) {
echo "welcome $username";
} else {
echo "invalid username or password";
}
}
?>
Link to comment
Share on other sites

everything looks fine to me except you have to change the table name in your sql query

[code]
$sql = "select * from table_name where user = '$username' and password = '$password'";
                              
[/code]

and you have to select a database in your mysql_connect

[code]
$conn=mysql_connect("host", "userid", "password");
if(!mysql_select_db("dbname",$conn))
    die("No database selected.");
[/code]

other then that every thing looks fine
Link to comment
Share on other sites

<?php
mysql_connect("localhost", "user", "pass", "DB_name");
//connect to database here

if ($_POST['username'] and $_POST['password']) {
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "select * from login where user = '$username' and password = '$password'";
$result = mysql_query($sql);
$is_user = mysql_num_rows($result);
if ($is_user > 0) {
echo "welcome $username";
} else {
echo "invalid username or password";
}
}
?>
Link to comment
Share on other sites

this wont work to call your db

[code]
mysql_connect("localhost", "user", "pass", "DB_name");
[/code]
all mysql_connect will log you in
to select a db you have to do something like this
[code]
$conn=mysql_connect("localhost", "userid", "password");
if(!mysql_select_db("dbname",$conn))
    die("No database selected.");
[/code]

Link to comment
Share on other sites

I have connected to databases that way before.

OMG, I figured out what happened!

My script was fine.
I edited my HTML page with the form, saved it, but hit the back button. It didnt refresh the new HTML... That is why.


Sorry for wasting your time.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.