我已经在这个问题上苦苦挣扎了几天,因为这是我第一个使用PHP的项目,所以很难自己解决这个问题.我在其他线程的帮助下重写了我的代码,但我仍然找不到解.
所以,我想进行简单的用户注册.我从这个表单中获取用户的输入,该表单包含在registration_page.html中,代码如下:
<form id="registerForm" action="http://localhost//register.php" method="post">
<fieldset align="center">
<legend id="legendText">Register</legend>
<p>Name:
<input type="text" name="fname" value="" required autofocus maxlength="16"></p>
<p>Last name:
<input type="text" name="lname" value="" required maxlength="16"></p>
<p>E-mail:
<input type="email" name="mail" value="" placeholder="@mail.com" maxlength="32" required></p>
<p>Age:
<input type="number" name="age" value="" maxlength="2" max="99" maxlength="2" size="2" min="1" required></p>
<p>Job:
<input type="text" name="job" value="" maxlength="16" required></p>
<table align="center" id="registerPwdAndUsrTable" border="1" width="30%">
<tr>
<td>
<p>Username:
<input type="text" name="username" value="" required maxlength="16"></p>
</td>
<td>
<p>Password:
<input type="password" name="password" value="" required maxlength="16"></p>
</td>
</tr>
</table>
<input form="registerForm" type="Submit" value="???????">
<input form="registerForm" type="Reset" value="???????">
</fieldset>
</form>
Run Code Online (Sandbox Code Playgroud)
提交表单后,调用的php脚本是register.php(正如您在action=" "我的脚本中看到的那样由服务器处理).现在,register.php是:
<!Doctype html>
<html>
<head>
<meta charset="utf-8"><!--characters recognised-->
<title>Register</title>
<style>
body{
background-image: url(my_images//background_homepage.jpg);
background-repeat: no-repeat;
background-size:cover;
}
</style>
</head>
<body>
<?php
extract( $_POST );//superglobal variable
if( isset($_POST['submit'] ) ) {
print_r($_POST);
//Getting the variables from 'registerForm' ++ /* checking if user inputs are set */
$first_name = (isset($_POST['fname'])) ? $_POST['fname'] : NULL;
$last_name = (isset($_POST['lname'])) ? $_POST['lname'] : NULL;
$age = (isset($_POST['age'])) ? $_POST['age'] : NULL;
$job = (isset($_POST['job'])) ? $_POST['job'] :NULL;
$email = (isset($_POST['mail'])) ? $_POST['mail'] : NULL;
$username = (isset($_POST['username'])) ? $_POST['username'] : NULL;
$password = (isset($_POST['password'])) ? $_POST['password'] : NULL;
//INSERT INTO Query Creation
$sqlQuery = "INSERT INTO registered_user ( age,email,fname,job,lname,password,username )
VALUES ('$age','$email','$first_name','$job','$last_name','$password','$username')";
//MySQL connection
if( !( $database = mysql_connect( "localhost","root","" ) ) )//server name , a username , password
die( "Couldn't connect to DB </body></html>" );//if false --> script gets terminated
//Opening database "htmlproject"
if( !mysql_select_db("htmlproject",$database) )//Database to be used , htmlproject
die( "Couldnt open htmlproject db </body></html>" );
//Query
mysql_query( $sqlQuery, $database);
if( !( $result = mysql_query( $sqlQuery, $database) ) )
{
print( "failed query! <br />" );
die( mysql_error() . "</body></html>" );
}else{
print("success query!");
}//end if
//Free resources
mysql_close( $database );
}//end ifisset
?><!--end php script.To be executed by server-->
<script type="text/javascript">//registration completed
var r=window.confirm("registration completed");
if( r == true){
document.location.href = "HOMEPAGE.html";
}else{
document.location.href = "HOMEPAGE.html";
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,这INSERT INTO registered_user...不起作用.我没有得到我的数据库的条目,没有.我正在使用XAMPP,表格如下:
你永远不会定义'提交'.您没有具有该名称的表单元素.因此:
if( isset($_POST['submit'] ) ) {
Run Code Online (Sandbox Code Playgroud)
每次都会失败.您没有输出任何错误消息,是否已检查错误日志?您假设查询正在运行.在开始<?php标记之后立即将错误报告添加到文件顶部error_reporting(E_ALL); ini_set('display_errors', 1);添加错误检查,例如or die(mysql_error())查询.或者您可以在当前的错误日志中找到问题.错误检查将显示$_POST['submit']未定义.
此外:
Little Bobby说你的脚本存在SQL注入攻击的风险..即使逃避字符串也不安全!
请停止使用mysql_*功能.这些扩展已在PHP 7中删除.了解PDO和MySQLi的预处理语句并考虑使用PDO,这非常简单.
永远不要存储纯文本密码!请使用PHP的内置函数来处理密码安全性.如果您使用的PHP版本低于5.5,则可以使用password_hash() 兼容包.在散列之前,请确保不要转义密码或使用任何其他清理机制.这样做会更改密码并导致不必要的额外编码.
| 归档时间: |
|
| 查看次数: |
95 次 |
| 最近记录: |