doudiecai1572 2013-07-09 18:01
浏览 61
已采纳

使用PHP将数据添加到SQL表以及如何显示错误

Updated Problem: I'm trying to use PHP to add information into a SQL table if there is no errors, but I am not sure how to get it to fully working so that it'll show the errors and such. Where do I put the PHP code? Also where what language would I use to display the errors?

Updated 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>

Updated 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"
}
?>
  • 写回答

6条回答 默认 最新

  • dongzhi1949 2013-07-09 20:38
    关注

    Your code is structured very strangely, and I suspect you're confused about what you should use to do what. Why are you sticking your PHP at the bottom of the page, after all the HTML? That, and the way you talk about your PHP code "getting triggered" in the comments makes me wonder if you understand what exactly PHP does. PHP is a server-side tool for building HTML dynamically. By the time your user sees the page, all your PHP has finished running and can't do anything more until they load the next page. I don't know exactly what you're trying to do here, but putting PHP after all your HTML is never the right solution.

    If you're putting data into a form, and then reloading the same page with the form data, what you should do is put the bulk of your PHP at the top of the page, and then put little bits of PHP interspersed throughout your HTML wherever you want something dynamic. Javascript shouldn't be needed unless you're doing something fancy.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b