Jump to content


Photo

Inserting twice into the database


  • Please log in to reply
12 replies to this topic

#1 NeilB

NeilB
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 09 October 2006 - 08:26 AM

I'm trying to get an insert to sucessfully work with this code, it does insert the data but it does it twice. I've been looking at the code for ages but I can't see the woods for the tree's. :( Has anyone got any idea's as to why it's playing silly beggers?

if(isset($_POST["submit"])){

        $connection = mysql_connect("", "", "");

        mysql_select_db("car_rental", $connection)
        or die("Unable to select database");

        $query = "INSERT INTO customer VALUES ('','$title','$fname','$lname','$housenum','$street','$town','$postcode', '$email', '$phonenum', '$make', '$model','$warrenty','$card_num','$type','$expires')";

        mysql_query($query);


        echo mysql_error().

        mysql_close($connection);

        Print "Thank you for your order";
        }
?>

#2 zyonsherlock

zyonsherlock
  • Members
  • PipPip
  • Member
  • 10 posts
  • LocationPhilippines

Posted 09 October 2006 - 08:41 AM

where are the variables coming from? ???

#3 NeilB

NeilB
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 09 October 2006 - 09:51 AM

The variables are coming from a HTML form on another page. But I can't see anything on there to make it insert twice either. ???

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 09 October 2006 - 07:08 PM

Sounds like the form is being submitted twice.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#5 tzuuyi

tzuuyi
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 13 October 2006 - 09:44 PM

I had this twice.  I don't know why php codes does this.  I guessed php loading the page in a different order.  The first time I got this, I let php load php codes first and html last.  The second time I got this, actually, right now, I got two forms doing post and I am not sure what to put it.

#6 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 13 October 2006 - 10:42 PM

I'm not sure I understand what you mean by "php codes" and loading order.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#7 NeilB

NeilB
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 02 November 2006 - 10:08 AM

>:( Arggggh I'm still getting nowhere with this, the PHP looks fine could it be something to do with the HTML form?

<form method="POST" action="order.php">
Title<br>
<input type="text" name="title" size="20" value="<? echo $title; ?>"><p><p>
First name<br>
<input type="text" name="first_name" size="20" value=""><p>
Last name<br>
<input type="text" name="last_name" size="20" value=""><p>
House Number<br>
<input type="text" name="house_num" size="20" value=""><p>
Street<br>
<input type="text" name="street" size="20" value=""><p>
Town<br>
<input type="text" name="town" size="20" value=""><p>
Postcode<br>
<input type="text" name="postcode" size="20" value=""><p>
e-mail<br>
<input type="text" name="email" size="20" value=""><p>
Phone Number<br>
<input type="text" name="phone" size="20" value=""><p>
Qty<br>
<input type="text" name="qty" size="20" value=""><p>
<img SRC="images/payment.png" ALT="Logo" ><p>
Card number<br>
<input type="text" name="card_num" size="20" value=""><p>
Type<br>
<select name="type">
<option>Delta</option>
<option>Mastercard</option>
<option>Maestro</option>
<option>Visa</option>
<option>Visa Electron</option>
</select><p>
expires<br>
<input type="text" name="expires" size="20" value=""><p>
<?
//Create the drop down menu from the database
$database ="****";
mysql_connect("****","****","****")
   or die("Unable to connect to server");
mysql_select_db($database) or die ("Unable to connect to database");

$query1 = "SELECT DISTINCT name FROM sims";

$result1 = mysql_query($query1)
or die ("couldnt execute query 1.");

echo "Select Simcard <br> <select name='provider'>\n";

while ($row = mysql_fetch_array($result1))
{
echo "<option value=\"{$row[name]}\">$row[name]</option>\n";
}
echo "</select>\n";

echo  "<br>";

?>

<input type="submit" value="Buy" name="submit">
</form>
<?
}
?>


#8 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 02 November 2006 - 06:52 PM

Nope... the form has no idea it's being submitted.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#9 gluck

gluck
  • Members
  • PipPipPip
  • Advanced Member
  • 216 posts

Posted 02 November 2006 - 07:29 PM

you don't seem to be submitting through html
I'm back baby!

#10 NeilB

NeilB
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 03 November 2006 - 12:08 PM

At least that has narrowed it down to the order.php piece of coding, however I cannot see why it would enter twice though, as the same code just slightly modified enters just once into another database. ???

<?

session_start();

$stocknumber = $_SESSION["stocknumber"];
$origstocknumber = $_SESSION["stocknumber"];
$stocknumber = $stocknumber - $_POST['qty'];

$make = $_SESSION["make"];
$model = $_SESSION["model"];

//This script looks at validating what the customer has entered
function out_of_range($value,$min=1,$max=25)
{
        return (strlen(trim($value))<$min || strlen(trim($value))>$max);
}

if(isset($_POST["submit"]))
{
        $error_msg = array();
        if(out_of_range($_POST['title']))
        {
           $error_msg[] = "Please enter a valid title";
        } else {
                // check if textbox is using numerical values only
                if(!ereg("[a-zA-Z]", $_POST['title'])) $error_msg[].="Please enter a valid title<br>";
        }
        if(out_of_range($_POST['first_name']))
        {
           $error_msg[] = "Please enter a valid first name";
        } else {
                // check if textbox is using numerical values only
                if(!ereg("[a-zA-Z]", $_POST['first_name'])) $error_msg[].="Please enter a valid first name<br>";
        }
        if(out_of_range($_POST['last_name']))
        {
           $error_msg[] = "Please enter a valid last name";
        } else {
                // check if textbox is using numerical values only
                if(!ereg("[a-zA-Z]", $_POST['last_name'])) $error_msg[].="Please enter a valid last name<br>";
        }
        if(out_of_range($_POST['house_num']))
        {
           $error_msg[] = "Please enter a valid house number";
        }
        if(out_of_range($_POST['street']))
        {
           $error_msg[] = "Please enter a valid street";
        } else {
                // check if textbox is using numerical values only
                if(!ereg("[a-zA-Z]", $_POST['street'])) $error_msg[].="Please enter a valid street<br>";
        }
        if(out_of_range($_POST['town']))
        {
           $error_msg[] ="Please enter a valid town";
        } else {
                // check if textbox is using numerical values only
                if(!ereg("[a-zA-Z]", $_POST['town'])) $error_msg[].="Please enter a valid town<br>";
        }
        if(out_of_range($_POST['postcode']))
        {
           $error_msg[] = "Please enter a valid postcode";
        } else {
                // check if email is in a valid format (LN NLL or LLN NLL)
                if(!ereg("^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}$", $_POST['postcode'])) $error_msg[].="Please enter a valid postcode<br>";
        }
        if(out_of_range($_POST['email']))
        {
           $error_msg[] = "Please enter a valid email address";
        } else {
                // check if email is in a valid format using a regular expression
                if(!ereg("[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]", $_POST['email'])) $error_msg[].="Please enter a valid email address<br>";
        }
        if(out_of_range($_POST['phone']))
        {
           $error_msg[] = "Please enter a valid phone number";
        } else {
                // check if textbox is using numerical values only
                if(!ereg("[0-9]", $_POST['phone'])) $error_msg[].="Please enter a valid phone number<br>";
        }
        if(out_of_range($_POST['qty']))
        {
           $error_msg[] = "Please enter a valid quantity";
        } else {
          if($_POST['qty'] > $origstocknumber)
          $error_msg[] = "Please enter a valid quantity";
        
        //} else {
                // check if textbox is using numerical values only
                //if(!ereg("[0-9]", $_POST['qty'])) $error_msg[].="Please enter a valid quantity*<br>";
        }
        if(out_of_range($_POST['provider']))
        {
           $error_msg[] = "Please enter a valid provider";
        }
        if(out_of_range($_POST['card_num']))
        {
           $error_msg[] = "Please enter a valid card number";
        //} else {
                // check if email is in a valid format using a regular expression
        //        if(!ereg("^(\d{4}[- ]){3}\d{4}|\d{16}$", $_POST['card_num'])) $error_msg[].="Please enter a card number*<br>";
        }
        if(out_of_range($_POST['type']))
        {
           $error_msg[] = "Please enter a valid type of card";
        }
        if(out_of_range($_POST['expires']))
        {
          $error_msg[] = "Please enter a valid expiry date";
        } else {
                // check if textbox is using numerical values only
                if(!ereg("[0-9]", $_POST['expires'])) $error_msg[].="Please enter a valid expirey date<br>";
        }
        if(count($error_msg)<=0)
        {

        //Database entry for the customer order and customer details.
        $title = mysql_real_escape_string($_POST["title"]);
        $fname = mysql_real_escape_string($_POST["first_name"]);
        $lname = mysql_real_escape_string($_POST["last_name"]);
        $housenum = mysql_real_escape_string($_POST["house_num"]);
        $street = mysql_real_escape_string($_POST["street"]);
        $town = mysql_real_escape_string($_POST["town"]);
        $postcode = mysql_real_escape_string($_POST["postcode"]);
        $email = mysql_real_escape_string($_POST["email"]);
        $phone = mysql_real_escape_string($_POST["phone"]);
        $qty = mysql_real_escape_string($_POST["qty"]);
        $provider = mysql_real_escape_string($_POST["provider"]);
        $card_num = mysql_real_escape_string($_POST["card_num"]);
        $type = mysql_real_escape_string($_POST["type"]);
        $expires = mysql_real_escape_string($_POST["expires"]);

        $connection = mysql_connect("***", "***", "***");

        mysql_select_db("***", $connection)
        or die("Unable to select database");

        $query = "INSERT INTO customer VALUES ('','$title','$fname','$lname','$housenum','$street','$town','$postcode', '$email', '$phone', '$qty', '$make', '$model','$provider','$card_num','$type','$expires')";

        mysql_query($query);


        echo mysql_error().

        mysql_close($connection);

        Print "Thank you for your order";
        //}

        //Update totals
        $connection = mysql_connect("***", "***", "***");

        mysql_select_db("***", $connection)
        or die("Unable to select database");

        $result = mysql_query("UPDATE mobile SET stock='$stocknumber' WHERE model='$model'")
        or die(mysql_error());

        mysql_query($query);


        echo mysql_error().

        mysql_close($connection);
                //Show any error messages for the user to see in red text
}
        else {
                echo "<span style='font-weight: bold; color: red;'>Error(s):</span><br /><ul><li>";
                echo join('</li><li>',$error_msg);
                echo "</ul>";

        //Shows data within variables if needed during testing
        //foreach($_POST as $key => $value) echo "$key => $value <br>";
        }
}
?>


#11 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 03 November 2006 - 12:41 PM

Well, you have

mysql_query($query);

_after_ the UPDATE statement as well; but at this point, the $query variable still holds the previous INSERT statement.  Also, you're connecting twice for no reason.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#12 NeilB

NeilB
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 03 November 2006 - 04:54 PM

Ah thanks, couldn't see the wood for the tree's there. I'm trying to get the update command to now work that was causing the problem, but it won't accept it now I have renamed it no error messages or anything. >:(

//Update totals
        $connection2 = mysql_connect2("***", "***", "***");

        mysql_select_db2("neil", $connection2)
        or die("Unable to select database");

        $result = mysql_query2("UPDATE mobile SET stock='$stocknumber' WHERE model='$model'")
        or die(mysql_error());

        mysql_query2($query2);


        echo mysql_error().

        mysql_close($connection2);


#13 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 04 November 2006 - 12:49 AM

You're never assigning the UPDATE statement to $query2.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users