![](/img/trans.png)
[英]How to add dynamically html table data in mysql database using PHP?
[英]Trying to Add record to mysql using php and html
我正在尝试将记录添加到sql表中,但是这样做很麻烦。
我有一个注册页面,该页面将使用用户名,密码和用户的电子邮件,并将其传递给一个小的php脚本,该脚本从较大的php文件中调用函数。
当我尝试按下注册按钮时,页面将刷新,并且没有任何内容添加到表格中。
任何建议表示赞赏。
的HTML:
<body>
<div class="body"></div>
<div class="grad"></div>
<div class="header">
</div>
<br>
<div class="signup">
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script type = "text/javascript" src="js/js_functions.js"></script>
<form method="post" action ="signup_script.php" onsubmit = "return checkEmailandPassword(this);" method = "post" >
<input type="text" placeholder="username" name="username" required = "required" maxlength = "15"><br>
<input type ="email" placeholder = "email" name = "email" required = "required" maxlength = "50"><br>
<input type ="email" placeholder = "re-enter email" name = "reemail" required = "required" maxlength = "50"><br>
<input type="password" placeholder="password" name="password" required = "required" pattern = "(?=.*\d)(?=.*[A-Z]).{10,}"><br>
<input type="password" placeholder="re-enter password"name="repassword" required = "required"><br>
<p class = "passwordreq">Password must:</p>
<ol class = "passwordreq">
<li>Have 10 characters</li>
<li>Have one number</li>
<li>Have one uppercase letter</li>
</ol>
<input type="submit" value="sign up"> <input type="button" value="go back" onclick="window.location='index.html'">
</form>
</body>
</html>
小php文件:
include_once('profile_Functions.php');
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
createUser($username,$password,$email);
大型php文件:
<?php
extract($_GET);
extract($_POST);
extract($_SERVER);
function setup(){
extract($_GET);
extract($_POST);
extract($_SERVER);
$host="localhost";
$user="root";
$passwd="";
$connect=mysql_connect($host,$user,$passwd) or die("error connecting mysql server");
mysql_select_db('oddjob',$connect) or die("error accessing db");
}
function createUser($name, $pass, $email){
setup();
$hashed_password = password_hash($pass, PASSWORD_DEFAULT);
$sql = "insert into profiles (UserName, Password, Email) values ($name, $hashed_password, $email); ";
$res = mysql_query($sql);
} ?>
您的insert
语句失败。 您应该在values
部分内用引号引起来的字符值:
$sql = "insert into profiles (UserName, Password, Email)
values ('$name', '$hashed_password', '$email'); ";
最好在执行mysql_query
之后检查$res
是否为false
。
请注意,将用户输入值直接注入SQL语句是不安全的。 阅读有关SQL注入攻击的信息,以及如何保护数据库免受攻击。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.