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!

2
  • incluse? Did you mean include? Also, you shouldn't have multiple HTML elements with the same ID Commented Aug 10, 2016 at 4:28
  • You're using the wrong quotes for interpolation 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 Commented Aug 10, 2016 at 4:32

2 Answers 2

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'.
Sign up to request clarification or add additional context in comments.

3 Comments

6) Don't inject variables into a query, use prepared statements and placeholders
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!
Thank you for help. It was just practice. And you are right I should not inject variable directly to query.
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?

2 Comments

Thanks for help. It was not working with double quotes before. But now I changed again and seems working. Thanks again!
you could also select answers which helped you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.