php - 使用 PHP 向 SQL 表添加数据以及如何显示错误

标签 php javascript jquery html css

更新问题: 如果没有错误,我正在尝试使用 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"
}
?>

最佳答案

您的代码结构非常奇怪,我怀疑您对应该使用什么来做什么感到困惑。为什么在所有 HTML 之后将 PHP 放在页面底部?那,以及你在评论中谈论你的 PHP 代码“被触发”的方式让我想知道你是否理解 PHP 到底做了什么。 PHP 是用于动态构建 HTML 的服务器端工具。当您的用户看到该页面时,您所有的 PHP 都已完成运行并且在他们加载下一页之前无法执行任何操作。我不确切知道您要在这里做什么,但是将 PHP 放在所有 HTML 之后绝不是正确的解决方案。

如果您要将数据放入表单,然后使用表单数据重新加载同一页面,您应该做的是将大部分 PHP 放在页面的顶部,并且然后将少量 PHP 散布在整个 HTML 中,无论您需要什么动态内容。除非您正在做一些奇特的事情,否则不需要 Javascript。

关于php - 使用 PHP 向 SQL 表添加数据以及如何显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17555108/

相关文章:

jquery - 查询将两个输入字段添加在一起

javascript - jQuery/javaScript 被浏览器忽略( Bootstrap )

php - (PHP/Postgresql) 调用返回 void 的函数?

javascript - 使用表单输入设置 cookie

PHP fatal error : Call to a member function bind_param() on boolean

javascript - 在 javascript 中模拟点击(也在窗口外......)

javascript - 通过装饰器执行方法

javascript - 在悬停另一个 div 时隐藏一个 div

php - 使用OAuth验证应用程序以访问融合表

php - 尝试获取存储过程结果时出错,但普通查询工作正常