Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Concat Poscode

Status
Not open for further replies.

andyb2005

IS-IT--Management
Feb 9, 2005
58
0
0
GB
Hi

I have a UK postcode broken down into two database fields:
postcode1 and postcode2.

What I want to be able to do is to query the database from a form and return customers from the complete postcode entered on the web form.

I am trying this sort of thing at the moment but it's not happening. Just getting an error. "you have an error in your SQL syntax "

Code:
$query= "SELECT CONCAT('postcode1', 'postcode2') AS c_postcode FROM customer
WHERE c_postcode = '$_POST[new_search]";

Am I nearly there? Any help appreciated! Many thanks!
 
This is more a MySQL question than a PHP question, but what the hey. You shouldn't put column names in qoutes as that will concat literal strings then. Also, you should close the single quote at the end and move the POST variable reference out of the qoutes. Try changing your query to:
Code:
$query= "SELECT CONCAT(postcode1, postcode2) AS c_postcode FROM customer WHERE c_postcode = '" . $_POST[new_search] . "'";
 
Since this is a PHP forum and Vrag was kind enough to provide a query answer, I'll provide a PHP answer.
Code:
$results is your row obtained from the something like mysql_fetch_array or mysql_fetch_assoc.

$postcode=$results['postcode1'] . $results['postcode2'];
echo $postcode;

This echo out the entire postcode together.






----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi

What I ended up doing was:

Code:
$query= "SELECT * FROM customer WHERE postcode1 = '$_POST[postcode1]' AND postcode2 = '$_POST[postcode2]'";

And just having two form fields for each part of the postcode. But thanks anyway guys!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top