[help] User Login

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
painful-one
Forum Newbie
Posts: 11
Joined: Tue Jan 05, 2010 11:26 am

[help] User Login

Post by painful-one »

I have a php script that allows someone to log into my website (provided an account is set up in the MySQL DB). My question is, if they fill out a form to change the data in the DB does it automatically change the information in the section of the table that person logged in to? Here is the script that the user uses to log in. When successful it goes to a Login Success page. I tested the passwords and when the wrong one is entered it gives the error.



<?php
$host="fohusers.db.4742196.hostedresource.com"; // Host name
$username=" xxx "; // Mysql username
$password=" xxx "; // Mysql password
$db_name=" xxx "; // Database name
$tbl_name=" xxx "; // 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";
}
?>
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: [help] User Login

Post by Weiry »

painful-one wrote:My question is, if they fill out a form to change the data in the DB does it automatically change the information in the section of the table that person logged in to?
If by that you mean if they submit some information to modify the table which would result in a query like
[sql]UPDATE `users` SET `username` = 'blah' WHERE `username` = 'roccat'[/sql]
Then yes it would automatically update the moment the query is executed by the server.
painful-one
Forum Newbie
Posts: 11
Joined: Tue Jan 05, 2010 11:26 am

Re: [help] User Login

Post by painful-one »

So my question is, if you don't mind helping - if I wanted them to submit information from a form (I have the functions for it already) to change a section labeled 'level' would this be correct?

$level=$_POST['level'];
UPDATE `members` SET `level` = $level WHERE `username` = 'myusername'; // myusername would be the name in the username field and members would be the table. If I can't use $level where I put it, how would I use the 'POST'ed data?

Just an example I am asking about, I know it's not fully correct. Forgive me, I am new, even so I love to learn.
painful-one
Forum Newbie
Posts: 11
Joined: Tue Jan 05, 2010 11:26 am

Re: [help] User Login

Post by painful-one »

I've been messing with this and still can't seem to get it to work? Can anyone help?
Post Reply