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 ???

Link to comment
Share on other sites

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

 

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.