PHP代码不会抛出错误,但数据不会输入数据库

标签 php mysql forms

好吧,我花了 5 分钟想出了一个像样的标题,让读者更好地理解我的问题。我有一个表单、一个用于添加新用户(任何网站的用户注册部分)的 php 代码和一个数据库。我在 php 代码中进行了足够好的错误检查。但是,当我填写表格并单击注册时,没有显示错误。通常情况下,这意味着成功,但在这种情况下,表单中的数据不会进入数据库。我错过了什么吗?我对 php 用户授权/验证相当陌生,所以这可能意味着我错过了一些东西。代码如下:

表格:

<form class="form-inline" method="post" name="login_form">
       <form action="useradd.php" method="post">
              <p><input type="text" class="span2" name="firstname" id="firstname" placeholder="First Name"></p>
              <p><input type="text" class="span2" name="lastname" id="Last Name" placeholder="Last Name"></p>
              <p><input type="text" class="span2" name="username" id="username" placeholder="Username"></p>
              <p class="help-block" style="font-size:12px"> Username should be between 4-20 characters long.</p>
              <p><input type="Password" class="span2" name="Password" placeholder="Password"></p>
              <p class="help-block" style="font-size:12px"> Password must be between 4-20 characters long. Must be alpha-numeric</p>
              <p><input type="Password" class="span2" name="Password" placeholder="Re-Enter Password"></p>
              <p><input type="text" class="span4" name="emailid" id="emailid" placeholder="Emaid ID - example@example.com"></p>
              <p><input type="text" class="span2" name="teamname" id="teamname" placeholder="Team name"></p>
              <p class="help-block" style="font-size:12px"> Select your Unique team name.</p>
              <p>
                  <select class="selectpicker">
                     <option>The name of the city where you were born</option>
                     <option>The name of your first pet</option>
                     <option>What is your mother's maiden name</option>
                  </select>
                </p>
                <p><input type="text" class="span2" name="secretanswer" id="secretanswer" placeholder="Secret Answer"></p>
                <p>
                <input type="hidden" name="form_token" value="<?php echo $form_token; ?>" /><br />
              <p><button type="submit" class="btn btn-primary">Register</button></p>
            </form>

php 文件 - 名为 useradd.php

<?php
/*** begin our session ***/
session_start();

/*** first check that both the username, password, form token etc have been sent ***/
if(!isset( $_POST['firstname'],$_POST['lastname'],$_POST['username'], $_POST['password'],$_POST['emailid'],$_POST['teamname'],$_POST['secret_question'],$_POST['secret_answer'], $_POST['form_token']))
{
    $message = 'Please make sure you have the filled the form correctly';
}
/*** check the form token is valid ***/
elseif( $_POST['form_token'] != $_SESSION['form_token'])
{
    $message = 'Invalid form submission';
}
/*** check the username is the correct length ***/
elseif (strlen( $_POST['username']) > 20 || strlen($_POST['username']) < 4)
{
    $message = 'Incorrect Length for Username';
}
/*** check the password is the correct length ***/
elseif (strlen( $_POST['password']) > 20 || strlen($_POST['password']) < 4)
{
    $message = 'Incorrect Length for Password';
}
/*** check the username has only alpha numeric characters ***/
elseif (ctype_alnum($_POST['username']) != true)
{
    /*** if there is no match ***/
    $message = "Username must be alpha numeric";
}
/*** check the password has only alpha numeric characters ***/
elseif (ctype_alnum($_POST['password']) != true)
{
        /*** if there is no match ***/
        $message = "Password must be alpha numeric";
}
else
{
    /*** if we are here the data is valid and we can insert it into database ***/
    $firstname = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING);
    $lastname = filter_var($_POST['lastname'], FILTER_SANITIZE_STRING);
    $username = filter_var($_POST['username'], FILTER_SANITIZE_STRING);
    $password = filter_var($_POST['password'], FILTER_SANITIZE_STRING);
    $emailid = filter_var($_POST['emailid'], FILTER_SANITIZE_STRING);
    $teamname = filter_var($_POST['teamname'], FILTER_SANITIZE_STRING);
    $secret_question = filter_var($_POST['secret_question'], FILTER_SANITIZE_STRING);
    $secret_answer = filter_var($_POST['secret_answer'], FILTER_SANITIZE_STRING);


    /*** now we can encrypt the password ***/
    $password = sha1( $password );

    /*** connect to database ***/
    /*** mysql hostname ***/
    $mysql_hostname = 'localhost';

    /*** mysql username ***/
    $mysql_username = 'root';

    /*** mysql password ***/
    $mysql_password = 'hassan28';

    /*** database name ***/
    $mysql_dbname = 'adb project';

    try
    {
        $dbh = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);
        /*** $message = a message saying we have connected ***/

        /*** set the error mode to excptions ***/
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        /*** prepare the insert ***/
        $stmt = $dbh->prepare("INSERT INTO users (firstname,lastname,username,password,emailid,teamname, secret_question,secret_answer ) VALUES (:firstname,:lastname,:username,:password, :emailid,:teamname,:secret_question,:secret_answer)");

        /*** bind the parameters ***/
        $stmt->bindParam(':firstname', $firstname, PDO::PARAM_STR);
        $stmt->bindParam(':lastname', $lastname, PDO::PARAM_STR);
        $stmt->bindParam(':username', $username, PDO::PARAM_STR);
        $stmt->bindParam(':password', $password, PDO::PARAM_STR, 40);
        $stmt->bindParam(':emailid', $emailid, PDO::PARAM_STR);
        $stmt->bindParam(':teamname', $teamname, PDO::PARAM_STR);
        $stmt->bindParam(':secret_question', $secret_question, PDO::PARAM_STR);
        $stmt->bindParam(':secret_answer', $secret_answer, PDO::PARAM_STR);

        /*** execute the prepared statement ***/
        $stmt->execute();

        /*** unset the form token session variable ***/
        unset( $_SESSION['form_token'] );

        /*** if all is done, say thanks ***/
        $message = 'New user added';
    }
    catch(Exception $e)
    {
        /*** check if the username already exists ***/
        if( $e->getCode() == 23000)
        {
            $message = 'Username already exists';
        }
        else
        {
            /*** if we are here, something has gone wrong with the database ***/
            $message = 'We are unable to process your request. Please try again later"';
        }
    }
}
?>

<html>
<head>
<title>Login</title>
</head>
<body>
<p><?php echo $message; ?>
</body>
</html>

最佳答案

检查 secretanswer(html 格式)与 secret_answer(php 格式)。应该是:

<p><input type="text" class="span2" name="secret_answer" id="secret_answer" placeholder="Secret Answer"></p>

此外,您的 PHP 需要“secret_question”值,但您的表单未提交该值。你想要这样的东西:

<select class="selectpicker" id="secret_question" name="secret_question">
  <option value="city_born">The name of the city where you were born</option>
  <option value="first_pet">The name of your first pet</option>
  <option value="mom_maiden_name">What is your mother's maiden name</option>
</select>

关于PHP代码不会抛出错误,但数据不会输入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20203104/

相关文章:

php - 使用 Laravel 将数组数据存储在数据库中

PHP 用方法调用查询

MySQL 选择有序行,然后随机化结果

文本字段中的 HTML 表单提交按钮

jquery - 在 jquery 中提交后清除表单输入字段

PHP 查找所有主题标签但没有链接

php - MySQL使用多个表查找一条记录

php - Doctrine 返回 null 代替 EntityNotFoundException

JavaScript 表单验证错误

javascript - 如何修改 PHP/Jquery/Ajax 脚本以拥有多个表单字段