使用PHP将表单数据添加到SQL表并具有错误消息

时间:2013-07-09 23:57:28

标签: php html sql forms

如果没有错误,我正在尝试使用PHP将信息添加到SQL表中,但我不确定如何使其完全正常工作以便它显示错误等。我在哪里放PHP代码?还有哪种语言用于显示错误?主要是,我如何实现这一切?

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>
<title>TheQuantumBros</title>
<meta http-equiv="Content-Language" content="en-us" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="MainStyles.css" />
<link rel="shortcut icon" href="favicon.ico" />
<style type="text/css">
.style2 {
font-size: xx-large;
color: white;
}
.style3 {
font-size: xx-large;
color: #008000;
}
</style>
</head>

<body style=";background-image:url('bg_wallpaper_black.jpg');margin:0;padding-top:0;">
<div id="topBar" class="topBar">
    <h1 id="title" class="title">
        <img src="TheQuantumBros3.png" alt="" style="padding-right:5px" />
        TheQuantumBros
        <img src="TheQuantumBros3.png" alt="" style="padding-left:5px"/>
    </h1>
</div>

<div id="topDiv" class="topDiv" style="height:200px">
    <div id="topDivFrame" class="topDivFrame" style="background-image:url('TheQuantumBros2.png');height:200px"></div>
</div>

<div id="menuBar" class="menuBar">
    <button name="homeButton" class="menuButton1" onclick="javascript:window.location.href='http://www.tqbtest.comlu.com/'">Home</button>
    <button name="forumsButton" class="menuButton2">Forums</button>
    <button name="bfButton" class="menuButton2">BF P4F</button>
    <button name="mcButton" class="menuButton2">Minecraft</button>
    <button name="applyButton" class="menuButton2">Applications</button>
    <button name="infoButton" class="menuButton2">About Us</button>
</div>
<div style="width:100%;height:150px;margin-top: 5px;margin-left:auto;margin-right:auto;text-align:center">
    <div style="width:75%;height:100%;text-align:center">
        <span style="width:75%;height:10%;margin-right:15%;margin-left:45%;text-align:center" class="style2">TheQuantumBros Registration</span>
        <form method="post" action="register.php" style="height:100%;width:100%;text-align:center">

            <span class="registrationText">First Name:</span>
            <input name="firstname" type="text" class="registrationInput"/>

            <span class="registrationText">Last Name:</span>
            <input name="lastname" type="text" class="registrationInput"/>

            <span class="registrationText">Username:</span>
            <input name="username" type="text" class="registrationInput"/>

            <span class="registrationText">Email</span>
            <input name="email" type="text" class="registrationInput"/>

            <span class="registrationText">Password</span>
            <input name="password" type="password" class="registrationInput"/>

            <span class="registrationText">Retype Password:</span>
            <input name="password2" type="password" class="registrationInput"/>

            <span class="registrationText">Region</span>
            <select name="region" class="registrationInput">
                <option>North America</option>
                <option>South America</option>
                <option>Europe</option>
                <option>Asia</option>
                <option>Africa</option>
                <option>Australia</option>
            </select>

            <span style="width:25%"></span>
            <input name="Submit1" type="submit" value="Submit" class="registrationSubmit"/>
            <span class="notFilled" id="notFilled">*Please fill in all the fields.</span>
        </form>
    </div>
</div>

PHP:     

    $sql="SELECT username, email FROM Profiles";
    $result=mysqli_query($con,$sql);

    if($result === false){
        echo mysqli_error($con);
    }else{
        while($row = mysqli_fetch_array($result)){
            $rows[] = $row;

            foreach ($rows as $row) {

                if($row['username'] == $_POST['username'] || $row['email'] == $_POST['email']){
                   //Error: "Username or Email already in use"
                }else {
                   if($_POST['password'] == $_POST['password2']){
                       if (strpos($_POST['email'],'@') !== false) {
                           //INSERT INTO code?
                       }else{
                          //Error: "Please enter a valid email."
                       }
                   }else{
                      //Error: "Passwords do not match."
                   }
                }
            }
        mysqli_close($con);
        }
    }
}else{
   //Error code: "Please fill in all fields"
}
?>

1 个答案:

答案 0 :(得分:0)

您可以针对每个错误执行以下操作并将其打印出来。只需将错误放入array

$errors[] = "First error.";
$errors[] = "And so on...";

  foreach($errors as $error) {

          echo $error;

  }