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


first here code:

<?php  //establish connection database require("database.php"); try{   // prepare , bind $stmt = $conn->prepare("insert clients (phonenumber, firstname) values (:phonenumber, :firstname)"); $stmt->bindparam(':phonenumber', $phonenumber, pdo::param_str); $stmt->bindparam(':firstname', $firstname, pdo::param_str);   // set parameters , 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 database $e";         exit;         } //my attempt on checking if data has been entered in 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 check if form data has been entered this:

<?php  if($inserted = true){  echo "the data has been entered in database";  } ?> 

now can see i'm trying set variable named $inserted true when data entered can determine if data has been entered successfully. reason not working. keeps giving me error $inserted undefined wrapped isset() , though got rid of error did not check see if $inserted set. in other words keep getting echo message has been entered though has not reason.

help appreciated, thank much.

instead of using flag, use ->lastinsertid method check whether last insertion 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 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 database $e";         echo $e->getmessage();         exit;     }      if($conn->lastinsertid() > 0) {         echo 'insertion 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: use ->rowcount() well:

if($conn->rowcount() > 0) {     // thing } 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -