user1084949
user1084949

Reputation: 65

Duplicate records are being inserted into tables

I'm trying to implement the shopping cart in my project. I found a tutorial and everything works fine, but when I click on "place oder" it insert a duplicate record into the following tables: customers, orders, order_detail.

<?php
    include("includes/db.php");
    include("includes/functions.php");

    if($_REQUEST['command']=='update'){
        $name=$_REQUEST['name'];
        $email=$_REQUEST['email'];
        $address=$_REQUEST['address'];
        $phone=$_REQUEST['phone'];

        $result=mysql_query("insert into customers values('','$name','$email','$address','$phone')");
        $customerid=mysql_insert_id();
        $date=date('Y-m-d');
        $result=mysql_query("insert into orders values('','$date','$customerid')");
        $orderid=mysql_insert_id();

        $max=count($_SESSION['cart']);
        for($i=0;$i<$max;$i++){
            $pid=$_SESSION['cart'][$i]['productid'];
            $q=$_SESSION['cart'][$i]['qty'];
            $price=get_price($pid);
            mysql_query("insert into order_detail values ($orderid,$pid,$q,$price)");
        }
        die('Thank You! your order has been placed!');
    }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Billing Info</title>
<script language="javascript">
    function validate(){
        var f=document.form1;
        if(f.name.value==''){
            alert('Your name is required');
            f.name.focus();
            return false;
        }
        f.command.value='update';
        f.submit();
    }
</script>
</head>


<body>
<form name="form1" onsubmit="return validate()">
    <input type="hidden" name="command" />
    <div align="center">
        <h1 align="center">Billing Info</h1>
        <table border="0" cellpadding="2px">
            <tr><td>Order Total:</td><td><?php echo get_order_total()?></td></tr>
            <tr><td>Your Name:</td><td><input type="text" name="name" /></td></tr>
            <tr><td>Address:</td><td><input type="text" name="address" /></td></tr>
            <tr><td>Email:</td><td><input type="text" name="email" /></td></tr>
            <tr><td>Phone:</td><td><input type="text" name="phone" /></td></tr>
            <tr><td>&nbsp;</td><td><input type="submit" value="Place Order" /></td></tr>
        </table>
    </div>
</form>
</body>
</html>

Hope someone can advise. I went through the code, but I couldn't find what is causing this behavior.

Upvotes: 0

Views: 299

Answers (4)

user1084949
user1084949

Reputation: 65

Thanks all for your valuable replies and help, I solved it by removing the below line from the code and it worked.

f.submit(); 

Upvotes: 0

Akinslove
Akinslove

Reputation: 109

In your case, I think you would want to use another validation technique. Like I said before, your code is written in such a way that when the user click the 'place order' button, a validation (javascript) is triggered which produces an alert statement and still sends a message. I have modified a better solution of your validation code (mark as correct answer if it works)

<script language="javascript"> 
    function validate(){ 
        var f=document.form1; 
        if(f.name.value==''){ 
            alert('Your name is required'); 
            f.name.focus(); 
            return false; 
        } 
else
{
        f.command.value='update'; 
        f.submit();
} 
    } 
</script> 

Upvotes: 0

Akinslove
Akinslove

Reputation: 109

It is possible looking at your code for user to save transaction twice cos of your validation cos when you submit if($_REQUEST['command']=='update') will be true and an attempt will be made to insert data. Another reason could be user refreshing the page or clicking upload button more than once. hence u might wanna unset $_REQUEST['command'] when insert is complete. Let us know if that works. (mark as answer if correct)

Upvotes: 0

HPWD
HPWD

Reputation: 2240

Off the cuff, your validation function submits as well as the form. In your validation, change the f.submit(); to return true;

Upvotes: 2

Related Questions