php - $_POST 错误保存 mysql 数据库

标签 php mysql http-post

我有这段代码,这是通过电子邮件提供的链接注册用户。所以 ID 和电子邮件被表单获取并显示,然后检查是否有用户然后它会在字段上显示内容,然后它只是一个更新。现在我要做的是创建,因为提供给用户的链接中的 ID 尚不存在。这是名为 Register.php 的文件中的代码

<?php

    load_function('database.php');

    if(!empty($_GET['user_id']) && !empty($_GET['cbemail'])) 
    {
        $user_id = base64_decode($_GET['user_id']);
        $email = base64_decode($_GET['cbemail']);   

        $employee = get_employee($user_id,$email);

    }

        /*$first_name = $_POST['first_name'];
        $middle_name = $_POST['middle_name'];
        $last_name = $_POST['last_name'];
        $suffix = $_POST['suffix'];
        $gender = $_POST['gender'];
        $date_birth = $_POST['date_birth'];
        $cbemail = $_POST['cbemail'];
        $locality_id = $_POST['locality_id'];
        $home_phone = $_POST['home_phone'];
        $mobile_phone = $_POST['mobile_phone'];*/

?>

<div id="main-content">
    <div id="emp_registrationform">
      <form action="" method="post">
        <table width="600" border="0">
        <h1>Create Employee</h1>
          <tr>
            <td class="align-right"><label for="user_id">User</label>
            <input type="text" name="user_id" id="user_id" 
             value="<?php if(isset($user_id)){echo $user_id ; } ?>"/></td>
            </td>
          </tr>
          <tr>
            <td class="align-right"><label for="first_name">First Name</label>
              <input type="text" name="first_name" id="first_name" value='<?php echo $employee['first_name'] ?>'/></td>
            <td><label for="birth_date">Birth Date</label>
              <input type="text" name="birth_date" id="birth_date" value='<?php echo $employee['date_birth'] ?>'/></td>
          </tr>
          <tr>
            <td class="align-right"><label for="middle_name">Middle Name</label>
              <input type="text" name="middle_name" id="middle_name" value='<?php echo $employee['middle_name'] ?>'/></td>
            <td><label for="cbemail">Email</label>
              <input type="text" name="cbemail" id="cbemail" 
               value="<?php if(isset($email)){echo $email ; } ?>"/></td>
          </tr>
          <tr>
            <td class="align-right"><label for="last_name">Last Name</label>
              <input type="text" name="last_name" id="last_name" value='<?php echo $employee['last_name'] ?>'/></td>
            <td><label for="locality_id">Locality ID</label>
              <input type="text" name="locality_id" id="locality_id" value='<?php echo $employee['locality_id'] ?>'/></td>
          </tr>
          <tr>
            <td class="align-right"><label for="suffix">Suffix</label>
              <input type="text" name="suffix" id="suffix" value='<?php echo $employee['suffix'] ?>'/></td>
            <td><label for="home_phone">Phone(Home)</label>
              <input type="text" name="home_phone" id="home_phone" value='<?php echo $employee['home_phone'] ?>'/></td>
          </tr>
          <tr>
            <td class="align-right"><label for="gender">Gender</label>
              <select>
                <option selected><?php echo  $employee['gender'] ?></option>
                <option value="male">Male</option>
                <option value="female">Female</option>
              </select></td>
            <td><label for="mobile_phone">Phone(Mobile)</label>
              <input type="text" name="mobile_phone" id="mobile_phone" value='<?php echo  $employee['mobile_phone'] ?>'/></td>
          </tr>
           <td class="align-right">
              <input type="submit" id="submit-btn" value="Create Employee" />
           </td>
           <td>&nbsp;</td>
        </table>
      </form>
    </div>
  </div>

我的问题是这些数据在保存到数据库时没有被捕获。我在数据库中的功能如下。

function add_employee($user_id, $first_name, $middle_name, $last_name, 
                      $suffix, $gender, $date_birth, $cbemail, 
                      $locality_id, $home_phone, $mobile_phone) {
    $db   =  load_db();
    $sql = "INSERT into employees (
            user_id, first_name, middle_name, last_name, 
            suffix, gender, date_birth, cbemail,
            locality_id, home_phone, mobile_phone 
            ) VALUES (
            $user_id, $first_name, $middle_name, $last_name, 
            $suffix, $gender, $date_birth, $cbemail,
            $locality_id, $home_phone, $mobile_phone
            )";
    $result = $db->query($sql);
}

是的,我需要一个 POST,但我不知道如何在输入框的值字段中捕获“值”。

提交按钮将在这行代码中重定向。

if(isset($_POST['save_user'])){
    add_employee($_POST['user_id'], $_POST['first_name'], $_POST['middle_name'], $_POST['last_name'], 
                 $_POST['suffix'], $_POST['gender'], $_POST['date_birth'], $_POST['cbemail'], 
                 $_POST['locality_id'], $_POST['home_phone'], $_POST['mobile_phone']); 

有什么想法吗?我希望你能理解这个问题。

最佳答案

要将字符串存储在 Mysql 数据库中,您需要使用 ''(引号)。

所以对于像 $_POST['first_name'] 这样的字符串,使用 '$_POST['first_name']'

顺便说一句,尝试使用参数化查询也是安全的,

那你应该是好的。

关于php - $_POST 错误保存 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16584153/

相关文章:

php - mysql 连接被拒绝并且 mysql 没有这样的文件或目录

mysql - 从案例陈述中选择

mysql - 从React表单发布到我的后端API时出错

c# - POST 提交的表单显示 URL 中的值。难道不应该把他们藏起来吗?

php - 如何获取 PHP 脚本的运行时间

php - 如何使用 wamp 在 netbeans 7.1.1 中为 zend 框架 1.11.11 设置开发环境?

mysql - 选择最高的季节和剧集(mysql)

c# - 自托管 WebAPI 和多种 post 和 get 方法

php - 列出表中包含日期信息的行

php - 如何在 MySQL 的一个字段中存储多个动态值?