Jump to content

Updating a Record if cartId already exists


156418

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
Link to comment
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.
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.