Jump to content

Inserting twice into the database


NeilB

Recommended Posts

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";
        }
?>
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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

[code=php:0]
<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>
<?
}
?>
[/code]
Link to comment
Share on other sites

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. ???

[code=php:0]<?

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>";
        }
}
?>
[/code]
Link to comment
Share on other sites

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. >:(

[code=php:0]//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);[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.