Page 1 of 1

php mysql form: empty and duplicate records added

Posted: Sun Apr 19, 2009 4:04 pm
by AliHurworth
Evening all...

Okay, I have a form on a page.
When submitted, the users entries are displayed for confirmation.
On confirmataion, the data is sent to a database (mysql) and included on an email. (It's a marketing job).

Why does the mysql get two entries?
-and-
Why does it not pick up the data from the form?

This is the PHP:
The form:

Code: Select all

<form action="process1.php" method="post" name="data" id="data">
        <table width="383" border="0" cellpadding="5" cellspacing="5">
          <caption align="top">
            <strong>About you</strong>
          </caption>
          <tr>
            <td width="163">Your name</td>
            <td width="185"><input name="yname"/></td>
          </tr>
          <tr>
            <td>Your email</td>
            <td><input name="ymail" /></td>
          </tr>
          <tr>
            <td>Your phone</td>
            <td><input name="yphone" /></td>
          </tr>
        </table>
<table  border="0" cellspacing="5" cellpadding="5">
<caption align="top">
    <strong>About the recipient</strong>
  </caption>
  <tr>
    <td width="162">Company name</td>
    <td><input name="cpname"/></td>
  </tr>
  <tr>
    <td>Contact name</td>
    <td><input name="ctname" length="25" /></td>
  </tr>
  <tr>
    <td>Contact phone</td>
    <td><input name="ctphone" /></td>
  </tr>
  <tr>
    <td>Email</td>
    <td><input name="email" /></td>
  </tr>
  </table>
  <table  border="0" cellspacing="5" cellpadding="5">
  <caption align="top">
    <strong>About the message</strong>
  </caption>
   <tr>
    <td>Type of message</td>
    <td><p>
      <label>
        <input type="radio" name="RadioGroup1" value="1" id="RadioGroup1_0" />
        Few Appts</label>
      <br />
      <label>
        <input type="radio" name="RadioGroup1" value="2" id="RadioGroup1_1" />
        Company Recruitment</label>
      <br />
    </p></td>
  </tr>
  </table>
  <table border="0" cellspacing="5" cellpadding="5">
  <caption align="top">
    <strong>About the session</strong>
  </caption>
  <tr>
    <td>Session to promote</td>
    <td><input name="session" /></td>
  </tr>
  <tr>
    <td>Date of session</td>
    <td><input name="date" id="date"><a href="javascript&#058;NewCal('date','ddmmyyyy')"><img src="calendar/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a></td>
  </tr>
  <tr>
    <td><p>Any general notes <br />
        <span class="style3"><em>not for message</em></span><em></em></p>
      </td>
    <td><textarea name="comments" cols="25" rows="5" id="comments"></textarea></td>
  </tr>
  <tr>
    <td><input type="reset" value="reset" /></td>
    <td><input type="submit" value="send!" /></td>
  </tr>
  
</table>
 
    </form>
 
This is the confirmation page:

Code: Select all

 
<div id="container" style="background-color:#FF0000; font-family:Trebuchet MS, Arial, sans-serif; vertical-align:middle; background-image: url(bbl1.jpg); background-repeat: repeat-x;">
<?php
        // details sent from form
            $yname=$_POST['yname'];
            $ymail=$_POST['ymail'];
            $yphone=$_POST['yphone'];
            $cpname=$_POST['cpname'];
            $ctname=$_POST['ctname'];
            $email=$_POST['email'];
            $type=$_POST['RadioGroup1'];
            $session=$_POST['session'];
            $date=$_POST['date'];
    ?>
    <div style="background-color:#CC6666; color:#FFFFFF">
        <h2>Sender</h2>
            <?php 
                echo 'Your message is from '.$yname.' at '.$ymail.'.';
            ?>
        <h2>Company</h2>
            <?php echo 'You want to send a message to '.$ctname.' at '.$cpname.'. <br />';
                    echo 'Email addy: '.$email;
            ?>
        <h2>Campaign</h2>
            <?php echo 'Message Type: '.$ctname.' at '.$cpname.'. <br />';
                    echo 'Session: '.$session.'. <br />';
                    echo 'Date: '.$date.'. ';
            ?>
    </div>
<form action="preview.php" method="post">
<input type="hidden" value="<?php echo $yname; ?>" name="yname" /> 
<input type="hidden" value="<?php echo $ymail; ?>" name="ymail" />
<input type="hidden" value="<?php echo $yphone; ?>" name="yphone" /> 
<input type="hidden" value="<?php echo $cpname; ?>" name="cpname" />
<input type="hidden" value="<?php echo $ctname; ?>" name="ctname" /> 
<input type="hidden" value="<?php echo $email; ?>" name="email" />
<input type="hidden" value="<?php echo $type; ?>" name="RadioGroup1" />
<input type="hidden" value="<?php echo $session; ?>" name="session" /> 
<input type="hidden" value="<?php echo $date; ?>" name="date" /> 
    <input name="prvw" id="prvw" value="Preview and send!" type="submit"/>
    <input type="button" value="No, start again" ONCLICK="history.go(-1)"/>
</form>
 
 
 
  <div style="background-image:url(water.jpg); vertical-align:top" alt="water" width="125" /> 
    <img src="nbs_logo_sm.gif" alt="National Blood Service logo" /> <br />
     <center class="style1">
      <span style="color:#FFFFFF; font-size: large; color:#FF0000;"><strong>Save a life Give Blood</strong></span>
      <span style="color:#FFFFFF; font-size:x-large">FIND OUT MORE</span>
 
 
and this is how it is processed. The email part isn't finished.

Code: Select all

<div id="main_text" style="padding:inherit;"><!--main text-->
    <?php 
            //details sent from form
            $yname=$_POST['yname'];
            $ymail=$_POST['ymail'];
            $yphone=$_POST['yphone'];
            $cpname=$_POST['cpname'];
            $ctname=$_POST['ctname'];
            $ctphone=$_POST['ctphone'];
            $email=$_POST['email'];
            $type=$_POST['RadioGroup1'];
            $session=$_POST['session'];
            $date=$_POST['date'];
            $notes=$_POST['notes'];
                    
            $to = $email;
            $subject = "12 seconds";
 
            //get file according to radio selection
            $myFile = "header".$type.".txt";
            $fh = fopen($myFile, 'r');
            $theData = fread($fh, filesize($myFile));
            fclose($fh);
 
 
  error_reporting(E_ALL);
    /*login to mysql*/
    require_once 'mysql_login.php';
    mysql_select_db("mktg",$cid);
    
    /*create query*/
        $sql = "INSERT INTO `data` (`date`, `yname`, `ymail`, `cpname`, `ctname`, `email`, `ctphone`, `msg_type`, `session`, `session_date`, `notes`) VALUES(NOW(),'$yname','$ymail','$cpname','$ctname','$email','$ctphone','$type','$session','$date','$notes')";
        $result=mysql_query($sql, $cid) or die(mysql_error());
        if (mysql_query($sql, $cid))    {
        echo 'Entered on database';
        } else  {
        echo 'Please contact the help desk!';
        }
        
    ?>
    
    
        </div>
      
<hr />
To send another mail, click here. To execute this application, click here
<!--(date, yname, ymail, cpname, ctname, email, ctphone, msg_type, session, session_date, notes)-->
</div>
 

Re: php mysql form: empty and duplicate records added

Posted: Mon Apr 20, 2009 8:06 am
by MeLight
two entries are because of this (lines 33, 34 in your processing code - last file):

Code: Select all

 
$result=mysql_query($sql, $cid) or die(mysql_error());
[color=#FF0000]if (mysql_query($sql, $cid))[/color]    { //you're executing the $sql query AGAIN
//do stuff here
}
 
You're executing the $sql query twice, instead you should check the $result value to see if it's good.

Code: Select all

 
$result=mysql_query($sql, $cid) or die(mysql_error());
[color=#FF0000]if ($result)[/color]    { //you're only checking the query return value
//do stuff here
}