Insert data in a table from php with PDO

0

I see many old tutorials that do not use PDOs. I'm trying to insert but, I have not. The code is this:

<?php
require_once 'database.php';
$database_connection = database_connect();

$title = 'Home';
$content = '
<h4>Title 1</h4>
<table>
<tr>
    <td><input type="text" name="fname" required placeholder="First Name"></td>
</tr>
<tr>
    <td><input type="text" name="lname" required placeholder="Last Name"></td>
</tr>
<tr>
     <td><input type="number" name="age" required placeholder="Age" min="10" > </td>
</tr>
<tr>
    <td><input type="submit" name="insert"></input></td>
</tr>
';

$content .=  '</table>';
// get values form input text and number
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $age = $_POST['age'];
    // mysql query to insert data
    $pdoQuery = "INSERT INTO 'users'('id', 'name', 'type') VALUES (:fname,:lname,:age)";
    $pdoResult = $pdoConnect->prepare($pdoQuery);
    $pdoExec = $pdoResult->execute(array(":fname"=>$fname,":lname"=>$lname,":age"=>$age));
        // check if mysql insert query successful
    if($pdoExec)
    {
        echo 'Data Inserted';
    }else{
        echo 'Data Not Inserted';
    }

include 'Template.php';
?>

They give me errors of the following type:

  

Notice: Undefined index: lname in C: \ wamp \ www \ www \ tool.php on line 35

I'm a little disconcerted since I followed all the exact steps to get me wrong.

    
asked by Perl 11.09.2016 в 21:48
source

4 answers

3

I do not see that you are including the respective label to delimit the form:

<form method="POST">
    Inputs aquí ....
</form>

Please consult the documentation: link

    
answered by 11.09.2016 в 23:55
1

After adding the form tags, after $content .= '</table>'; , add:

if(!empty($_POST){

and close the condition

    }else{
        echo 'Data Not Inserted';
    }

}  // <--- después del else
    
answered by 12.09.2016 в 08:12
1

Apparently you should perform a verification to make sure that the PHP code you have only runs after sending the form. For this, you can put the following:

if(isset($_POST["insert"])) {

    //tu codigo aca...

}

In addition, you must wrap the form in "form" tags as follows:

<form method="POST">

    <!-- tu formulario aca -->

</form>

It's true, there are a lot of old documents in the network that do not explain their examples with PDO. However, you can be guided by this tutorial that I have found in which just give practical examples with PDO connection from PHP / MySQL:

link

You will be able to see that they give an example with mysqli and then with PDO as a comparison.

    
answered by 13.09.2016 в 04:30
0

Good, try using this code, I think the error you have is because you are not linking the values that you are going to insert that is done with the method bindParam ().

try {
  $fname = $_POST['fname'];
  $lname = $_POST['lname'];
  $age = $_POST['age'];

  $sql= "INSERT INTO 'users'('id', 'name', 'type') VALUES (:fname,:lname,:age)";

  $sentencia = $pdoConnect -> prepare($sql);
  $sentencia-> bindParam(':fname', $fname, PDO::PARAM_STR);
  $sentencia -> bindParam(':lname', $lname, PDO::PARAM_STR);
  $sentencia -> bindParam(':age', $age, PDO::PARAM_STR);

  $pdoExec = $sentencia -> execute(); 

} catch (PDOException $e) {
     print 'ERROR: '. $e->getMessage();
     print '<br/>Data Not Inserted';
}
if($pdoExec)
{
    echo 'Data Inserted';
}
    
answered by 19.04.2017 в 01:12