การสร้างระบบสมัครสมาชิกอะค่ะ ทำตามขั้นตอนทุกอย่างเลยแต่พอกรอกข้อมูลลงไป ทำไมไม่เห็นถูกบันทึกในฐานข้อมูลให้เลยขึ้น
ขอดูโค้ดหน่อยสิ
Date :
7 ธ.ค. 2550 13:13:58
By :
saifhon
ลองทำแบบนี้นะ
register.php
<html>
<head></head>
<body>
<title>Register</title>
<font face size="4.5"="MS Sans Serif">
<?
//This function will display the registration form
function register_form(){
echo "ระบบสมัครสมาชิก :: ";
$date = date('D'M,Y');
echo "<form action='?act=register' method='post'>"
."Username <input type='text' name='username' ><br>"
."Password <input type='password' name='password'><br>"
."ชื่อ-สกุล <input type='text' name='name'><br>"
."อีเมล์ <input type='text' name='email'><br>"
."ที่อยู่ <textarea name='address' cols=30 row=4></textarea><br>"
."วันเกิด <input type='text' name='date'><br>"
."เบอร์โทรศัพท์ <input type='text' name='tel'><br>"
."<input type='hidden' name='date' value='$date'><br>
<input type='submit' value='สมัครสมาชิก'>
<input type='reset' value='กรอกข้อมูลใหม่'>
</form>";
}
//This function will register users data
function register(){
//Connecting to database
$connect = mysql_connect("localhost", "root", "");
if(!$connect){
die(mysql_error());
}
//Selecting database
$select_db = mysql_select_db("mydatabase", $connect);
if(!$select_db){
die(mysql_error());
}
//Collecting info
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$address = $_REQUEST['address'];
$date = $_REQUEST['date'];
$tel = $_REQUEST['tel'];
//Here we will check do we have all inputs filled
if(empty($username)){
die("Please enter your username!<br>");
}
if(empty($password)){
die("Please enter your password!<br>");
}
if(empty($name)){
die("Please enter your name!<br>");
}
if(empty($email)){
die("Please enter your email!");
}
if(empty($address)){
die("Please enter your address!<br>");
}
if(empty($tel)){
die("Please enter your telephone!<br>");
}
//Let's check if this username is already in use
$user_check = mysql_query("SELECT username FROM member 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 member 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!<br>");
}
if($do_email_check > 0){
die("Email is already in use!");
}
//If everything is okay let's register this user
$insert = mysql_query("INSERT INTO member (username, password,name,email,address,date,tel) VALUES ('$username', '$password','$name','$email','$address','$date','$tel')");
if(!$insert){
die("There's little problem: ".mysql_error());
}
echo $username.", you are now registered. Thank you!<br><a href=login.php>Login</a> | <a href=index.php>Index</a>";
}
switch($act){
default;
register_form();
break;
case "register";
register();
break;
}
?>
</body>
</html>
อันนี้ login.php
<font face size="2"="MS Sans Serif">
<?php
session_start();
//This displays your login form
function index(){
echo "Login";
echo "<form action='?act=login' method='post'>"
."Username: <input type='text' name='username' size='30'><br>"
."Password: <input type='password' name='password' size='30'><br>"
."<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", "");
if(!$connect){
die(mysql_error());
}
//Selecting database
$select_db = mysql_select_db("mydatabase", $connect);
if(!$select_db){
die(mysql_error());
}
//Find if entered data is correct
$result = mysql_query("SELECT * FROM member WHERE username='$username' AND password='$password'");
$row = mysql_fetch_array($result);
$id = $row['id'];
$select_user = mysql_query("SELECT * FROM member 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 member WHERE username='$username' AND id='$id'");
$row3 = mysql_fetch_array($pass_check);
$email = $row3['email'];
$select_pass = mysql_query("SELECT * FROM member 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 to E-Learning. ".$username." <a href=index.php>Index</a>";
}
switch($act){
default;
index();
break;
case "login";
login();
break;
}
?>
สร้าง Database แบบนี้นะ
#phpMyAdmin MySQL-Dump
#http://phpwizard.net/phpMyAdmin/
#
#Host: localhost Database : mydatabase
# ----------------------------------------------------------
#
#Table structure for table 'member'
#
CREATE TABLE member(
id int(11) NOT NULL auto_increment,
user varchar(15) NOT NULL,
pass varchar(15) NOT NULL,
name varchar(50) NOT NULL,
email varchar(30) NOT NULL,
address varchar(50) NOT NULL,
date varchar(15) NOT NULL,
tel varchar(10) NOT NULL,
PRIMARY KEY (id)
);
#
#Dumping data for table 'member'
#
INSERT INTO member VALUES('1','aom','001','นกแก้ว มาคอร์','[email protected] ','BKK');
INSERT INTO member VALUES('2','may','002','ตุ๊กตา น่ารัก','[email protected] ','BKK');
แล้วลอง login ดูนะจ๊ะ
ไม่เข้าใจตรงไหน เมล์มาถามได้ acyber_g@hotmail........ นะจ๊ะ
Date :
2 ส.ค. 2551 00:25:13
By :
aum
ขอบคุณค่ะ
Date :
2012-02-01 11:48:55
By :
cat
Load balance : Server 01