我有一个html表单,用户需要选择数据库中不能存在的关键字。就像检查域是否存在一样。
如果关键字存在,我想在同一页面上(在输入字段的顶部)回显该关键字是否可用。
修改1
<?php
//Start session
session_start();
//Include database connection details
require_once('config.php');
//Array to store validation errors
$errmsg_arr = array();
//Validation error flag
$errflag = false;
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
//Sanitize the POST values
$email = clean($_POST['email']);
//Input Validations
if($email == '') {
$errmsg_arr[] = 'Email missing';
$errflag = true;
}
//Check for duplicate login ID
if($email != '') {
$qry = "SELECT * FROM members WHERE email='$email'";
$result = mysql_query($qry);
if($result) {
if(mysql_num_rows($result) > 0) {
$errmsg_arr[] = 'Email already in use';
$errflag = true;
}
@mysql_free_result($result);
}
else {
die("Query failed");
}
}
//If there are input validations, redirect back to the registration form
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: register-form.php");
exit();
}
if() {
header("location: samepage.php");
exit();
}else {
die("Query failed");
}
?>
这是html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Login Form</title>
<link href="loginmodule.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
if( isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count($_SESSION['ERRMSG_ARR']) >0 ) {
echo '<ul class="err">';
foreach($_SESSION['ERRMSG_ARR'] as $msg) {
echo '<li>',$msg,'</li>';
}
echo '</ul>';
unset($_SESSION['ERRMSG_ARR']);
}
?>
<form id="loginForm" name="loginForm" method="post" action="register-exec.php">
<table width="300" border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<th>First Name </th>
<td><input name="email" type="text" class="textfield" id="email" /></td>
</tr>
<td><input type="submit" name="Submit" value="Register" /></td>
</tr>
</table>
</form>
</body>
</html>
我不知道在php的最后一个if语句中该怎么做。任何人都可以帮我这个代码吗?
修改2
<?php
//Start session
session_start();
require_once('config.php');
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(count($_POST) > 0){
//Array to store validation errors
$errmsg_arr = array();
//Validation error flag
$errflag = false;
$email = $conn->real_escape_string($_POST['email']);
if($email == '') {
$errmsg_arr[] = 'Email missing';
$errflag = true;
}
$sql = "SELECT * FROM members WHERE email = '$email'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$errmsg_arr[] = 'Email already in use';
$errflag = true;
}
else
{
$errflag = false; // if record not exist.
}
}
$conn->close();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Login Form</title>
<link href="loginmodule.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
if($errflag){
echo implode(",", $errmsg_arr); // if any error found print error array and use your error stuff like redirection as you are using.
}
else{
echo "Available"; // else print Available message or use your stuff.
}
?>
<form id="loginForm" name="loginForm" method="post" action="register-exec.php">
<table width="300" border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<th>Email </th>
<td><input name="email" type="text" class="textfield" id="email" /></td>
</tr>
<td><input type="submit" name="Submit" value="Check" /></td>
</tr>
</table>
</form>
</body>
</html>