doudou_3636 2018-09-21 02:25
浏览 66
已采纳

使用PHP和SQLite的DB应用程序在数据库中返回空值

I'm currently building an ultra basic first name and last name database app to practice my skills with both PHP and SQLite and I have come into a bit of an issue that I can't figure out and I'm ready to pull my hair out over.

I'll try and explain this as best I can, bit of a newbie.

I'm using the Symfony HTTP-Foundation and polyfill-mbstring frameworks, the app is running error free and as expected apart from the issue of the input fields where I add the first name and last name don't seem to be writing to the database. When I view the data in the database, each row appears as NULL.

Here is the function that I have created to add a new client to the database:

function addNewClient($firstName, $lastName) {
   global $db;

   try {
      $query = 'INSERT INTO client_summary (first_name, last_name) VALUES (:first_name, :last_name)';

      $stmt = $db->prepare($query);

      $stmt->bindParam(':first_name', $firstName);
      $stmt->bindParam(':last_name', $lastName);

      return $stmt->execute();
   } catch(\Exception $e) {
      throw $e;
   }
}

Here's the code I am using for the input field

<form method="post" action="procedures/add-client.php" class="mt-5">
   <div class="form-group">
      <label for="firstName">First Name</label>
      <input type="text" class="form-control" id="firstName" name="firstName" value="<?php if(isset($firstName)) echo $firstName; ?>" placeholder="First Name" required />
   </div>

   <div class="form-group">
      <label for="lastName">Last Name</label>
      <input type="text" class="form-control" id="lastName" name="lastName" value="<?php if(isset($lastName)) echo $lastName; ?>" placeholder="Last Name" />
   </div>

   <button type="submit" class="btn btn-block btn-outline-primary">
      Insert Into Database
   </button>
</form>

Then the procedure for adding a new client

<?php
require_once __DIR__ . '/../inc/bootstrap.php';

$firstName = request()->get('first_name');
$lastName = request()->get('last_name');

try {
   $newClient = addNewClient($firstName, $lastName);

   redirect('/index.php');
   $response->send();
   exit;
} catch(\Exception $e) {
   throw $e;
}

The bootstrap.php is for the functions.php and connection.php files.

Here is the results in the DB Browser I am using.

Screenshot of DB Browser and the results I am getting

Surely this is something I'm missing and just need a pair of experienced eyes.

Thanking you in advance,

Stu :)

  • 写回答

1条回答 默认 最新

  • duanhuren5581 2018-09-21 08:37
    关注

    In your PHP code, where you are getting the first_name and last_name, you have an error. Those two names you are pulling from request() should match the fields name from your input tags on your HTML. So they should be changed for request()->get('firstName') and request()->get('lastName').

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?