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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵