0

Hi I was developing my first db connection with php. But for some reason data is not inserting to the table. I have a page that takes the first name and last name. And I created a table called user info. here is the whole code except(error and result page which is all fine).

index.php

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Test</title>
  <link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>
<div id="wrapper">
  <div id="main">
    <div id="inbox">
       <form action="function.php" method="post">
         <span id="first">First Name: </span>
         <input type="text" name="fname" id="textField">
         <span id="second">Last name: </span>
         <input type="text" name="lname" id="textField"><br><br>
         <input type="submit" value="Submit" id="submitBtn">
        </form>
      </div>
    </div>
   </div>
   </body>
   </html>

database.php

<?php
$dsn = 'mysql:host=localhost;dbname=practice';
$username = 'user';
$password = 'user123';

 try {
    $db = new PDO($dsn, $username, $password);
 } catch (PDOException $e) {
    $error_message = $e->getMessage();
    include('database_error.php');
    exit();
 }
?>

function.php

<?php
 require_once('database.php');

$firstName = filter_input(INPUT_POST, 'fname');
$lastName  = filter_input(INPUT_POST, 'lname');

$firstName_q = $db->quote($firstName);
$lastName_q = $db->quote($lastName);

$query = 'INSERT INTO PRACTICE.USERINFO
        (USERID, FIRSTNAME, LASTNAME)
     VALUES(DEFAULT, $firstName_q, $lastName_q)';

$insert_count = $db->exec($query);

include('result.php');
?>

I am not sure where I am doing the mistake. It seems like its connecting successfully. And shows result page(which is just a text saying successful). But the table is empty.I saw some post similar to this but they are either too long or complicated for me at this time. A help would be highly appreciated. thanks in advance!

Thomson Mathew
  • 419
  • 1
  • 9
  • 28
  • `incluse`? Did you mean `include`? Also, you shouldn't have multiple HTML elements with the same ID – Phil Aug 10 '16 at 04:28
  • You're using the [wrong quotes for interpolation](http://php.net/manual/en/language.types.string.php#language.types.string.syntax) into your `$query` variable and you obviously aren't running PHP with decent error reporting enabled. Make sure your `php.ini` has `display_errors = On` and `error_reporting = E_ALL`. You should really be using a [prepared statement with bound parameters](http://php.net/manual/en/pdo.prepared-statements.php) – Phil Aug 10 '16 at 04:32

2 Answers2

2
  • first, in your database-php code replace incluse with include. Incluse does not exist in php.
  • second, I would advise not to use a dot in your table name, use an underscore instead. PRACTICE_USERINFO. Personally I always prefer lower-case for table names.
  • third, in test mode echo your $error_message var directly, to see what the error might be.
  • fourth, in test mode let your page show all errors. (google on 'let php show all errors'). Be sure not to do this in real-life mode.
  • fifth, me not being expert on OOP, I might have missed something...
  • sixth, embody your sql-query with double-quotes, not single quotes, as a commenter mentioned already. When you use single quotes, the variables starting with $ are not 'read'.
Tharif
  • 13,794
  • 9
  • 55
  • 77
Harryk
  • 36
  • 3
  • 1
    6) Don't inject variables into a query, use prepared statements and placeholders – DarkBee Aug 10 '16 at 04:53
  • Thank you very much I think I should use the double quotes to read the variable inside. The dot is used to refer the particular database. My table name is just userinfo which is inside practice database. Thank you very much and its working now! – Thomson Mathew Aug 10 '16 at 04:57
  • Thank you for help. It was just practice. And you are right I should not inject variable directly to query. – Thomson Mathew Aug 10 '16 at 04:59
0

There are several issues in your code ,most of them are mentioned by @HarryK in answer , i would like to focus particularly on the specific issue of "Why insert not getting triggered ?" Your query :

$query = 'INSERT INTO PRACTICE.USERINFO
        (USERID, FIRSTNAME, LASTNAME)
     VALUES(DEFAULT, $firstName_q, $lastName_q);

Replace with double quotes as below :

$query = "INSERT INTO PRACTICE.USERINFO
        (USERID, FIRSTNAME, LASTNAME)
     VALUES(DEFAULT, $firstName_q, $lastName_q)";

Good Reference :

When to use single quotes, double quotes, and backticks in MySQL

PHP - Single quotes or double quotes around SQL query?

Community
  • 1
  • 1
Tharif
  • 13,794
  • 9
  • 55
  • 77