Eric W Eric W - 2 months ago 14
MySQL Question

Insert Data into MySQL DB using PDO

I am new to PHP and MySQL. I am attemping to develop a Student Information System. Below is the code that i am using to insert information from a form to the database but does not seem to be working when I submit the form. Please tell me where i may be going wrong.

The table is as below:

CREATE TABLE IF NOT EXISTS `sims`.`tbl_users` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) NULL,
`email` VARCHAR(100) NULL,
`password` VARCHAR(200) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;


The insert code is as below:

//Create new user
if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['passowrd'])):

$sql="INSERT INTO tbl_users (name, email, password) VALUES (:name, :email, :password)";
$stmt=$conn->prepare($sql);
$stmt->bindParam(':name',$_POST['name']);
$stmt->bindParam(':email',$_POST['email']);
$stmt->bindParam(':password',password_hash($_POST['password'], PASSWORD_BCRYPT));

if($stmt->execute() ):
die('Success');
else:
die('fail');
endif;

endif;

Answer

You have a typo there in your if statement:

if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['passowrd']))
                                                                           ^^