How: Inserting 2 $variables to 1 field
Moderator: General Moderators
How: Inserting 2 $variables to 1 field
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
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
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 = $var1.$var2;Code: Select all
$to_store = $var1.', '.$var2;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.
- 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
Code: Select all
$sql = "INSERT INTO `main` (`official_receipt`) VALUES('$OR1$OR2')";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.
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!
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!
Re: How: Inserting 2 $variables to 1 field
What if these are integers?feyd wrote:Code: Select all
$sql = "INSERT INTO `main` (`official_receipt`) VALUES('$OR1$OR2')";
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.
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
I tried this one and gave me this result:feyd wrote:Code: Select all
$sql = "INSERT INTO `main` (`official_receipt`) VALUES('$OR1$OR2')";
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
- 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
No posted specification of the data contained, so I took a guess that they were to be concatenated.astions wrote:What if these are integers?
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.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.
----
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 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.
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)