Lucas Santos
Lucas Santos

Reputation: 1369

How can I determine when the form data has been successfully entered in the database?

First here is my code:

<?php 
//establish connection to the database
require("database.php");
try{
  // prepare and bind
$stmt = $conn->prepare("INSERT INTO clients (phonenumber, firstname) VALUES (:phonenumber, :firstname)");
$stmt->bindParam(':phonenumber', $phonenumber, PDO::PARAM_STR);
$stmt->bindParam(':firstname', $firstname, PDO::PARAM_STR);

 // set parameters and execute
if(isset($_POST['phonenumber'])){ $phonenumber = $_POST['phonenumber']; }
if(isset($_POST['firstname'])){ $firstname = $_POST['firstname']; }
$stmt->execute();

}catch (Exception $e) {
        echo "Could not insert data into the database $e";
        exit;
        }
//my attempt on checking if the data has been successfully entered in the database
        $inserted = true;
?>

<h2>The Form</h2>
<hr />
<br />
<form action="" method="post">
Number: <input type="text" name="phonenumber" value="" />
<br /><br />
First Name: <input type="text" name="firstname" value="" />
<br /><br />
<input type="submit" name="submit" value="Submit">
</form>
<br />
<hr />

</body>
</html>

Then I'm attempting to check if the form data has been successfully entered like this:

<?php 
if($inserted = true){ 
echo "THE DATA HAS BEEN SUCCESSFULLY ENTERED IN THE DATABASE"; 
}
?>

Now as you can see I'm trying to set a variable named $inserted as true when the data is entered so that I can determine if the data has been entered successfully. But for some reason it is NOT working. It keeps giving me an error that $inserted is undefined so I wrapped it with isset() and even though that got rid of the error it however did not check to see if $inserted was set. In other words I always keep getting the echo message that it has been entered successfully even though it has not for some reason.

Help is greatly appreciated, thank you very much.

Upvotes: 1

Views: 1122

Answers (1)

Kevin
Kevin

Reputation: 41885

Instead of using a flag, you could use the ->lastInsertId method to check whether the last insertion was succesful.

<?php 

if(isset($_POST['firstname'], $_POST['phonenumber'])) {
    require('database.php');
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $phonenumber = $_POST['phonenumber'];
    $firstname = $_POST['firstname'];

    try{
        $stmt = $conn->prepare("INSERT INTO clients (phonenumber, firstname) VALUES (:phonenumber, :firstname)");
        $stmt->bindParam(':phonenumber', $phonenumber, PDO::PARAM_STR);
        $stmt->bindParam(':firstname', $firstname, PDO::PARAM_STR);
        $stmt->execute();
    }
    catch (Exception $e) {
        echo "Could not insert data into the database $e";
        echo $e->getMessage();
        exit;
    }

    if($conn->lastInsertId() > 0) {
        echo 'insertion was made';
    }

}
?>

<h2>The Form</h2>
<hr />
<br />
<form action="" method="post">
Number: <input type="text" name="phonenumber" value="" />
<br /><br />
First Name: <input type="text" name="firstname" value="" />
<br /><br />
<input type="submit" name="submit" value="Submit">
</form>
<br />
<hr />

</body>
</html>

Sidenote: You could also use ->rowCount() as well:

if($conn->rowCount() > 0) {
    // do your thing
}

Upvotes: 2

Related Questions