I am currently working in my project and my problem starts here : I am inserting in a table and it insert correctly but the problem is it look so messy because in one id theres a lot of reference numbers they are just separated by commas , and what I want to do is use str_split to separate it...
Here is my code:
$sql = "INSERT INTO transaction_detail (`transaction_id`,`ref_number`)
VALUES ('$transaction_id','$ref_number') ";
$query = $conn->query($sql);
How can I do it? I also want to use while loop because I don't know the maximum number of ref_number? Can somebody help me? Thanks :)
UPDATE
id transaction_id ref_number
1 12 12411235435
2 12 214354657
3 12 2153564657
I want that my output will look something like this because my current output look something like this.
id transaction_id ref_number
1 12 12411235435,214354657,2153564657
UPDATE
$ref_array = explode(',' , $ref_number);
$po_array = explode(',' , $po_number);
$inv_array = explode(',' , $inv_number);
$asn_array = explode(',' , $asn_number);
$adj_array = explode(',' , $adj_number);
$amount_array = explode(',' , $amount);
// count the number of po,invoice,asn and adj
if(count($po_array) != count($ref_array) || count($inv_array) != count($ref_array) || count($asn_array) != count($ref_array) || count($adj_array) != count($ref_array) || count($ref_array) != count($amount_array)){
foreach ($ref_array as $i => $ref_num){
$po_num = isset($po_array[$i]) ? $po_array[$i] : '' ; //leave blank there is no $po_array[$i]
$inv_num = isset($inv_array[$i]) ? $invoice_array[$i] : '';
$asn_num = isset($asn_array[$i]) ? $asn_array[$i] : '' ;
$adj_num = isset($adj_array[$i]) ? $adj_array[$i] : '' ;
$amount_num = isset($amount_array[$i])? $amount_array[$i] : '';
if(intval($ref_num) != 0 ){
$conn->query ("INSERT INTO transaction_detail (`transaction_id`,`ref_number`,`po_number`,`inv_number`,`asn_number`,`adj_number`,`amount`)
VALUES ('$transaction_id','$ref_num','$po_num','$inv_num','$asn_num','$adj_num','$amount_num') " );
}
}
}
Here are the errors :
[29-Jun-2015 04:20:59 Europe/Berlin] PHP Notice: Undefined offset: 1 in C:\\xampp\\htdocs\\WebService\\webservice_revised.php on line 83
[29-Jun-2015 04:20:59 Europe/Berlin] PHP Notice: Undefined offset: 2 in C:\\xampp\\htdocs\\WebService\\webservice_revised.php on line 83
[29-Jun-2015 04:21:11 Europe/Berlin] PHP Notice: Undefined offset: 1 in C:\\xampp\\htdocs\\WebService\\webservice_revised.php on line 83
[29-Jun-2015 04:21:11 Europe/Berlin] PHP Notice: Undefined offset: 2 in C:\\xampp\\htdocs\\WebService\\webservice_revised.php on line 83
[29-Jun-2015 04:21:11 Europe/Berlin] PHP Notice: Undefined offset: 3 in C:\\xampp\\htdocs\\WebService\\webservice_revised.php on line 83
[29-Jun-2015 04:21:11 Europe/Berlin] PHP Notice: Undefined offset: 4 in C:\\xampp\\htdocs\\WebService\\webservice_revised.php on line 83
[29-Jun-2015 04:21:11 Europe/Berlin] PHP Notice: Undefined offset: 5 in C:\\xampp\\htdocs\\WebService\\webservice_revised.php on line 83
[29-Jun-2015 04:21:11 Europe/Berlin] PHP Notice: Undefined offset: 6 in C:\\xampp\\htdocs\\WebService\\webservice_revised.php on line 83
Use explode()
to split up the comma-separated reference numbers, and loop over them with a foreach
.
foreach (explode(',', $ref_number) as $refnum) {
$conn->query("INSERT INTO transaction_detail (`transaction_id`,`ref_number`)
VALUES ('$transaction_id','$refnum') ";
}
To include the PO number, explode that variable into another array, and use the index in the foreach
loop to access it:
$transaction_id = '123456';
$po_number = '1,2';
$ref_number = '11,22,33,44,55,66';
$po_array = explode(',', $po_number);
$ref_array = explode(',', $ref_number);
foreach ($ref_array as $i => $refnum) {
$ponum = isset($po_array[$i]) ? $po_array[$i] : '';
if (intval($refnum) != 0) {
$conn->query("INSERT INTO transaction_detail (`transaction_id`,`ref_number`, `po_number`)
VALUES ('$transaction_id','$refnum', '$ponum') ");
}
}
I tested the above code, it performs the following queries, with no errors or notices from PHP:
INSERT INTO transaction_detail (`transaction_id`,`ref_number`, `po_number`) VALUES ('123456','11', '1')
INSERT INTO transaction_detail (`transaction_id`,`ref_number`, `po_number`) VALUES ('123456','22', '2')
INSERT INTO transaction_detail (`transaction_id`,`ref_number`, `po_number`) VALUES ('123456','33', '')
INSERT INTO transaction_detail (`transaction_id`,`ref_number`, `po_number`) VALUES ('123456','44', '')
INSERT INTO transaction_detail (`transaction_id`,`ref_number`, `po_number`) VALUES ('123456','55', '')
INSERT INTO transaction_detail (`transaction_id`,`ref_number`, `po_number`) VALUES ('123456','66', '')
If you could also have fewer ref numbers than po numbers, you could use this code to use the longer array for the loop:
$po_array = explode(',', $po_number);
$ref_array = explode(',', $ref_number);
$limit = max(count($po_array), count($ref_array));
for ($i = 0; $i < $limit; $i++) {
$ponum = isset($po_array[$i]) ? $po_array[$i] : '';
$refnum = isset($ref_array[$i]) ? $ref_array[$i] : '';
$conn->query("INSERT INTO transaction_detail (`transaction_id`,`ref_number`, `po_number`)
VALUES ('$transaction_id','$refnum', '$ponum') ");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.