Hey guys, so I am trying to convert my site to MySQL, but I cant find a tutorial that shows both adding tables and deleting tables and editing tables, and the content within, AND accessing them with PHP.
I looked at the tutorial in the tutorials section by jordan and its nice but how do I make those tables? The tutorials I end up finding are for either the older versions or I get errors.
If I can just get some complete code to read and play with to learn from then it would help me a great deal. All I want to do is create User accounts with password and usernames, and then some extra content (signatures, permissions, status, etc)
Can anyone help me out here?
Php and MySQL help?
Started by phpforfun, Mar 04 2008 11:40 AM
14 replies to this topic
#1
Posted 04 March 2008 - 11:40 AM
|
|
|
#2
Posted 04 March 2008 - 12:13 PM
Have you looked at this tutorial? http://forum.codecal...off-system.html
#3
Posted 04 March 2008 - 04:52 PM
ok so I tried that, and it *kinda* works.
It lets me make the account, but when I go to login, it says "Username is wrong!"
But here it is.
http://ffe.no-ip.org/db.jpg
It lets me make the account, but when I go to login, it says "Username is wrong!"
But here it is.
http://ffe.no-ip.org/db.jpg
#4
Posted 04 March 2008 - 04:55 PM
Here is the code im using, its edited.
register.php
login.php
register.php
<?php
//This function will display the registration form
function register_form(){
$date = date('D, M, Y');
echo "<form action='?act=register' method='post'>"
."Username: <input type='text' name='username' size='30'>"
."Password: <input type='password' name='password' size='30'>"
."Confirm your password: <input type='password' name='password_conf' size='30'>"
."Email: <input type='text' name='email' size='30'>"
."<input type='hidden' name='date' value='$date'>"
."<input type='submit' value='Register'>"
."</form>";
}
//This function will register users data
function register(){
//Connecting to database
$connect = mysql_connect("localhost", "root", "private1");
if(!$connect){
die(mysql_error());
}
//Selecting database
$select_db = mysql_select_db("users", $connect);
if(!$select_db){
die(mysql_error());
}
//Collecting info
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$pass_conf = $_REQUEST['password_conf'];
$email = $_REQUEST['email'];
$date = $_REQUEST['date'];
//Here we will check do we have all inputs filled
if(empty($username)){
die("Please enter your username!");
}
if(empty($password)){
die("Please enter your password!");
}
if(empty($pass_conf)){
die("Please confirm your password!");
}
if(empty($email)){
die("Please enter your email!");
}
//Let's check if this username is already in use
$user_check = mysql_query("SELECT username FROM users WHERE username='$username'");
$do_user_check = mysql_num_rows($user_check);
//Now if email is already in use
$email_check = mysql_query("SELECT email FROM users WHERE email='$email'");
$do_email_check = mysql_num_rows($email_check);
//Now display errors
if($do_user_check > 0){
die("Username is already in use!");
}
if($do_email_check > 0){
die("Email is already in use!");
}
//Now let's check does passwords match
if($password != $pass_conf){
die("Passwords don't match!");
}
//If everything is okay let's register this user
$insert = mysql_query("INSERT INTO accounts (username, password, email) VALUES ('$username', '$password', '$email')");
if(!$insert){
die("There's little problem: ".mysql_error());
}
echo $username.", you are now registered. Thank you!<a href=login.php>Login</a> | <a href=index.php>Index</a>";
}
switch($act){
default;
register_form();
break;
case "register";
register();
break;
}
?>
login.php
<?php
session_start();
//This displays your login form
function index(){
echo "<form action='?act=login' method='post'>"
."Username: <input type='text' name='username' size='30'>"
."Password: <input type='password' name='password' size='30'>"
."<input type='submit' value='Login'>"
."</form>";
}
//This function will find and checks if your data is correct
function login(){
//Collect your info from login form
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
//Connecting to database
$connect = mysql_connect("localhost", "root", "private1");
if(!$connect){
die(mysql_error());
}
//Selecting database
$select_db = mysql_select_db("users", $connect);
if(!$select_db){
die(mysql_error());
}
//Find if entered data is correct
$result = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'");
$row = mysql_fetch_array($result);
$id = $row['id'];
$select_user = mysql_query("SELECT * FROM users WHERE id='$id'");
$row2 = mysql_fetch_array($select_user);
$user = $row2['username'];
if($username != $user){
die("Username is wrong!");
}
$pass_check = mysql_query("SELECT * FROM accounts WHERE username='$username' AND id='$id'");
$row3 = mysql_fetch_array($pass_check);
$email = $row3['email'];
$select_pass = mysql_query("SELECT * FROM accounts WHERE username='$username' AND id='$id' AND email='$email'");
$row4 = mysql_fetch_array($select_pass);
$real_password = $row4['password'];
if($password != $real_password){
die("Your password is wrong!");
}
//Now if everything is correct let's finish his/her/its login
session_register("username", $username);
session_register("password", $password);
echo "Welcome, ".$username." please continue on our <a href=index.php>Index</a>";
}
switch($act){
default;
index();
break;
case "login";
login();
break;
}
?>
#5
Posted 04 March 2008 - 06:33 PM
You should really develop a procedure to debug your own code. For example:
"I'm getting the error 'Username is wrong!' I wonder why it isn't working?"
*looks through the code*
Oh here is where the error is being thrown:
hmm...that must mean $username does not equal $user.
*goes and checks what $user is and $username is directly prior to this statement, therefore I can determine which value is wrong, and continue debugging*
"I'm getting the error 'Username is wrong!' I wonder why it isn't working?"
*looks through the code*
Oh here is where the error is being thrown:
if($username != $user){
die("Username is wrong!");
}
hmm...that must mean $username does not equal $user.
*goes and checks what $user is and $username is directly prior to this statement, therefore I can determine which value is wrong, and continue debugging*
#6
Posted 05 March 2008 - 05:48 PM
I got it! Had to do a bit of tweaking, the issue was it was throwing in an extra table before ID, this it was looking at the wrong column for the username.
Im did however add an extra value, the column is "type" and the value is "user". Im trying to echo that as a variable so I can use it in the code for permissions.
That code selects the column "type", but how do I make it select the row of the user?
Thanks for the patience and help
Im did however add an extra value, the column is "type" and the value is "user". Im trying to echo that as a variable so I can use it in the code for permissions.
$connect = mysql_connect("localhost", "root", "password");
if(!$connect){
die(mysql_error());
}
//Selecting database
$select_db = mysql_select_db("users", $connect);
if(!$select_db){
die(mysql_error());
}
$chktype = mysql_query("SELECT * FROM accounts WHERE type='$type'");
$row5 = mysql_fetch_array($chktype);
$type= $row['type'];
That code selects the column "type", but how do I make it select the row of the user?
Thanks for the patience and help
#7
Posted 05 March 2008 - 08:13 PM
oh wow, nvm, I got it, this is awesome..
<?php
//This will start a session
session_start();
$username = $_SESSION['username'];
$password = $_SESSION['password'];
//Check do we have username and password
if(!$username && !$password){
echo "Welcome Guest! <a href=login.php>Login</a> | <a href=register.php>Register</a>";
} else {
//Connecting to database
$connect = mysql_connect("localhost", "root", "password");
if(!$connect){
die(mysql_error());
}
//Selecting database
$select_db = mysql_select_db("users", $connect);
if(!$select_db){
die(mysql_error());
}
$result = mysql_query("SELECT * FROM accounts WHERE username='$username' AND password='$password'");
$row5 = mysql_fetch_array($result);
$type = $row5['type'];
echo "Welcome ".$username." (<a href=logout.php>Logout</a>) You are a $type";
}
?>
#8
Guest_Jordan_*
Posted 06 March 2008 - 06:09 AM
Guest_Jordan_*
Good work! Debugging code can be a very time consuming task.
#9
Posted 06 March 2008 - 08:39 AM
Thanks, this is weird though, im going to figure it out, but I put all the login code and logout code on a different page (a real page on my site, not a testing page), and I cant login, it wont let me, it will say I did but it wont keep the session, but when I go to the test pages I made, it wont let me login till I close the browser and login using that first.
That means that its not keeping the right data for the session in the real page I made. weird
That means that its not keeping the right data for the session in the real page I made. weird
#10
Posted 06 March 2008 - 10:20 AM
ok quick question.
I left the code in the login.php file the same, did not change it, so it is looking at row2 for the username..
however I added like 10 rows in the database, but left rows 1 2 3 and 4 the same. So it should still be looking at row2 for the username.
however now when I go to login, it says username is wrong, any idea?
I left the code in the login.php file the same, did not change it, so it is looking at row2 for the username..
however I added like 10 rows in the database, but left rows 1 2 3 and 4 the same. So it should still be looking at row2 for the username.
however now when I go to login, it says username is wrong, any idea?
#11
Posted 06 March 2008 - 10:24 AM
I know you said try to debug my own code, and im trying, but I think this has something to do with the DB, not the code, and I cant get it.. See it should work, i would think..
Image:

Code:
Image:

Code:
//Selecting database
$select_db = mysql_select_db("users", $connect);
if(!$select_db){
die(mysql_error());
}
//Find if entered data is correct
$result = mysql_query("SELECT * FROM accounts WHERE username='$username' AND password='$password'");
$row = mysql_fetch_array($result);
$id = $row['id'];
$select_user = mysql_query("SELECT * FROM accounts WHERE id='$id'");
$row2 = mysql_fetch_array($select_user);
$user = $row2['username'];
if($username != $user){
die("Username is wrong!");
}
#12
Posted 06 March 2008 - 01:44 PM
ok I figured it out, thats weird, its because of the password somehow..
Edit: I think there is a flaw in the tutorial code
Edit: I think there is a flaw in the tutorial code


Sign In
Create Account


Back to top









