我没有收到任何错误。我清除了错误php_error_log文件,运行脚本,没有错误。它假设要插入表中,但它不会。这是代码。
感谢您提供的任何帮助,我只是不知道为什么这个sint工作。没有错误,没有输出,但没有修改表。
PS-是的,我确实声明了db等,它现在刚刚显示。
stmt = $db->prepare("INSERT INTO users (id,username,password,first_name,last_name,email) VALUES(NULL,:username,:password,:first_name,:last_name,:email)");
$stmt->execute(array('username' => $sec_user, 'password' => $sec_pass, 'first_name' => $sec_first, 'last_name' => $sec_last, 'email' => $sec_email));
if ($stmt) {
//echo "New Record has id ".$db->insert_id;
echo "<p>You are now registered. You will be redirected in 5 seconds, or click Login below.</p>";
header("refresh:5;url=http://scrubhost.servehttp.com/login.php");
} else {
echo "<p>MySQL error no {$db->getMessage} : {$db->getMessage}</p>";
exit();
}
&#13;
我也试过
$resultprep = $db->prepare("INSERT INTO users (`username`, `password`, `first_name`, `last_name`, `email`) VALUES ( '?','?','?','?','?')");
$resultprep->bindParam(1, $sec_usere, PDO::PARAM_STR);
$resultprep->bindParam(2, $sec_passe, PDO::PARAM_STR);
$resultprep->bindParam(3, $sec_firste, PDO::PARAM_STR);
$resultprep->bindParam(4, $sec_laste, PDO::PARAM_STR);
$resultprep->bindParam(5, $sec_emaile, PDO::PARAM_STR);
$resultprep->execute();
if ($resultprep) {
}
&#13;
使用&#34;?&#34;&#39; s
的数组方法- 编辑
try {
$stmt = $db->prepare("INSERT INTO users (id,username,password,first_name,last_name,email) VALUES(NULL,:username,:password,:first_name,:last_name,:email)");
$stmt->execute(array('username' => $sec_user, 'password' => $sec_pass, 'first_name' => $sec_first, 'last_name' => $sec_last, 'email' => $sec_email));
if ($stmt) {
//echo "New Record has id ".$db->insert_id;
echo "<p>You are now registered. You will be redirected in 5 seconds, or click Login below.</p>";
header("refresh:5;url=http://scrubhost.servehttp.com/login.php");
} else {
echo "<p>MySQL error no {$db->getMessage} : {$db->getMessage}</p>";
exit();
}
} catch(PDOException $e){ $e->getMessage(); }
&#13;
编辑编辑编辑编辑
<!DOCTYPE html>
<html>
<?php
$iv = '1928705354837653';
$encrypt_key = '8913f09a87A098F7YA90D8FTAG97RGA[W9UHDASOIJHASDIJSGADIUGashfaouihfadfdaf1ad56';
function pkcs7_pad($data, $size)
{
$length = $size - strlen($data) % $size;
return $data . str_repeat(chr($length), $length);
}
function pkcs7_unpad($data)
{
return substr($data, 0, -ord($data[strlen($data) - 1]));
}
?>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>RSPS Host - Register</title>
<link href="/ucp/css/bootstrap.min.css" rel="stylesheet">
<link href="/ucp/font-awesome/css/font-awesome.css" rel="stylesheet">
<link href="/ucp/css/plugins/iCheck/custom.css" rel="stylesheet">
<link href="/ucp/css/animate.css" rel="stylesheet">
<link href="/ucp/css/style.css" rel="stylesheet">
</head>
<body class="gray-bg">
<div class="middle-box text-center loginscreen animated fadeInDown">
<div>
<div>
<h1 class="logo-name"> </h1>
</div>
<h3>Register</h3>
<?php
require_once("db_const.php");
if (!isset($_POST['submit'])) {
?> <!-- The HTML registration form -->
<body class="gray-bg">
<p>Please fill out all fields. </p>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<form class="m-t" role="form">
<div class="form-group">
<input type="username" name="username" class="form-control" placeholder="Username" required="">
<input type="first_name" name="first_name" class="form-control" placeholder="First name" required="">
<input type="last_name" name="last_name" class="form-control" placeholder="Last name" required="">
<input type="email" name="email" class="form-control" placeholder="Email" required="">
<input type="password" name="password" class="form-control" placeholder="Password" required="">
</div>
<button type="submit" name="submit" value="Register" class="btn btn-primary block full-width m-b">Register</button>
</form>
<p class="text-muted text-center"><small>Already have an account?</small></p>
<?php
} else {
## connect mysql server
$db = new PDO('mysql:host=localhost;dbname=mainrspshosts', 'root', '');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->beginTransaction();
## query database
# prepare data for insertion
$username = $_POST['username'];
$password = $_POST['password'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$encryption_key_user = $encrypt_key . $username;
$encryption_key_pass = $encrypt_key . $password;
$encryption_key_firstname = $encrypt_key . $first_name;
$encryption_key_lastname = $encrypt_key . $last_name;
$encryption_key_email = $encrypt_key . $email;
$sec_user = openssl_encrypt(
pkcs7_pad($username, 16),
'AES-256-CBC',
$encryption_key_pass,
0,
$iv
);
$sec_pass = openssl_encrypt(
pkcs7_pad($password, 16),
'AES-256-CBC',
$encryption_key_pass,
0,
$iv
);
$sec_first = openssl_encrypt(
pkcs7_pad($first_name, 16),
'AES-256-CBC',
$encryption_key_firstname,
0,
$iv
);
$sec_last = openssl_encrypt(
pkcs7_pad($last_name, 16),
'AES-256-CBC',
$encryption_key_lastname,
0,
$iv
);
$sec_email = openssl_encrypt(
pkcs7_pad($email, 16),
'AES-256-CBC',
$encryption_key_email,
0,
$iv
);
# check if username and email exist else insert
$exists = 0;
$resultprep = $db->prepare("SELECT username from users WHERE username = ? LIMIT 1");
$resultprep->execute(array($sec_user));
$result = $resultprep->fetchAll();
if (count($result) == 1) {
$exists = 1;
$resultprepare = $db->prepare("SELECT email from users WHERE email = '?' LIMIT 1");
$resultprep->execute(array($sec_email));
$result = $resultprep->fetchAll();
if (count($result) == 1) $exists = 2;
} else {
$resultprep = $db->prepare("SELECT email from users WHERE email = '?' LIMIT 1");
$resultprep->execute(array($sec_email));
$result = $resultprep->fetchAll();
if (count($result) == 1) $exists = 3;
}
if ($exists == 1) echo "<p>Username already exists!</p>";
else if ($exists == 2) echo "<p>Username and Email already exists!</p>";
else if ($exists == 3) echo "<p>Email already exists!</p>";
else {
# insert data into mysql database
ini_set('error_reporting', E_ALL);// turn on error reporting by placing this on top
$resultprep = $db->prepare("INSERT INTO `users` (`password`) VALUES ('test')");
$resultprep->execute();
if ($resultprep) {
//echo "New Record has id ".$db->insert_id;
echo "<p>You are now registered. You will be redirected in 5 seconds, or click Login below.</p>";
header("refresh:5;url=http://scrubhost.servehttp.com/login.php");
} else {
exit();
}
}
}
?>
<a class="btn btn-sm btn-white btn-block" href="login.php">Login</a>
</form>
<p class="m-t"> Copyright <strong>RSPSHost.info</strong> © 2015 - 2016
</div>
</div>
<!-- Mainly scripts -->
<script src="js/jquery-2.1.1.js"></script>
<script src="js/bootstrap.min.js"></script>
<!-- iCheck -->
<script src="js/plugins/iCheck/icheck.min.js"></script>
<script>
$(document).ready(function(){
$('.i-checks').iCheck({
checkboxClass: 'icheckbox_square-green',
radioClass: 'iradio_square-green',
});
});
</script>
</body>
</html>
&#13;
答案 0 :(得分:0)
在您的数据库中id
应该是自动增量,可能是主键。
ini_set('error_reporting', E_ALL);// turn on error reporting by placing this on top
$resultprep = $db->prepare("INSERT INTO `users` (`username`, `password`, `first_name`, `last_name`, `email`) VALUES (:username,:password,:first_name,:last_name,:email)");
$resultprep->bindParam(:username, $sec_usere);
$resultprep->bindParam(:password, $sec_passe);
$resultprep->bindParam(:first_name, $sec_firste);
$resultprep->bindParam(:last_name, $sec_laste);
$resultprep->bindParam(:email, $sec_emaile);
$resultprep->execute();
if ($resultprep) {
}
您的完整代码
<?php
ini_set('error_reporting', E_ALL);// turn on error reporting by placing this on top
//require_once("db_const.php");
try {
$db = new PDO('mysql:host=localhost;dbname=mainrspshosts', 'root', '');
} catch (Exception $e) {
echo "Failed: " . $e->getMessage();
$db->rollBack();
}
$iv = '1928705354837653';
$encrypt_key = '8913f09a87A098F7YA90D8FTAG97RGA[W9UHDASOIJHASDIJSGADIUGashfaouihfadfdaf1ad56';
function pkcs7_pad($data, $size)
{
$length = $size - strlen($data) % $size;
return $data . str_repeat(chr($length), $length);
}
function pkcs7_unpad($data)
{
return substr($data, 0, -ord($data[strlen($data) - 1]));
}
if (isset($_POST['submit'])) {
# prepare data for insertion
$username = $_POST['username'];
$password = $_POST['password'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$encryption_key_user = $encrypt_key . $username;
$encryption_key_pass = $encrypt_key . $password;
$encryption_key_firstname = $encrypt_key . $first_name;
$encryption_key_lastname = $encrypt_key . $last_name;
$encryption_key_email = $encrypt_key . $email;
$sec_user = openssl_encrypt(
pkcs7_pad($username, 16),
'AES-256-CBC',
$encryption_key_pass,
0,
$iv
);
$sec_pass = openssl_encrypt(
pkcs7_pad($password, 16),
'AES-256-CBC',
$encryption_key_pass,
0,
$iv
);
$sec_first = openssl_encrypt(
pkcs7_pad($first_name, 16),
'AES-256-CBC',
$encryption_key_firstname,
0,
$iv
);
$sec_last = openssl_encrypt(
pkcs7_pad($last_name, 16),
'AES-256-CBC',
$encryption_key_lastname,
0,
$iv
);
$sec_email = openssl_encrypt(
pkcs7_pad($email, 16),
'AES-256-CBC',
$encryption_key_email,
0,
$iv
);
# check if username and email exist else insert
$resultprep = $db->prepare("SELECT `username`,`email` from `users` WHERE `username` = :username OR `email`=:email");
$resultprep->bindParam(:username,$sec_user);
$resultprep->bindParam(:email,$sec_email);
$resultprep->execute();
$error=0;
while($result = $db->fetch(PDO::FETCH_ASSOC)) {
if ($sec_user == $result['username']) {
echo "<p>Username already exists!</p>";
$error=1;
}
if ($sec_email == $result['email']) {
echo "<p>Email already exists!</p>";
$error=1;
}
if ($sec_user == $result['username'] && $sec_email == $result['email']) {
$error=1;
echo "<p>Username and Email already exists!</p>";
}
}
# insert data into mysql database
if($error!=0) {
$resultprep = $db->prepare("INSERT INTO `users` (`username`, `password`, `first_name`, `last_name`, `email`) VALUES (:username, :password, :first_name, :last_name, :email)");
$resultprep->bindParam(:username, $sec_user);
$resultprep->bindParam(:password, $sec_pass);
$resultprep->bindParam(:first_name, $sec_first);
$resultprep->bindParam(:last_name, $sec_last);
$resultprep->bindParam(:email, $sec_email);
$resultprep->execute();
if ($resultprep) {
echo "<p>You are now registered. You will be redirected in 5 seconds, or click Login below.</p>";
header("refresh:5;url=http://scrubhost.servehttp.com/login.php");
} else {
exit();
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>RSPS Host - Register</title>
<link href="/ucp/css/bootstrap.min.css" rel="stylesheet">
<link href="/ucp/font-awesome/css/font-awesome.css" rel="stylesheet">
<link href="/ucp/css/plugins/iCheck/custom.css" rel="stylesheet">
<link href="/ucp/css/animate.css" rel="stylesheet">
<link href="/ucp/css/style.css" rel="stylesheet">
</head>
<body class="gray-bg">
<div class="middle-box text-center loginscreen animated fadeInDown">
<div>
<div>
<h1 class="logo-name"> </h1>
</div>
<h3>Register</h3>
<body class="gray-bg">
<p>Please fill out all fields. </p>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<form class="m-t" role="form">
<div class="form-group">
<input type="username" name="username" class="form-control" placeholder="Username" required="">
<input type="first_name" name="first_name" class="form-control" placeholder="First name" required="">
<input type="last_name" name="last_name" class="form-control" placeholder="Last name" required="">
<input type="email" name="email" class="form-control" placeholder="Email" required="">
<input type="password" name="password" class="form-control" placeholder="Password" required="">
</div>
<button type="submit" name="submit" value="Register" class="btn btn-primary block full-width m-b">Register</button>
</form>
<p class="text-muted text-center"><small>Already have an account?</small></p>
<a class="btn btn-sm btn-white btn-block" href="login.php">Login</a>
</form>
<p class="m-t"> Copyright <strong>RSPSHost.info</strong> © 2015 - 2016
</div>
</div>
<!-- Mainly scripts -->
<script src="js/jquery-2.1.1.js"></script>
<script src="js/bootstrap.min.js"></script>
<!-- iCheck -->
<script src="js/plugins/iCheck/icheck.min.js"></script>
<script>
$(document).ready(function(){
$('.i-checks').iCheck({
checkboxClass: 'icheckbox_square-green',
radioClass: 'iradio_square-green',
});
});
</script>
</body>
</html>