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

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

    点赞 评论

相关推荐 更多相似问题