Fatal error when calling the exec function

0

Hello when the code exactly and should work gives me this error and I do not know why the error gives me in this part of the code:

$users->exec($sql);

Complete code:

<?php

require_once 'database.php';

$database_connection = database_connect();

$users = $database_connection->query('SELECT * FROM coffee')->fetchAll();

$title = 'Home';
$content = '
<h4>Title 1</h4>
<form method="POST">
<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 .=  '</form></table>';
try {
      // set the PDO error mode to exception

    $sql = "INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ('John', 'Doe', '[email protected]')";
    // use exec() because no results are returned
    $users->exec($sql);
    echo "New record created successfully";
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;

include 'Template.php';
      ?>
    
asked by Perl 12.09.2016 в 01:22
source

1 answer

1

Using PDO it happens that:

  • By doing $users = $database_connection->query('SELECT * FROM coffee')->fetchAll() , you get a array .

  • That is, $users is a array .

Solution: use $database_connection instead of $users .

//
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', '[email protected]')";
$database_connection->exec($sql);
    
answered by 30.01.2017 / 18:30
source