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').

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

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改