我在数据库中制作了两个表,一个用于登录和注册客户,另一个用于登录和注册经销商。只有客户表正在工作。但是,如果是经销商,数据不会保存到表格中。 这是数据库代码
<?php
session_start();
// variable declaration
$username = "";
$email = "";
$errors = array();
$_SESSION['success'] = "";
$db = mysqli_connect('localhost', 'root', '', 'registration');
// SIGNUP CUSTOMER
if (isset($_POST['reg_user'])) {
$username = mysqli_real_escape_string($db, $_POST['username']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
$password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
$telephone = mysqli_real_escape_string($db,$_POST['telephone']);
$country = mysqli_real_escape_string($db,$_POST['country']);
$state= mysqli_real_escape_string($db,$_POST['state']);
if (empty($username)) { array_push($errors, "Username is required"); }
if (empty($email)) { array_push($errors, "Email is required"); }
if (empty($password_1)) { array_push($errors, "Password is required"); }
if (empty($telephone)) { array_push($errors, "Telephone no. is required"); }
if (empty($country)) { array_push($errors, "country is required"); }
if (empty($state)) { array_push($errors, "state is required"); }
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
}
if (count($errors) == 0) {
$password = md5($password_1);
$query = "INSERT INTO users (username, email, password, telephone,country,state)
VALUES('$username', '$email', '$password','$telephone','$country','$state')";
mysqli_query($db, $query);
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: index1.php');
}
}
// ...
// SIGNIN CUSTOMER
if (isset($_POST['login_user'])) {
$username = mysqli_real_escape_string($db, $_POST['username']);
$password = mysqli_real_escape_string($db, $_POST['password']);
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
if (count($errors) == 0) {
$password = md5($password);
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1) {
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: index1.php');
}else {
array_push($errors, "Wrong username/password combination");
}
}
}
//SIGN UP DEALER
if (isset($_POST['reg_dealer'])) {
$username = mysqli_real_escape_string($db, $_POST['username']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
$password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
$account = mysqli_real_escape_string($db, $_POST['account']);
$IFSC = mysqli_real_escape_string($db,$_POST['IFSC']);
$target = mysqli_real_escape_string($db,$_POST['target']);
$sales= mysqli_real_escape_string($db,$_POST['sales']);
$date= mysqli_real_escape_string($db,$_POST['date']);
$address= mysqli_real_escape_string($db,$_POST['address']);
$telephone = mysqli_real_escape_string($db,$_POST['telephone']);
$country = mysqli_real_escape_string($db,$_POST['country']);
$state= mysqli_real_escape_string($db,$_POST['state']);
if (empty($username)) { array_push($errors, "Username is required"); }
if (empty($email)) { array_push($errors, "Email is required"); }
if (empty($password_1)) { array_push($errors, "Password is required"); }
if (empty($telephone)) { array_push($errors, "Telephone no. is required"); }
if (empty($country)) { array_push($errors, "country is required"); }
if (empty($state)) { array_push($errors, "state is required"); }
if (empty($account)) { array_push($errors, "account is required"); }
if (empty($IFSC)) { array_push($errors, "IFSC is required"); }
if (empty($sales)) { array_push($errors, "sales is required"); }
if (empty($date)) { array_push($errors, "date is required"); }
if (empty($target)) { array_push($errors, "target is required"); }
if (empty($address)) { array_push($errors, "address is required"); }
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
}
if (count($errors) == 0) {
$password = md5($password_1);
$query = "INSERT INTO dealers (username, email, password, account,IFSC,target,sales, date,address,telephone,country,state)
VALUES('$username', '$email', '$password','$account','$IFSC','$target','$sales','$date','$address',$telephone','$country','$state')";
mysqli_query($db, $query);
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: index1.php');
}
}
//SIGNIN DEALER
if (isset($_POST['login_dealer'])) {
$username = mysqli_real_escape_string($db, $_POST['username']);
$password = mysqli_real_escape_string($db, $_POST['password']);
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
if (count($errors) == 0) {
$password = md5($password);
$query = "SELECT * FROM dealers WHERE username='$username' AND password='$password'";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1) {
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: index1.php');
}else {
array_push($errors, "Wrong username/password combination");
}
}
}
?>
reg_user是用于注册客户的按钮的名称 login_user是用于登录客户的按钮的名称 reg_dealer是用于注册经销商的按钮的名称 login_dealer是用于登录经销商的按钮的名称
答案 0 :(得分:0)
你正在犯小错误
试试这个会对你有用。
我还建议您在表格中插入数据时使用prepare语句。
INSERT INTO dealers (username, email, password, account,IFSC,target,sales, date,address,telephone,country,state)
VALUES('$username', '$email', '$password','$account','$IFSC','$target','$sales','$date','$address','$telephone','$country','$state')";