How: Inserting 2 $variables to 1 field

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ariseo
Forum Newbie
Posts: 8
Joined: Mon Aug 21, 2006 8:01 pm

How: Inserting 2 $variables to 1 field

Post by ariseo »

Hi,

I would like to ask.... how can i insert the value of my two variables into 1 field? Is't possible?

To further illustrate:

The values of

$OR1
$OR2

need to be inserted to my Official_Receipt field in the database.


I'm using this code:

$sql = "INSERT INTO main(official_receipt) VALUES('$or1')";

it works but i can't use this code if i make two or more variables.


Please help....... Thanks in advance
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Concatenate your variables?

Code: Select all

$to_store = $var1.$var2;
Or, if you need them comma separated?

Code: Select all

$to_store = $var1.', '.$var2;
Or, serialize an array so you can manpiulate it later?

Code: Select all

$to_store = serialize(array($var1,$var2));
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Re: How: Inserting 2 $variables to 1 field

Post by feyd »

Code: Select all

$sql = "INSERT INTO `main` (`official_receipt`) VALUES('$OR1$OR2')";
:?
KillaH425
Forum Newbie
Posts: 3
Joined: Mon Aug 21, 2006 3:38 pm

Post by KillaH425 »

What are in these variables exactly? Because you could just put some type of punctuation between the two and insert that, and then when you retrieve the data, just split the data according to that punctuation. If you are looking to simply join two variables you can just do $or3 = $or1.$or2;, but I don't know if that would accomplish much here.
ariseo
Forum Newbie
Posts: 8
Joined: Mon Aug 21, 2006 8:01 pm

Post by ariseo »

Uhm.... it is just like a billing statement..


Here it goes:

1. The encoder encodes the ORs
2. There is a page where the encoder can view csummary of encoded ORs and amout etc.


In the data entry part, im planning to give the encoder 5 fields (rows) where he can encode 5 ORs.

But my problem now is i can only store in the db the 1st entered OR.

<?php $sql = "INSERT INTO `main` (`official_receipt`) VALUES('$OR1$OR2$OR3$OR4$OR5')"; ?>

can this line help?


Thanks this site is great!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

So using that line of code still doesn't allow you to store more of these $OR variables? I'd guess "office_receipt" isn't adequately sized for more than one OR. Why do you wish to store them in a single record? Why not store them separately?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: How: Inserting 2 $variables to 1 field

Post by Benjamin »

feyd wrote:

Code: Select all

$sql = "INSERT INTO `main` (`official_receipt`) VALUES('$OR1$OR2')";
:?
What if these are integers?

I would either delimit these with commas and use the explode(), implode() functions or just use serialize & unserialize. You can look up the usage for these in the manual.
ariseo
Forum Newbie
Posts: 8
Joined: Mon Aug 21, 2006 8:01 pm

Post by ariseo »

feyd wrote:So using that line of code still doesn't allow you to store more of these $OR variables? I'd guess "office_receipt" isn't adequately sized for more than one OR. Why do you wish to store them in a single record? Why not store them separately?

So you mean i need to create another field in the table for OR2, OR3 etc..?

My table would be like this then:

-------------
OR1| OR2 |
-------------
100 | 200
200 | 300
400 | 100


If i have 10 ORs to be encoded that means I need 10 fields in my table?

I thought I can simply have one field for my OR then encode ORs as much as I want then query that field (in this case the official_receipt) and show all the saved ORs.
ariseo
Forum Newbie
Posts: 8
Joined: Mon Aug 21, 2006 8:01 pm

Re: How: Inserting 2 $variables to 1 field

Post by ariseo »

feyd wrote:

Code: Select all

$sql = "INSERT INTO `main` (`official_receipt`) VALUES('$OR1$OR2')";
:?
I tried this one and gave me this result:

I've encoded in the text field 1: OR123
in the textfield 2: OR145


The result is: OR123OR145

:lol:

Im expecting to have:

OFFICIAL RECEIPTS

OR123
OR145
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Re: How: Inserting 2 $variables to 1 field

Post by feyd »

astions wrote:What if these are integers?
No posted specification of the data contained, so I took a guess that they were to be concatenated.
astions wrote:I would either delimit these with commas and use the explode(), implode() functions or just use serialize & unserialize. You can look up the usage for these in the manual.
To give a ruling, I would need to know a lot of factors. Information as simple as their type to as complicated as what exactly they're used for. Other than being involved in a receipt of some form, I have little else to go off of. They could be item skus or credit card numbers, it's really not possible to say at the moment.


----
ariseo wrote:So you mean i need to create another field in the table for OR2, OR3 etc..?
...

If i have 10 ORs to be encoded that means I need 10 fields in my table?

I thought I can simply have one field for my OR then encode ORs as much as I want then query that field (in this case the official_receipt) and show all the saved ORs.
At this time, I see no reason why the OR values should be in the same record. My gut tells me that you need two tables. One for the receipt (whatever data that may be other than the OR values) and a table for the OR's. This OR table would have two columns that I can see: an id (that references the receipt it belongs to) and the OR value itself. There could be more fields for that table, but since I have no idea what an OR is, I can't say.
ariseo
Forum Newbie
Posts: 8
Joined: Mon Aug 21, 2006 8:01 pm

Post by ariseo »

Code: Select all

$or1 = $_REQUEST['or1'];
$or2 = $_REQUEST['or2'];
$or3 = $_REQUEST['or3'];
$or4 = $_REQUEST['or4'];

//If i use this syntax:


$sql = "INSERT INTO main(receiptno) VALUES('$or1')";


//only the value of  $or1 will be saved in my receiptno field.
//how can i possibly save the other variables? 
//Each OR has unique ID number (auto increment)
ariseo
Forum Newbie
Posts: 8
Joined: Mon Aug 21, 2006 8:01 pm

Post by ariseo »

sorry, i'll be changing my logic.. thanks for your input. i got an idea.

I'll be requiring instead the user to encode 1 OR at a time then click the SAVE button to save the whole transaction then clears the form so that he can encode another transaction.
Post Reply