1

我创建了一个注册页面,它正在工作,但我有一个问题。

该页面包含用户名、密码、电子邮件、地址和电话。根据我所达到的,用户可以添加所有这些要求并且只能添加“用户名”才能成功注册我需要一种方法让用户添加所有这些要求(用户名、密码、电子邮件、地址和电话)不仅添加 1 个要求。

 <?php
 $con = mysql_connect("localhost","root","");
 if (!$con)
 {
 die('Could not connect: ' . mysql_error());
 }

 mysql_select_db("theater", $con);

 $sql="INSERT INTO member (username, password, email, telephone, address)
 VALUES
    ('$_POST[username]','$_POST[password]','$_POST[email]','$_POST[telephone]','$_POST[address]')";

 if (!mysql_query($sql,$con))
 {
 die('Error: ' . mysql_error());
 }
 echo "records added";

 mysql_close($con);
 ?>

signup.php页面是一个普通表格,其中包含输入用户名、电子邮件、密码、地址和带有注册按钮的电话。我认为我们不应该在这里使用它,不是吗?

4

2 回答 2

1

您可以检查是否设置了特定项目,如果是,请继续。

$errors = array(); //Initialize an empty errors array.

if(!isset($_POST["username"]){
  $errors[] = "Username not set; please try again.";
}

if(!isset($_POST["email"]){
  $errors[] = "Email address not set; please try again.";
}

//If there are no errors in the $errors array (aka: every field is set):
if(empty($errors)) {
  //Query the database and whatnot.
} else {
  //Echo out the errors and demand proper values.
}
于 2013-02-03T15:52:15.957 回答
0

您应该尝试这种方式来检查是否所有表单域都已填写。

<?php
 $con = mysql_connect("localhost","root","");
 if (!$con)
 {
 die('Could not connect: ' . mysql_error());
 }

 mysql_select_db("theater", $con);

if($_POST['username'] == '' || $_POST['password'] == '' || $_POST['email'] == '' || $_POST['telephone'] == '' || $_POST['address'] == '') $filled = false;
if($filled) {
 $sql="INSERT INTO member (username, password, email, telephone, address)
 VALUES
    ('$_POST[username]','$_POST[password]','$_POST[email]','$_POST[telephone]','$_POST[address]')"; } else echo 'Please fill all the fields!';

 if (!mysql_query($sql,$con))
 {
 die('Error: ' . mysql_error());
 }
 echo "records added";

 mysql_close($con);
 ?>
于 2013-02-03T15:52:24.027 回答