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 有偿求效果比较好的遥感影像匹配的c++代码
  • ¥15 博主,你好,我下载了你的智能网联汽车辅助驾驶安全信息检测系统,现在不会运行,可以教我吗,
  • ¥15 怎么在excle输入下列公式
  • ¥15 Arduino,利用modbus的RS485协议,进行对外置的温湿度传感器进行数据读取
  • ¥15 vhdl+MODELSIM
  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题