Page 1 of 1
How: Inserting 2 $variables to 1 field
Posted: Mon Aug 21, 2006 8:08 pm
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
Posted: Mon Aug 21, 2006 8:16 pm
by s.dot
Concatenate your variables?
Or, if you need them comma separated?
Or, serialize an array so you can manpiulate it later?
Code: Select all
$to_store = serialize(array($var1,$var2));
Re: How: Inserting 2 $variables to 1 field
Posted: Mon Aug 21, 2006 8:17 pm
by feyd
Code: Select all
$sql = "INSERT INTO `main` (`official_receipt`) VALUES('$OR1$OR2')";

Posted: Mon Aug 21, 2006 8:18 pm
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.
Posted: Mon Aug 21, 2006 8:27 pm
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!
Posted: Mon Aug 21, 2006 8:31 pm
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?
Re: How: Inserting 2 $variables to 1 field
Posted: Mon Aug 21, 2006 8:37 pm
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.
Posted: Mon Aug 21, 2006 8:42 pm
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.
Re: How: Inserting 2 $variables to 1 field
Posted: Mon Aug 21, 2006 8:44 pm
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
Im expecting to have:
OFFICIAL RECEIPTS
OR123
OR145
Re: How: Inserting 2 $variables to 1 field
Posted: Mon Aug 21, 2006 8:47 pm
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.
Posted: Mon Aug 21, 2006 9:06 pm
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)
Posted: Mon Aug 21, 2006 10:06 pm
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.