Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-07-2013, 12:50 PM   PM User | #1
minkoko
New Coder

 
Join Date: Aug 2010
Location: myeik
Posts: 72
Thanks: 4
Thanked 5 Times in 5 Posts
minkoko can only hope to improve
page loading insert condition problem

Hello Everyone ,i just want to ask one question about the php condition statement

i insert comment by using the sql statement and i check the condition using like this
PHP Code:
if(isset($_POST['submit'])){

and i check the validation of form value if form is empty it don't work and
all field is complete ,the query is true
My problem is when i insert the comment and i reload the browser the query is return insert and everytime i reload this page my mysql query statement is working but i want to just one query when i click the submit button

How to solve this problem?
My Code is
PHP Code:
<?php 
  
if(isset($_POST['submit']) ){
     
$name=$_POST['name'];
     
$email=$_POST['email'];
     
$comment=$_POST['comment'];
     
$comment_length=strlen($comment);
     
$limitcomment 200;
                                            
       if(
$comment_length $limitcomment){
             echo 
'    <script type="text/javascript">alert("Your message more than  Limit !. \n Thanks for your Comment ");</script> ';
     }else{
    if((!empty(
$name))){   if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['comment'])) {  echo '    <script type="text/javascript">alert("Your Comment Success!. \n Please wait Admin Approval ");</script> ';         
       
$comment_sql=mysql_query("INSERT INTO `tw_gallery`.`comment`
(`id`,`pho_id`,`name`,`email`,`comment`,`date`,`show`) VALUES (NULL,'$photoid','$name','$email','$comment',NOW(),'No')"
);
                                                    
    if(
$comment_sql){
   echo 
'    <script type="text/javascript">alert("Your Comment Success!. \n Please wait Admin Approval ");</script> ';  }else{ echo '<font color="red">Not Insert Comment</font>';
        }
         }
       }else{
    echo 
'    <script type="text/javascript">alert("Please enter the all field!. \n Thanks ");</script> ';
      }
      }
     }
     
?>
                                                
<form action="" id="comment_form" method="post">
<label for="name">Name (Required)</label>
<input type="text" name="name" id="name" />
<label for="email">Email (Required)</label>
<input type="email" name="email" id="email" />
<label for="comment">Comment (Required)</label>
<textarea name="comment" id="comment" cols="30" rows="10"></textarea>
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
__________________
Quote:
Myanmar Web Developer & Designer
http://www.cyberoot.com

Last edited by minkoko; 01-07-2013 at 12:54 PM..
minkoko is offline   Reply With Quote
Old 01-07-2013, 01:15 PM   PM User | #2
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
First things first - if TangoForce could get his hands on you, he'd probably slap you. Kidding. But he preaches don't use if($_POST['submit']) to check whether a form has been submitted. Here's why:


If you click the round curved arrow(in Mozilla) where its supposed to reload, it will resubmit the form. I think it even asks you whether you want to submit the form again and resend the data that has will probably repeat the action such as send and such.

What you can do, is make a hash of a concatenation of each commenters email and the message which will hold true for about 1 hour or so(To prevent them being unable to post the exact same message on another post later) then checking the hash and notifyng the user that they have already submitted that comment. This can also help prevent spam bots to a small degree.

Or even just use a session variable and compare the message. If it matches, tell the user that he has already posted such a message.
__________________
For professional Hosting and Web design.....


NetEssentials.co.uk

Last edited by Redcoder; 01-07-2013 at 01:22 PM..
Redcoder is offline   Reply With Quote
Users who have thanked Redcoder for this post:
minkoko (01-07-2013)
Old 01-07-2013, 01:33 PM   PM User | #3
minkoko
New Coder

 
Join Date: Aug 2010
Location: myeik
Posts: 72
Thanks: 4
Thanked 5 Times in 5 Posts
minkoko can only hope to improve
Thanks Redcoder ,Guiding to me

So,What method should i use to check? Do you think And
You probably slap me. Kidding
__________________
Quote:
Myanmar Web Developer & Designer
http://www.cyberoot.com
minkoko is offline   Reply With Quote
Old 01-07-2013, 01:53 PM   PM User | #4
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
Use session variables to check the messages....you can store an array of them to cater for multiple tabs like $_SESSION['message1'], $_SESSION['message2'] etc. Then compare the message posted to each variable in the array. If one of them matches, then don't execute query.
__________________
For professional Hosting and Web design.....


NetEssentials.co.uk
Redcoder is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:42 AM.


Advertisement
Log in to turn off these ads.