下面的询问没有告诉我,数据库中已经存在用户名称,尽管如此。
我正试图学习如何约束参数等,但把自己混为一谈。
<?php
// Include config.php
require_once("".$_SERVER[ DOCUMENT_ROOT ]."/admin/config.php");
// top.inc.php
require_once($top_inc);
?>
<!-- Meta start -->
<title></title>
<meta name="description" content="" />
<meta name="keywords" content="" />
<!-- Meta end -->
<!-- CONTENT START -->
<?php
// sidebar.inc.php
require_once($sidebar_inc);
// main.inc.php
require_once($main_inc);
// check if form has been submitted
if($_SERVER[ REQUEST_METHOD ] == POST && isset($_POST[ submit ])){
// initialize form errors array
$error = array();
// fetch form data
$username = $_POST[ username ];
$email = $_POST[ email ];
$password = $_POST[ password ];
// validate form data
if(!preg_match(constant("USERNAME_REGEX"), $username)){
$error[] = "Please enter a username. Use 3 to 15 digits and letters";
}
if(!preg_match(constant( PASSWORD_REGEX ), $password)){
$error[] = "Please enter a password. Minimum of 6 characters required";
}
if(!empty($password) && $password == $username){
$error[] = "Your pasword cannot be you username for security reasons";
}
if(empty($email)){
$error[] = "Please enter your email address";
}
if(!empty($email) && !filter_var($email, FILTER_VALIDATE_EMAIL)){
$error[] = "Your email address is not valid";
}
// connect to database
sql_con();
// Get instance of statement
$stmt = mysqli_stmt_init($connect);
// sql statement
$UserExists = "
SELECT
`user_login`
FROM
`users`
WHERE
`user_login` = ? ";
// prepare sql statement for execution
if (mysqli_stmt_prepare($stmt, $UserExists)) {
// bind parameters [s for string]
mysqli_stmt_bind_param($stmt, "s", $username) or die(mysqli_stmt_error());
// execute statement
mysqli_stmt_execute($stmt) or die(mysqli_stmt_error());
// check if username is found
if(mysqli_stmt_num_rows($stmt) > 0 ){
$error[] = The username you have choose has already been taken ;
}
}
// If errors found display errors
if(!empty($error)){
foreach($error as $msg){
echo "$msg <br />";
}
} else {
echo My Query Worked! ;
}
}
// signup.tpl template location
$tpl = inc/tpl/signup.tpl ;
// load signup form template
PageContentTemplate($tpl);
?>
<!-- CONTENT FINISH -->
<?php
// footer.inc.php
require_once($footer_inc);
?>
Basically it just echos out My Query Worked even though it should say the username is already taken which it is, I enter details on the form and a username I know that has been taken and submit the form, I know I am doing something probably really silly but being new to mysqli and binding parameters etc. I don t know where I am going wrong even though I have looked at some examples.
坦率地说,我不相信这是以程序风格这样做的最佳方式,我不知道PDO/OOP,我的改变主要原因是在我的问询中使用地主等来避免投放。