I am trying to insert
form data into MySQL using PHP but somehow the insert
query get fired two times.I have attached my code below...please help if anyone know about solution...
<?php
class Product{
// database connection and table name
private $conn;
private $table_name = "lspl_user_profile";
// object properties
public $id;
public $fname;
public $lname;
public $job;
public $dept;
public $email;
public $password;
public function __construct($db){
$this->conn = $db;
}
public function create(){
try{
// insert query
$query = "INSERT INTO lspl_user_profile
SET fname=:fname, lname=:lname, job=:job, dept=:dept, email=:email, password=:password";
// prepare query for execution
$stmt = $this->conn->prepare($query);
// sanitize
$fname=htmlspecialchars(strip_tags($this->fname));
$lname=htmlspecialchars(strip_tags($this->lname));
$job=htmlspecialchars(strip_tags($this->job));
$dept=htmlspecialchars(strip_tags($this->dept));
$email=htmlspecialchars(strip_tags($this->email));
$password=htmlspecialchars(strip_tags($this->password));
// bind the parameters
$stmt->bindParam(':fname', $fname);
$stmt->bindParam(':lname', $lname);
$stmt->bindParam(':job', $job);
$stmt->bindParam(':dept', $dept);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':password', $password);
// Execute the query
if(empty($fname) ||empty($lname) || empty($job) ||empty($dept) ||empty($email) || empty($password) )
{
return false;
}
else
{
$stmt->execute();
mysqli_close($conn);
return true;
}
}
// show error if any
catch(PDOException $exception){
die('ERROR: ' . $exception->getMessage());
}
}
}
?>
This is create_product.php file...
<?php
if($_POST){
// include core configuration
include_once '../config/core.php';
// include database connection
include_once '../config/database.php';
// product object
include_once '../objects/product.php';
// class instance
$database = new Database();
$db = $database->getConnection();
$product = new Product($db);
// set product property values
$product->fname = $_POST['fname'];
$product->lname = $_POST['lname'];
$product->job = $_POST['job'];
$product->dept = $_POST['dept'];
$product->email = $_POST['email'];
$product->password = $_POST['password'];
// create the product
echo $product->create() ? "true" : "false";
}
?>
This method send parameters to create_product.php
$.post("api/create_product.php", {
fname: this.state.fname,
lname: this.state.lname,
job: this.state.job,
dept: this.state.dept,
email: this.state.email,
password: this.state.password
}
);
Thank you, peoples..issue is resolved. Actually, I have made one silly mistake in HTML which causing post method invoke two time.
Especially, thanks to ADyson..