php - 使用表单将数据插入mysql数据库

标签 php mysql validation insert

我正在尝试使用表单将数据插入“poo12104368”数据库中的“riders”表中。目前,我的“if”语句存在问题,因为它们没有按应有的方式工作。例如,如果用户只需输入姓氏和电子邮件地址,他们就会创建一个帐户。当用户通过在字段中输入正确的详细信息来创建帐户时,应该将他们带到“newaccount.php”。有人可以帮忙吗?谢谢

代码:

$firstnameErr = $lastnameErr = $suemailErr = "";
$firstname = $lastname = $suemail = "";

if(isset($_POST['submit2'])){

if(empty($_POST["firstname"])||(empty($_POST["lastname"]))||(empty($_POST["suemail"]))){
echo "Something is wrong";  

if($_POST['firstname'] == null){
$firstnameErr = "First Name is required";
}else{
$firstname =($_POST["firstname"]);
}

if($_POST['lastname'] == null){
$lastnameErr = "Last Name is required";
}else{
$lastname = ($_POST["lastname"]);
}

if($_POST['suemail'] == null){
$suemailErr = "Email is required";
}else{
$suemail = ($_POST["suemail"]);
}

if($_POST['firstname'] == null){

echo "<b>Please enter a first name</b>";
}

else if($_POST['lastname'] == null){

echo "<b><p>Please enter a last name</p></b>";
}

else if($_POST['suemail'] == null){

echo "<b><p>Please enter an email</p></b>";     
}

$dblink = mysql_connect("localhost", "root", "" )
or die (mysql_error());
mysql_select_db("poo12104368");

// Query the database to see if the email that the user has entered is already in use
$rs2 = mysql_query("SELECT * FROM riders WHERE Email = '".$_POST['suemail']."'");

if($row = mysql_fetch_assoc($rs2)){
$dbEmail = $row['Email'];
if($row['Email'] == $_POST['suemail']){
echo "<p><b>Email already used. Please use another</b></p>";
}
}

else{

    // Insert query to insert the data into the riders table if their data meets the required inputs 
    $sql = "
    INSERT INTO riders (FirstName, LastName, Email) VALUES('".$_POST['firstname']."','".$_POST['lastname']."','".$_POST['suemail']."')";
    mysql_query($sql);

    // The web page that the user will be taken to  
    header('Location:http://localhost/newaccount.php');  

    }   
}   
}
?>
<h2><p> Sign Up </p></h2>   

<p><span class="error">* required field.</span></p>

<!-- Form that the users enters their data in -->
<form name = "suform" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

<p>First Name:<input type="text" name="firstname" style="width:20%"/>
<span class="error">*<?php echo $firstnameErr;?></span></p></br>

<p>Last Name:<input type="text" name="lastname" style="width:20%"/>
<span class="error">*<?php echo $lastnameErr;?></span></p></br>

<p>Email Address:<input type="text" name="suemail" style="width:20%"/></p>
<span class="error">*<?php echo $suemailErr;?></span></br>

<p><br><input type="submit" name="submit2" value="Submit"/></br></p>    


<h2>Our Links</h2>

<!-- Links to the various mediums for Bewdley Motorcycle Club -->
<p>YouTube:<a href="https://www.youtube.com/channel/UC6PVacK6L0rk-WD2wxMXMIA">BewdleyMCCOffcial</a><p>
<p>Website:<a href="http://www.bewdleymotorcycleclub.co.uk/">www.bewdleymotorcycleclub.co.uk</a></p>

最佳答案

试试这个,它会起作用:

使用flag来处理表单中的验证错误,使用此$error作为标志。

代码:

$firstnameErr = $lastnameErr = $suemailErr = "";
$firstname = $lastname = $suemail = "";

if(isset($_POST['submit2'])){

$error = 0;

if(empty($_POST["firstname"])||(empty($_POST["lastname"]))||(empty($_POST["suemail"]))){
$msg  = "something going wrong";
$error = 1;

}


if($_POST['firstname'] == null){
$firstnameErr = "First Name is required";
$error = 1;
}else{
$firstname =($_POST["firstname"]);
}

if($_POST['lastname'] == null){
$lastnameErr = "Last Name is required";
$error = 1;
}else{
$lastname = ($_POST["lastname"]);
}

if($_POST['suemail'] == null){
$suemailErr = "Email is required";
$error = 1;
}else{
$suemail = ($_POST["suemail"]);
}

if($_POST['firstname'] == null){
$msg = "Please enter a first name";
$error = 1;
}

else if($_POST['lastname'] == null){
$msg = "Please enter a last name";
$error = 1;
}

else if($_POST['suemail'] == null){
$msg = "Please enter an email"; 
$error = 1;    
}

if($error == '0')
{

$dblink = mysql_connect("localhost", "root" , "")
or die (mysql_error());
mysql_select_db("poo12104368");

// Query the database to see if the email that the user has entered is already in use
$rs2 = mysql_query("SELECT * FROM riders WHERE Email = '".$_POST['suemail']."'");

if($row = mysql_fetch_assoc($rs2)){
$dbEmail = $row['Email'];
if($row['Email'] == $_POST['suemail']){
echo "<p><b>Email already used. Please use another</b></p>";
}
}

else{

    // Insert query to insert the data into the riders table if their data meets the required standards 
    $sql = "
    INSERT INTO riders (FirstName, LastName, Email) VALUES('".$_POST['firstname']."','".$_POST['lastname']."','".$_POST['suemail']."')";
    mysql_query($sql);

    // The web page that the user will be taken to  
    header('Location:http://localhost/newaccount.php');  

    }   
}   
else
{
  echo $msg;
}
?>
<h2><p> Sign Up </p></h2>   

<p><span class="error">* required field.</span></p>

<!-- Form that the users enters their data in -->
<form name = "suform" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

<p>First Name:<input type="text" name="firstname" style="width:20%"/>
<span class="error">*<?php echo $firstnameErr;?></span></p></br>

<p>Last Name:<input type="text" name="lastname" style="width:20%"/>
<span class="error">*<?php echo $lastnameErr;?></span></p></br>

<p>Email Address:<input type="text" name="suemail" style="width:20%"/></p>
<span class="error">*<?php echo $suemailErr;?></span></br>

<p><br><input type="submit" name="submit2" value="Submit"/></br></p>    


<h2>Our Links</h2>

<!-- Links to the various mediums for Bewdley Motorcycle Club -->
<p>YouTube:<a href="https://www.youtube.com/channel/UC6PVacK6L0rk-WD2wxMXMIA">BewdleyMCCOffcial</a><p>
<p>Website:<a href="http://www.bewdleymotorcycleclub.co.uk/">www.bewdleymotorcycleclub.co.uk</a></p>

希望它对你有用。

关于php - 使用表单将数据插入mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28251235/

相关文章:

php - Webkit 转换使用 jquery

mysql - 将 MySQL 查询转换为 MS Access

mysql - SQL 根据另一表列条件更新一个表列

javascript - 如何使用 AM/PM Javascript 验证结束时间和开始时间与日期解析?

php - PHP 中的 POST 和原始 POST 有什么区别?

php - 重力形式创建唯一 ID

phpunit mock 不计算函数调用

PHP:将相当于用户在表单选择中的选择的数据库插入到变量中?

javascript - 验证不使用 INTL-TEL-INPUT

validation - 要求 joi 对象中至少有一个允许 null 的非 null 子项