【问题标题】:Inserting data into a mysql database using a form使用表单将数据插入 mysql 数据库
【发布时间】:2015-01-31 11:48:08
【问题描述】:

我正在尝试使用表单将数据插入到“poo12104368”数据库中的“车手”表中。目前,我的“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>

【问题讨论】:

    标签: php mysql validation insert


    【解决方案1】:

    试试这个吧:

    使用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>
    

    我希望它对你有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-12
      • 1970-01-01
      • 1970-01-01
      • 2018-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多