简体   繁体   English

PHP的插入MySQL无法正常工作

[英]php INSERT INTO MySQL not working

I 've been struggling with this one for a couple of days and since this is my first project using PHP it's kinda hard to figure it out by myself.I rewrote my code with the help from other threads but I still can't find the solution. 我已经为此苦苦挣扎了几天,因为这是我第一个使用PHP的项目,所以我自己很难弄清楚。在其他线程的帮助下,我重新编写了代码,但仍然找不到解。

So, I want to make a simple user registration.I get the user's input from this form , which is included in registration_page.html , the code is this: 因此,我想进行一个简单的用户注册,我从此表单获得用户的输入,该表单包含在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>

After submitting the form the php script that is called is register.php (as you see in the action=" " my script is handled by the server ). 提交表单后,称为的php脚本为register.php (如您在action=" "所见,我的脚本由服务器处理)。 Now,the register.php is: 现在, register.php是:

register.php 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>

So my problem is that the INSERT INTO registered_user... doesn't work.I don't get an entry to my database, nothing. 所以我的问题是INSERT INTO registered_user...不起作用。我没有数据库条目,什么也没有。 I am using XAMPP and the table is this: 我正在使用XAMPP,表是这样的:

registered_user table SQL script 已注册的用户表SQL脚本

You never, ever define 'submit'. 您永远都不会定义“提交”。 You don't have a form element with that name. 您没有使用该名称的表单元素。 Therefore this: 因此:

if( isset($_POST['submit'] ) ) {

will fail every time. 每次都会失败。 You're not outputting any error messages, have you checked your error logs? 您没有输出任何错误消息,是否检查了错误日志? You're making an assumption the queries are working. 您假设查询正在运行。 Add error reporting to the top of your file(s) right after your opening <?php tag error_reporting(E_ALL); ini_set('display_errors', 1); 在打开<?php标签error_reporting(E_ALL); ini_set('display_errors', 1);之后立即将错误报告添加到文件顶部error_reporting(E_ALL); ini_set('display_errors', 1); error_reporting(E_ALL); ini_set('display_errors', 1); Add error checking, such as or die(mysql_error()) to your queries. 向查询添加错误检查,例如or die(mysql_error()) Or you can find the issues in your current error logs. 或者,您可以在当前的错误日志中找到问题。 Error checking will reveal $_POST['submit'] is undefined. 错误检查将显示$_POST['submit']未定义。


In addition: 此外:

Little Bobby says your script is at risk for SQL Injection Attacks. 小鲍比说, 您的脚本有遭受SQL注入攻击的危险。 . Even escaping the string is not safe! 即使转义字符串也不安全!

Please stop using mysql_* functions . 停止使用mysql_*函数 These extensions have been removed in PHP 7. Learn about prepared statements for PDO and MySQLi and consider using PDO, it's really pretty easy . 这些扩展已在PHP 7中删除。了解有关PDOMySQLi的 准备好的语句并考虑使用PDO, 这确实非常容易

Never store plain text passwords! 切勿存储纯文本密码! Please use PHP's built-in functions to handle password security. 请使用PHP的内置函数来处理密码安全性。 If you're using a PHP version less than 5.5 you can use the password_hash() compatibility pack . 如果您使用的PHP版本低于5.5,则可以使用password_hash() 兼容性包 Make sure you don't escape passwords or use any other cleansing mechanism on them before hashing. 在散列之前,请确保您不要转义密码或对密码使用任何其他清除机制。 Doing so changes the password and causes unnecessary additional coding. 这样做会更改密码,并导致不必要的附加编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM