156418 Posted October 9, 2006 Share Posted October 9, 2006 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 Quote Link to comment Share on other sites More sharing options...
alpine Posted October 9, 2006 Share Posted October 9, 2006 Study this example[code]<?phpif(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. Quote Link to comment Share on other sites More sharing options...
156418 Posted October 9, 2006 Author Share Posted October 9, 2006 Thats Brilliant, thank you.I'll have a look into the prevention methods as well Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.