Jump to content

Archived

This topic is now archived and is closed to further replies.

156418

Updating a Record if cartId already exists

Recommended Posts

I have the following script, which updates a database table with the values posted though, is there an easy I can add to that so that if the cartId is already in the CalcAdult table it updates the record for it, rather than add another one, the structure of the table is the 4 fields as below, plus a CalcID primary key which auto_incerements

[code]<?php
$cartId = $_POST['cartId'];
$AdultQuantity = $_POST['AdultQuantity'];
$ATicketType = $_POST['ATicketType'];
$APrice = $_POST['APrice'];
$ChildQuantity = $_POST['ChildQuantity'];
$CTicketType = $_POST['CTicketType'];
$CTicketPrice = $_POST['CPrice'];
$PresentQuantity = $_POST['PresentQuantity'];
$PTicketType = $_POST['PTicketType'];
$PTicketPrice = $_POST['PPrice'];


//1) Add Adult Details to the CalcAdult Table
  $query = "INSERT INTO CalcAdult (
            cartId, AdultQuantity, ATicketType, APrice)
            VALUES (
            '$cartId',
            '$AdultQuantity',
            '$ATicketType',
            '$APrice')";
$insert = mysql_query($query)
    or (mysql_error());
?> [/code]

Many thanks

Share this post


Link to post
Share on other sites
Study this example
[code]
<?php

if(isset($_POST['cartId']))
{
$cartId = $_POST['cartId'];
$AdultQuantity = $_POST['AdultQuantity'];
$ATicketType = $_POST['ATicketType'];
$APrice = $_POST['APrice'];
$ChildQuantity = $_POST['ChildQuantity'];
$CTicketType = $_POST['CTicketType'];
$CTicketPrice = $_POST['CPrice'];
$PresentQuantity = $_POST['PresentQuantity'];
$PTicketType = $_POST['PTicketType'];
$PTicketPrice = $_POST['PPrice'];

$check = mysql_query("SELECT CalcID FROM CalcAdult WHERE cartId = '$cartId'") or die(mysql_error());
if(mysql_num_rows($check) == "1")
{
// one row with cartId like posted value where found, update
  $query = "UPDATE CalcAdult SET(
            cartId, AdultQuantity, ATicketType, APrice)
            VALUES (
            '$cartId',
            '$AdultQuantity',
            '$ATicketType',
            '$APrice') where cartId = '$cartId'";
$insert = mysql_query($query)
    or (mysql_error());
   
    $message = "Record updated";
}
else
{
//1) Add Adult Details to the CalcAdult Table
  $query = "INSERT INTO CalcAdult (
            cartId, AdultQuantity, ATicketType, APrice)
            VALUES (
            '$cartId',
            '$AdultQuantity',
            '$ATicketType',
            '$APrice')";
$insert = mysql_query($query)
    or (mysql_error());
   
    $message = "New record added";
}

echo $message;
}
?>
[/code]

You should also consider using mysql_real_escape_string(), htmlspecialchars() or similar on posted data to prevent sql injection.

Share this post


Link to post
Share on other sites
Thats Brilliant, thank you.

I'll have a look into the prevention methods as well

Share this post


Link to post
Share on other sites

×

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.