Jump to content


Photo

Updating a Record if cartId already exists


  • Please log in to reply
2 replies to this topic

#1 156418

156418
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 09 October 2006 - 02:05 PM

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

<?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());
?>

Many thanks

#2 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 09 October 2006 - 02:39 PM

Study this example
<?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;
}
?>

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

#3 156418

156418
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 09 October 2006 - 06:08 PM

Thats Brilliant, thank you.

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users