Jump to content

[SOLVED] Any Better Way I Can Present this PHP SQL Code?


stublackett

Recommended Posts

Hi,

 

I've collected POST VARS using

 

<?php
extract($_POST);
?>

 

Then when it comes to the SQL Insert, I'm actually inserting those variables into the Database... But the code to do it seems huuuuuuuuge or far too big anyways

 

<?php
{
	$sql = "INSERT INTO $db_table(experiencename,sendertitle,senderforename,sendersurname,senderaddress,senderaddress1,sendertown,sendercounty,sendertelephone,senderemail,recipient1title,recipient1forename,recipient1surname,recipient1address,recipient1address1,recipient1town,recipient1county,recipient1telephone,recipient1email,recipient2title,recipient2forename,recipient2surname,recipient2address,recipient2address1,recipient2town,recipient2county,recipient2telephone,recipient2email,recipient3title,recipient3forename,recipient3surname,recipient3address,recipient3address1,recipient3town,recipient3county,recipient3telephone,recipient3email,recipient4title,recipient4forename,recipient4surname,recipient4address,recipient4address1,recipient4town,recipient4county,recipient4telephone,recipient4email,numberforreducedrate,total) values ('$description','$sendertitle','$senderforename','$sendersurname','$senderaddress','$senderaddress1','$sendertown','$sendercounty','$sendertelephone','$senderemail','$title1' '$recipientitle1' '$recievertitle1' '$fortitle','$firstname1' '$recipientname1' '$recievername1' '$forfirstname','$surname1' '$recipientsurname1' '$recievername11' '$forsurname','$address1' '$recipientaddress1' '$recieveraddress1' '$foraddress','$address11' '$recipientaddress11' '$recieveraddress11' '$foraddress1','$town1' '$recipienttown1' '$recievertown' '$fortown','$county1' '$recipientcounty1' '$recievercounty1' '$forcounty','$telephone1' '$recipientelephone1' '$recievertelephone1' '$fortelephone','$email1' '$recipientemail' '$recieveremail1' '$foremail','$title2' '$recipienttitle2' '$recievertitle2','$firstname2' '$recipientname2' '$recievername2','$surname2' '$recipientsurname2' '$recievername21','$address2' '$recipientaddress2' '$recieveraddress2','$address21' '$recipientaddress21' '$recieveraddress21','$town2' '$recipienttown2','$county2' '$recipientcounty2' '$recievercounty2','$telephone2' '$recipientelephone2' '$recievertelephone2','$email2' '$recipientemail2' '$recieveremail2','$title3' '$recipienttitle3','$firstname3' '$recipientname3','$surname3' '$recipientsurname3','$address3' '$recipientaddress3','$address31' '$recipientaddress31','$town3' '$recipienttown3','$county3' '$recipientcounty3','$telephone3' '$recipientelephone3','$email3' '$recipientemail3','$title4','$firstname4','$surname4','$address4','$address41','$town4','$county4','$telephone4','$email4','$reducedrate','$total')"; 
	($result = mysql_query($sql ,$conn) or die(mysql_error()));
}
?>

 

Surely theres a better way to do code that ???

if you are inserting a value into every column, you don't need to list the columns. but if you aren't inserting into every column, you will need to list columns as you have done. i would definitely mysql_real_escape_string() all values before insert.

Not really.

I mean, you could some string building using arrays, but that would be just obfuscating things...

 

When dealing with such queries I tend to break them into sever lines for ease of reading.

 

INSERT INTO 
$db_table
(experiencename,sendertitle,senderforename,sendersurname,senderaddress,senderaddress1,sendertown,sendercounty,sendertelephone,senderemail,
recipient1title,recipient1forename,recipient1surname,recipient1address,recipient1address1,recipient1town,recipient1county,recipient1telephone,recipient1email,
recipient2title,recipient2forename,recipient2surname,recipient2address,recipient2address1,recipient2town,recipient2county,recipient2telephone,recipient2email,
recipient3title,recipient3forename,recipient3surname,recipient3address,recipient3address1,recipient3town,recipient3county,recipient3telephone,recipient3email,
recipient4title,recipient4forename,recipient4surname,recipient4address,recipient4address1,recipient4town,recipient4county,recipient4telephone,recipient4email,
numberforreducedrate,total) 
VALUES
('$description','$sendertitle','$senderforename','$sendersurname','$senderaddress','$senderaddress1','$sendertown','$sendercounty','$sendertelephone','$senderemail',
'$title1','$recipientitle1' '$recievertitle1' '$fortitle','$firstname1' '$recipientname1' '$recievername1' '$forfirstname','$surname1' '$recipientsurname1' '$recievername11' '$forsurname','$address1' '$recipientaddress1' '$recieveraddress1' '$foraddress','$address11' '$recipientaddress11' '$recieveraddress11' '$foraddress1','$town1' '$recipienttown1' '$recievertown' '$fortown','$county1' '$recipientcounty1' '$recievercounty1' '$forcounty','$telephone1' '$recipientelephone1' '$recievertelephone1' '$fortelephone','$email1' '$recipientemail' '$recieveremail1' '$foremail',
'$title2' '$recipienttitle2' '$recievertitle2','$firstname2' '$recipientname2' '$recievername2','$surname2' '$recipientsurname2' '$recievername21','$address2' '$recipientaddress2' '$recieveraddress2','$address21' '$recipientaddress21' '$recieveraddress21','$town2' '$recipienttown2','$county2' '$recipientcounty2' '$recievercounty2','$telephone2' '$recipientelephone2' '$recievertelephone2','$email2' '$recipientemail2' '$recieveremail2',
'$title3' '$recipienttitle3','$firstname3' '$recipientname3','$surname3' '$recipientsurname3','$address3' '$recipientaddress3','$address31' '$recipientaddress31','$town3' '$recipienttown3','$county3' '$recipientcounty3','$telephone3' '$recipientelephone3','$email3' '$recipientemail3',
'$title4','$firstname4','$surname4','$address4','$address41','$town4','$county4','$telephone4','$email4',
'$reducedrate','$total')

 

It also looks like you could normalise this table...

 

Archived

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

×
×
  • 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.