使用PHP向SQL表添加数据以及如何显示错误
Using PHP to add data to a SQL Table and how to show errors
更新的问题:如果没有错误,我会尝试使用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"
}
?>
您的代码结构非常奇怪,我怀疑您对应该使用什么来做什么感到困惑。你为什么要把PHP放在页面的底部,毕竟是HTML?这一点,以及你在评论中谈论你的PHP代码"被触发"的方式,让我怀疑你是否理解PHP到底做了什么。PHP是一种用于动态构建HTML的服务器端工具。当您的用户看到页面时,您的所有PHP都已经运行完毕,在加载下一个页面之前无法执行任何操作。我不知道你在这里到底想做什么,但把PHP放在HTML之后从来都不是正确的解决方案。
如果你将数据放入一个表单中,然后用表单数据重新加载同一个页面,你应该做的是将大部分PHP放在页面的顶部,然后在HTML中任何你想要动态的地方放一些PHP。除非你在做一些花哨的事情,否则不应该需要Javascript。
您正在关闭html
标记。所以页面忽略了后面的内容。将</body>
和</html>
标记移动到脚本后面。此外,您不需要回显脚本。除非您在脚本中执行一些php逻辑,否则不会显示这些逻辑。
<!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 />
<body>
//All my HTML code...
//Related HTML code
<div>
<span class="notFilled" id="notFilled">*Please fill in all the fields.</span>
</div>
<?php
//All my PHP code...
//Related PHP/JQuery code
echo '<script type="text/javascript">'
, '$("#notFilled").css("visibility","visible")'
, '</script>';
?>
</body>
</html>
您的<script>
在<html>
标记之外。。应将其放置在关闭</body>
标签之前
不管怎样,这是一个非常肮脏的代码。。混合Html、JS和PHP?。。你需要把它们完全分开。它会帮你省去一些麻烦
希望这能帮助
如果要编辑css,最好使用.hide()和.show()方法。你甚至可以使用转换使它看起来更好(.fadeIn()…)
在任何情况下,我都不建议涉及php来加载javascript,但归根结底,我不确定脚本的上下文。
.css()
函数时,jQuery可能尚未加载。
确保在任何jQuery函数调用之前(确保它出现在上面)加载jQuery-js文件。
首先。。
1) 在脑海中加载jquery.js脚本。。。。
2) 我不明白你为什么要用<script></script>
和其他方法。。。将<身体>和
3) 使用document.ready函数
更新代码
<!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>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
//load jquery script here
</head>
<body>
//All my HTML code...
//Related HTML code
<div>
<span class="notFilled" id="notFilled">*Please fill in all the fields.</span>
</div>
<?php
//all your php code
?>
<script type="text/javascript">
$(function(){
$('#buttonID').click(function(){ //<--updated here
$("#notFilled").css("visibility","visible");
});
});
</script>;
</body>
- 从桌面读取python文件时高亮显示代码
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 如何在生成下载文件时显示加载动画
- React重新渲染但未显示正确的组件
- nodejs-expressjs上传图像并显示它们
- 使用JSP从服务器检索和显示图像
- 音频控件在mouseover上显示,在mouseout上淡出
- 让文本输入幻灯片显示输入时的新文本输入?然后向后滑动
- 使用电话间隙在Android应用程序中显示SQL Lite的数据
- 显示5秒后隐藏潜水
- 如何显示显示的html元素:在特定的媒体屏幕上没有
- yii2:依赖于Action的条件显示/显示字段
- Jquery/JS:通过样式显示显示选择框(无,块)
- 为什么选择显示显示值而不是标签
- 在HTML中显示显示缩进的日志的更好方法是什么
- 如何在javascript中显示显示var值的警报
- 如何在核心PHP上显示显示来自第三方网站的RSS提要的要求
- 使用PHP或JS显示/显示选中的复选框值
- Ag-grid -如何显示显示/隐藏列的selectAll选项企业设置过滤器
- 窗体显示显示在框中