Here is the form for taking input. When i press submit button page just reload and no error or success message. Also no data inserted in table. Is there anything wrong in my code?
<?php
require_once 'classes/user.php';
$obj_user = new User();
if (isset($_POST['reg-btn'])) {
$message = $obj_user->save_user($_POST);
}
?>
<div class="col-md-8">
<h3 class="text-info text-center"><u>এখানে নিবন্ধন করুন</u></h3>
<?php if (isset($message)) {
echo $message;
unset($message);
} ?>
<hr/>
<form class="form-horizontal" role="form" action="" method="post">
<div class="form-group">
<label class="control-label col-md-3" for="first_name">নামের প্রথম অংশ</label>
<div class="col-md-9">
<input type="text" name="first_name" id="first_name" class="form-control">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3" for="last_name">নামের শেষ অংশ</label>
<div class="col-md-9">
<input type="text" name="last_name" id="last_name" class="form-control">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3" for="email_address">ইমেইল ঠিকানা</label>
<div class="col-md-9">
<input type="email" name="email_address" id="email" class="form-control">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3" for="user_name">ইউসারনেম</label>
<div class="col-md-9">
<input type="text" name="user_name" id="last_name" class="form-control">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3" for="password">পাসওয়ার্ড</label>
<div class="col-md-9">
<input type="password" name="password" id="password" class="form-control">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3" for="address">ঠিকানা</label>
<div class="col-md-9">
<textarea class="form-control" name="address" id="address"></textarea>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3" for="city">শহর</label>
<div class="col-md-9">
<input type="text" name="city" id="city" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-md-offset-3 col-md-9">
<input type="submit" name="reg_btn" class="btn btn-success btn-block" value="নিবন্ধন সম্পন্ন">
</div>
</div>
</form>
</div>
Here is the user class. I checked the connection, its ok.
<?php
require_once 'db_connect.php';
class User {
private $pdo;
public function __construct() {
$obj_db = new Db_connect();
$this->pdo = $obj_db->pdo;
}
public function save_user($data) {
try {
$query = "INSERT INTO tbl_user(first_name, last_name, email_address, user_name, password, address, city) VALUES(:first_name, :last_name, :email_address, :user_name, :password, :address, :city)";
$stmt = $this->pdo->prepare($query);
$stmt->bindParam(':first_name', $data['first_name'], PDO::PARAM_STR);
$stmt->bindParam(':last_name', $data['last_name'], PDO::PARAM_STR);
$stmt->bindParam(':email_address', $data['email_address'], PDO::PARAM_STR);
$stmt->bindParam(':user_name', $data['user_name'], PDO::PARAM_STR);
$stmt->bindParam(':password', $data['password'], PDO::PARAM_STR);
$stmt->bindParam(':address', $data['address'], PDO::PARAM_STR);
$stmt->bindParam(':city', $data['city'], PDO::PARAM_STR);
$stmt->execute();
$message = "আপনি সফলভাবে নিবন্ধন করেছেন";
return $message;
} catch (PDOException $e) {
echo $e->getMessage();
}
}
}
Your if (isset($_POST['reg-btn'])) {
never fires because $_POST['reg-btn']
is not set.
When in doubt about why something isn't firing trying taking out the conditional or giving it an else
option to see which case it falls into. From there you can use var_dump
to see what the values being sent are, etc.
So in this code you could have done:
if (isset($_POST['reg-btn'])) {
$message = $obj_user->save_user($_POST);
} else {
var_dump($_POST);
}
which would have shown you the name had the _
instead of -
. or
//if (isset($_POST['reg-btn'])) {
$message = $obj_user->save_user($_POST);
//}
which would have allowed your form to process so you'd know $_POST['reg-btn']
wasn't processing for some reason. The commenting out route is a bit longer.
Your submit button name is reg_btn
and you are checking isset($_POST['reg-btn'])
Replace this:
isset($_POST['reg-btn'])
with:
isset($_POST['reg_btn'])