Reputation: 195
I have a form and trying to insert data in table with class. Data is inserting in proper way. But when click on browser reload after insert query, then it inserts duplicate entry in table. I tried it with redirect function. But not working.
My form is:
<?php
if(isset($_POST['insertcourse']))
{
$userprofileobj->insert_course($_POST['c_name'],$_POST['c_description']);
}
?>
<form action="" method="POST">
<div class="control-group">
<label class="control-label">Course Name :</label>
<div class="controls">
<input type="text" class="span11" placeholder="First name" name="c_name" />
</div>
</div>
<div class="controls">
<label class="control-label">Course Description :</label>
<textarea class="textarea_editor span12" rows="6" placeholder="Enter text ..." name="c_description"></textarea>
</div>
<div class="form-actions">
<input type="submit" value="SAVE" name="insertcourse" class="btn btn-success">
</div>
</form>
My classes.php is:
<?php
class operations{
public $user_name;
public $error;
public $con;
public function __construct()
{
$this->con = new mysqli("localhost","root","","admin_with_oops");
}
public function insert_course($c_name,$c_description)
{
$date1 = date("Y-m-d");
$result = $this->con->query("insert into courses (c_name,c_description,date1,status1) VALUES ('$c_name', '$c_description', '$date1','1')");
if($result)
{
$alertmsgs = "Inserted Successfully";
$this->alertmsg($alertmsgs);
}
}
public function alertmsg($alertmsgs)
{
echo"<div class='alert alert-error'>
<button class='close' data-dismiss='alert'>×</button>
<strong></strong> $alertmsgs </div>
</div>";
}
}
Upvotes: 1
Views: 77
Reputation: 8392
You could generate a guid and put it in a hidden input in your form. Database that ID on the initial submit and don't allow it to be inserted a second time.
<input type="hidden" value="<?php com_create_guid() ?>">
It's a little more foolproof than post-redirect-get and will be a less jarring user experience, especially over high latency networks.
Upvotes: 1