这只是演示目的:不是完成项目的东西:您还应该清理所有帖子以避免来自松散者 = 黑客的 sql 注入 ..
也适用于您可以使用的唯一用户
$query = "INSERT INTO `users` (`user`, `pass`, `name`, `lastname`)
VALUES ('".strtolower($_POST['user'])."', '".$_POST['pass']."', '".$_POST['name']."', '".$_POST['lastname']."');";
<?php
/*
important php stuff here(headers,cookies)
without spaces and make sure your
editor save it in utf8 format!!!
this script work with table
CREATE TABLE `users` (
`id` int(15) unsigned zerofill NOT NULL AUTO_INCREMENT PRIMARY KEY,
`user` varchar(256) NOT NULL,
`pass` varchar(256) NOT NULL,
`name` varchar(256) NOT NULL,
`lastname` varchar(256) NOT NULL
) ENGINE='InnoDB';
### !!! first create a database in phpmyadmin or adminer or custom script
you should use phpmyadmin or adminer as assistent if you are beginner(whatever who read this)
if you want a quick start .. use youtube search to understand how to use them
then copy/paste and modify queries like your needs
*/
function error($no){
header('Location: index.php?error='.$no); exit();
}
$con = mysqli_connect('localhost','root','THEPASSWORD',"kindofdemo") or die("Connection failed");
if(isset($_POST['save'])){
$value = $_POST['user'];
(isset($_POST['pass'])and isset($_POST['pass2'])and($_POST['pass']===$_POST['pass2']))OR error(2);
$howmanyusers=mysqli_query($con,"SELECT count(*) FROM `users` WHERE `user` LIKE '%".$_POST['user']."%'");
$howmanyusers=$howmanyusers->fetch_array(MYSQLI_NUM);
//print_r($howmanyusers[0]);
if($howmanyusers[0]>0){ error(1); }
/*
INSERT INTO `users` (`user`, `pass`, `name`, `lastname`) VALUES ('user', 'pass', 'name', 'lastname');
*/
$query = "INSERT INTO `users` (`user`, `pass`, `name`, `lastname`)
VALUES ('".$_POST['user']."', '".$_POST['pass']."', '".$_POST['name']."', '".$_POST['lastname']."');";
echo('<b>executed:'.$query.'</b>');
$r = mysqli_query($con,$query) or die('Query failed!');
}
//phpinfo(); /*use this to see whatever you post , or many others usefull vars*/
if(isset($_REQUEST['error']))
switch($_REQUEST['error']){
case 1: echo '<b>The username already exist !</b>';
break;
case 2: echo '<b>The password confirmation is not equal with the choosen password</b>';
break;
}
/*
Let see what is in database
*/
if($actualusers=mysqli_query($con,"SELECT * FROM `users` WHERE 1;") ){
while($actualusers and($row = $actualusers->fetch_assoc()) ){
echo '<pre>';
print_r($row);
echo '</pre><hr>';
}
}
/*!!!!!!!!!!!!!!!!!!!*/
mysqli_close($con);
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="Custa">
<!-- <meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content=""> -->
<title>Document</title>
<style>
b{
color:red;
}
</style>
</head>
<body>
<h1>Secret message game</h1>
<form id ="form-data" action="index.php" method="post"> <!-- index.php without ? query will reset the custom error system :switch-->
<input type="text" name="user" id="user" autofocus placeholder="Enter your user name"><!-- this should not be constrain to that filter -->
<input type="text" name="name" placeholder="Enter your name" pattern="[a-z A-Z]{3,10}" title="a-z A-Z" required>
<input type="text" name="lastname" placeholder="Enter your lastname" pattern="[a-z A-Z]{3,10}" title="a-z A-Z" required>
<input type="password" name="pass" placeholder="Choose a password">
<input type="password" name="pass2" placeholder="Confirm the password to ensure it's the correct one">
<input type="submit" id="save" name="save">
</form>
</body>
</html>