SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    form placing two sets of same data into the database

    hi,

    I have recently created a form. everything works fine except for the fact that it places two sets of correct data into the database instead of one, once the submit button is clicked.

    Below is the relevant php:

    PHP Code:
    <?php require_once("includes/connection.php");
    include(
    "includes/functions.php");
    //form validation
    if(isset($_POST['submit'])){
    $errors=array();
    $requiredfields=array('site_use'=>'Please enter web site type','biz_type'=>'Please enter your business details','market'=>'Please enter the market question','style'=>'Please answer the style question If not sure just put unsure','message'=>'Please answer the message question If not sure just put unsure','branding'=>'Please answer the branding question If not sure put unsure','budget'=>'Please enter a budget figure','images'=>'Please state if you can provide images','name'=>'Please enter your name','email'=>'Please enter your email so we can contact you','aftercare'=>'Please state if you are wish to have aftercare services');
    foreach(
    $requiredfields as $fieldname => $requiredfieldsmessage){
    if(!isset(
    $_POST[$fieldname]) || empty($_POST[$fieldname]) || strlen(trim($_POST[$fieldname])) == 0/*|| $_POST[$fieldname]==""*/ {
    $errors[] = $requiredfieldsmessage;
    }
    }
     if(
    count($errors) == 0){
     
     
    $_POST array_map('mysqli_prep'$_POST);
     
    //end form validation
    $name  mysqli_prep($_POST['name']);
    $email  mysqli_prep($_POST['email']);
    $tel  mysqli_prep($_POST['tel']);
    $site_use  mysqli_prep($_POST['site_use']);
    $biz_type  mysqli_prep($_POST['biz_type']);
    $market  mysqli_prep$_POST['market']);
    $style  mysqli_prep$_POST['style']);
    $message  mysqli_prep($_POST['message']);
    $branding  mysqli_prep($_POST['branding']);
    $budget  mysqli_prep($_POST['budget']);
    $images  mysqli_prep($_POST['images']);
    $aftercare mysqli_prep($_POST['aftercare']);
    $query "INSERT into briefing_form (
    name,email,tel,site_use,biz_type,market,style,message,branding,budget,images,aftercare)Values('
    {$name}','{$email}','{$tel}','{$site_use}','{$biz_type}','{$market}','{$style}','{$message}','{$branding}','{$budget}','{$images}','{$aftercare}')";
    $result mysqli_query($connection$query);
    $message="You have recieved a briefing form";
    $subject="briefing form";
    $subject2="Your Quote";
    $message2="Thank you for contacting us. We will forward a quote to this address.";
    $email2=$email;
    mail('*****',$subject,$message);
    mail($email2$subject2$message2);
    $result mysqli_query($connection$query);
    header("Location: http://www.mysite/brief_return.php");
    exit;
    }
    }
    ?>
    and a snippet from the form:

    HTML Code:
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"><td style="padding:20px">
    <p >What is your type of business, what are are your products and/or services?</p>
    <textarea name="biz_type" cols="40" rows="5"   > <?php echo htmlspecialchars($_POST['biz_type'],ENT_QUOTES); ?></textarea></td>
    </tr>
    Anyone any ideas why the form is doing this?

    thanks

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Heh, it's because you execute mysql_query twice.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    After resolving the duplicate mysqli_query statement, you may also wish to investigate what happens when someone double-clicks on the submit button. Many peoeple do that for some reason.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    Heh, it's because you execute mysql_query twice.
    ...yes...just a test...i wondered how long it would someone to notice the unbearably obvious

    thanks works fine now!

    pmw57 wrote:

    After resolving the duplicate mysqli_query statement, you may also wish to investigate what happens when someone double-clicks on the submit button. Many peoeple do that for some reason.
    thanks I did try it but it didnt throw up a problem and only put the data in once.


    cheers

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Oh that's good. I'm still unsure as to whether links or button, or get or post are affected by multiple clicks.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Just a heads up, but all these mysqli_prep calls are not needed as you array_map this function call to $_POST earlier on.
    PHP Code:
    $name  mysqli_prep($_POST['name']);
    $email  mysqli_prep($_POST['email']);
    $tel  mysqli_prep($_POST['tel']);
    $site_use  mysqli_prep($_POST['site_use']);
    $biz_type  mysqli_prep($_POST['biz_type']);
    $market  mysqli_prep$_POST['market']);
    $style  mysqli_prep$_POST['style']);
    $message  mysqli_prep($_POST['message']);
    $branding  mysqli_prep($_POST['branding']);
    $budget  mysqli_prep($_POST['budget']);
    $images  mysqli_prep($_POST['images']);
    $aftercare mysqli_prep($_POST['aftercare']); 
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    Just a heads up, but all these mysqli_prep calls are not needed as you array_map this function call to $_POST earlier on.
    PHP Code:
    $name  mysqli_prep($_POST['name']);
    $email  mysqli_prep($_POST['email']);
    $tel  mysqli_prep($_POST['tel']);
    $site_use  mysqli_prep($_POST['site_use']);
    $biz_type  mysqli_prep($_POST['biz_type']);
    $market  mysqli_prep$_POST['market']);
    $style  mysqli_prep$_POST['style']);
    $message  mysqli_prep($_POST['message']);
    $branding  mysqli_prep($_POST['branding']);
    $budget  mysqli_prep($_POST['budget']);
    $images  mysqli_prep($_POST['images']);
    $aftercare mysqli_prep($_POST['aftercare']); 
    hi anthony, I see what you mean. I did it in the form validation bit. But i would still need to assign a variable here to each form element wouldnt I?

    i.e.
    PHP Code:
     $tel  $_POST['tel']; 

  8. #8
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did it in the form validation bit
    No, you did it after.
    Form validation ended before if(count($errors) == 0) line.
    As from it's name, mysqli_prep does nothing to form validation, but it belongs to SQL preparation. I's different things.

    i would still need to assign a variable here to each form element wouldn't I?
    There can be a way, if you have all field names in the $requiredfields array.
    If so, you can loop over that array and compose a query string.

    Edit:
    Another double found
    isset($_POST[$fieldname]) is unnecessary, because following empty() will check if variable is set

  9. #9
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Shrapnel_N5 View Post
    No, you did it after.
    Form validation ended before if(count($errors) == 0) line.
    As from it's name, mysqli_prep does nothing to form validation, but it belongs to SQL preparation. I's different things.


    There can be a way, if you have all field names in the $requiredfields array.
    If so, you can loop over that array and compose a query string.

    Edit:
    Another double found
    isset($_POST[$fieldname]) is unnecessary, because following empty() will check if variable is set
    thanks for that input. im getting there. All these doubles, ive decided to open both eyes when im writing code


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •