【发布时间】:2015-06-25 03:13:20
【问题描述】:
数据库已连接 - 但我似乎无法插入新记录。 数据库已创建,表已创建,但我的表单不起作用。
我必须能够添加新用户,验证密码 - 但我已经能够解决这部分问题。
我尝试了很多次 - 不同的代码 - 我不确定是语法错误还是变量等。
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<style type="text/css">
body{
color:black;
background-color:#6BDBAF;
}
h1{
color: #177A58;
font: 48px arial, sans-serif;
font-weight: bold;
text-align: center; padding: 3px;
text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
}
h2,h3,h4{
color:#177A58;
text-align: center;
text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
}
.banner{
height:25px;
width:100%;
color:green;
background-color:#177A53;
font:arial bold 15/25px;
text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
}
.menu a {
margin-left: 50px;
margin-right: 50px;
padding: auto;
width: auto;
height: 20px;
text-decoration: none;
color:white;
font: 15px/25px Helvetica, sans-serif, solid;
}
.menu a:hover {
background: #00CC66;
}
#login-form{
positive: relative;
}
td{
min-width: 100px;
}
.form-container {
border: 2px solid #38875a;
background: #060f0e;
background: -webkit-gradient(linear, left top, left bottom, from(#177A53), to(#060f0e));
background: -webkit-linear-gradient(top, #177A53, #060f0e);
background: -moz-linear-gradient(top, #177A53, #060f0e);
background: -ms-linear-gradient(top, #177A53, #060f0e);
background: -o-linear-gradient(top, #177A53, #060f0e);
background-image: -ms-linear-gradient(top, #177A53 0%, #060f0e 100%);
-webkit-border-radius: 26px;
-moz-border-radius: 26px;
border-radius: 26px;
-webkit-box-shadow: rgba(000,000,000,0.9) 0 1px 2px, inset rgba(255,255,255,0.4) 0 1px 0;
-moz-box-shadow: rgba(000,000,000,0.9) 0 1px 2px, inset rgba(255,255,255,0.4) 0 1px 0;
box-shadow: rgba(000,000,000,0.9) 0 1px 2px, inset rgba(255,255,255,0.4) 0 1px 0;
font-family: 'Helvetica Neue',Helvetica,sans-serif;
text-decoration: none;
vertical-align: middle;
min-width:500px;
padding:20px;
width:70%;
margin-left: auto;
margin-right: auto;
}
.form-field {
border: 1px solid #c9b7a2;
background: #84ba94;
-webkit-border-radius: 8px;
-moz-border-radius: 8px;
border-radius: 8px;
color: #194221;
-webkit-box-shadow: rgba(255,255,255,0.4) 0 1px 0, inset rgba(000,000,000,0.7) 0 1px 1px;
-moz-box-shadow: rgba(255,255,255,0.4) 0 1px 0, inset rgba(000,000,000,0.7) 0 1px 1px;
box-shadow: rgba(255,255,255,0.4) 0 1px 0, inset rgba(000,000,000,0.7) 0 1px 1px;
padding:8px;
margin-bottom:20px;
width:80%;
}
.form-field:focus {
background: #fff;
color: #020504;
}
.form-container h2 {
text-shadow: #0a1f17 0 1px 0;
font-size:18px;
margin: 0 0 10px 0;
font-weight:bold;
text-align:center;
}
.form-title {
margin-bottom:20px;
margin-left: 40px;
color: white;
text-shadow: #0a1f17 0 1px 0;
}
.Submit-container {
margin:8px 0;
text-align:right;
}
.Submit {
border: 2px solid #26402e;
background: #424a44;
background: -webkit-gradient(linear, left top, left bottom, from(#4a9e65), to(#424a44));
background: -webkit-linear-gradient(top, #4a9e65, #424a44);
background: -moz-linear-gradient(top, #4a9e65, #424a44);
background: -ms-linear-gradient(top, #4a9e65, #424a44);
background: -o-linear-gradient(top, #4a9e65, #424a44);
background-image: -ms-linear-gradient(top, #4a9e65 0%, #424a44 100%);
-webkit-border-radius: 11px;
-moz-border-radius: 11px;
border-radius: 11px;
-webkit-box-shadow: rgba(255,255,255,0.4) 0 1px 0, inset rgba(255,255,255,0.4) 0 1px 0;
-moz-box-shadow: rgba(255,255,255,0.4) 0 1px 0, inset rgba(255,255,255,0.4) 0 1px 0;
box-shadow: rgba(255,255,255,0.4) 0 1px 0, inset rgba(255,255,255,0.4) 0 1px 0;
text-shadow: #384728 0 1px 0;
color: #070a05;
font-family: helvetica, serif;
padding: 8.5px 18px;
font-size: 14px;
text-decoration: none;
vertical-align: middle;
float: left;
width: 300px;
}
.Submit:hover {
border: 2px solid #7bc7b4;
text-shadow: #31540c 0 1px 0;
background: #88bfb1;
background: -webkit-gradient(linear, left top, left bottom, from(#357a5e), to(#88bfb1));
background: -webkit-linear-gradient(top, #357a5e, #88bfb1);
background: -moz-linear-gradient(top, #357a5e, #88bfb1);
background: -ms-linear-gradient(top, #357a5e, #88bfb1);
background: -o-linear-gradient(top, #357a5e, #88bfb1);
background-image: -ms-linear-gradient(top, #357a5e 0%, #88bfb1 100%);
color: #fff;
}
.Submit:active {
text-shadow: #31540c 0 1px 0;
border: 2px solid #447314;
background: #48a67c;
background: -webkit-gradient(linear, left top, left bottom, from(#394231), to(#88bfb1));
background: -webkit-linear-gradient(top, #394231, #48a67c);
background: -moz-linear-gradient(top, #394231, #48a67c);
background: -ms-linear-gradient(top, #394231, #48a67c);
background: -o-linear-gradient(top, #394231, #48a67c);
background-image: -ms-linear-gradient(top, #394231 0%, #48a67c 100%);
color: #fff;
}
.report{
color: grey;
width: 70%;
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>
<div class="banner">
<div class="menu">
<a href="index.php" class="one">Home</a>
<a href="resume.html" class="two">About</a>
<a href="gallery.php" class="three">ArtGallery</a>
<a href="checkout.php" class="four">Shop</a>
<a href="register.php" class="five">Register</a>
<a href="theory.html" class="six">ArtTheory</a>
<a href="account.php" class="seven">MyAccount</a>
<a href="userlogin.php" class="eight">UserLogin</a>
</div>
</div>
<h1>Website Sign Up</h1>
<form action="register.php" method="post">
<input type="hidden" name="Submit" value="true"/>
<table class="form-container">
<tr><td><label class="form-title">Enter User Email:* </td><td><input type="text" name="loginEmail" class="form-field" value=""/></label></td></tr>
<tr><td><label class="form-title">Enter a Password:* </td><td><input type="text" id="password" name="password" class="form-field" value=""/></label></td></tr>
<tr><td><label class="form-title">First Name: </td><td><input type="text" name="firstName" class="form-field" value=""/></label></td></tr>
<tr><td><label class="form-title">Last Name: </td><td><input type="text" name="lastName" class="form-field" value=""/></label></td></tr>
<tr><td><label class="form-title">Street Address: </td><td><input type="text" name="street" class="form-field" value=""/></label></td></tr>
<tr><td><label class="form-title">City: </td><td><input type="text" name="city" class="form-field" value=""/></label></td></tr>
<tr><td><label class="form-title">State: </td><td><input type="text" name="state" class="form-field" value=""/></label></td></tr>
<tr><td><label class="form-title">Postcode: </td><td><input type="text" name="zip" class="form-field" value=""/></label></td></tr>
<tr><td><label class="form-title">Phone: </td><td><input type="text" name="phone" class="form-field" value=""/></label></td></tr>
<tr><td></td><td><input type="Submit" class="Submit" value="Submit"/></td></tr>
</table>
<br />
</form>
<div class="report">
<?php
include ('libcommon.php');
mysql_select_db($membersDB,$conn);
if (!$membersDB) {
echo mysql_error();
}else{
echo "<p>Successfully connected to the <em>MembersDirectory</em> database: Waiting for new record...";
}
// query
$db = new PDO('mysql:host=localhost;dbname=MembersDirectory;charset=utf8', 'root', '');
$q = $db->prepare("INSERT INTO members (loginEmail,password,firstName,lastName,street,city,state,zip,phone)
VALUES (:loginEmail,:password,:fname,:lname,:street,:city,:state,:zip,:phone)");
$q->execute(array(':loginEmail'=>$uname,
':password'=>$pwd,
':firstName'=>$fname,
':lastName'=>$lname,
':street'=>$Street,
':city'=>$City,
':state'=>$State,
':zip'=>$PostCd,
':phone'=>$Phone));
?>
</div>
</body>
</html>
【问题讨论】:
-
什么错误,如果有的话?什么输出,如果有的话?始终运行
print_r()以确认您获得了预期值。 -
另外,使用 PDO 函数,而不是 mysql* 函数来避免 SQL 注入风险。
-
停下!在你说 www 之前插入未经处理的用户输入会让你被黑
-
存储纯文本密码也超级糟糕
-
应该解雇仍在教 mysql_* 的人(在教规之外(在 @Fred-ii- 的家))
标签: php html mysql validation