'Can you figure out why this PHP model doesn't insert the data in a MySQL database?
I'm trying to insert some data with PHP, but I have this problem with the following model not inserting the data into the SQL database. The controller sends the data correctly, I checked and is also received in the model(also checked), but the query is not working, the data is not inserted in the database. Can you help me?
This is the controller:
<?php
try {
$name = "Frank";
$lname = "Casttle";
$email = "[email protected]";
$mobile = "3136556195";
// $name = htmlspecialchars($_POST['name']);
// $lname = htmlspecialchars($_POST['lname']);
// $email = htmlspecialchars(trim($_POST['mail']));
// $mobile = htmlspecialchars(trim($_POST['phone']));
require_once ("../model/connection.php");
require_once ("../model/handle_data_model.php");
$connection = Connect::connection();
$handleUser = new Handle_users($connection);
$insertUser = new Users_obj();
if(empty($name) || empty($lname) || empty($email) || empty($mobile)) {
echo "<b class='text-danger'>¡You must complet all required fields!<b>";
} else {
$insertUser->set_name(mb_strtolower($name));
$insertUser->set_lname(mb_strtolower($lname));
$insertUser->set_email(mb_strtolower($email));
$insertUser->set_userMobile($mobile);
// echo "<b class='text-danger'>¡Data Sent!<b>";
$handleUser->set_basicUser($insertUser);
}
} catch(exception $e) {
// die ("Error: " . $e->getMessage());
die ();
echo "<b class='text-danger'>1. ".$e->getMessage()."<b>)";
}
?>
This is the model:
<?php
public function set_basicUser(Users_obj $setUser) {
$name = $setUser->get_name();
$lname = $setUser->get_lname();
$email = $setUser->get_email();
$mobile = $setUser->get_userMobile();
echo $name." ".$lname." ".$email." ".$mobile ; // Data is arriving correctly
$this->connection->beginTransaction();
try {
$insert_persona = "INSERT INTO usuario (name, lname, mail, phone) VALUES
('".$name."', '".$lname."', '".$email."', '".$mobile."')";
$this->connection->exec($insert_persona);
echo "<b class='text-primary'>2. ¡User Inserted!<b>";
} catch (PDOexception $e) {
$this->connection->rollback();
echo "<b class='text-danger'>3. ".$e->getMessage()."<b>)";
}
$this->connection = null;
}
?>
At the end, the code enters in the try and execute the code, the transaction is succesful, even it didn't throw error, but the model is not inserting the data into the database. Can you help me? Maybe I'm missing something. Thank you!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
