Problem
I have a form table with multiple textbox and one submit button
Im using implode but all value's are inserted in array
$val = implode(',',$textboxval);
result is (80,80,80,40,80,90,70,70,70)
What i want is i want to insert each row of data's in an group of array in the database
[0]=(80,80,80),1=(40,80,90),2=(70,70,70)
RESULT
You could use array_chunk:
foreach (array_chunk($textboxval, 3) as $values) {
$val = implode(',', $values);
// insert $val into database
}
Related
I should insert an array such as $attendanceList = [18354012,18354013,18354014,18354015] and $present = "FALSE" and same for all item of $attendanceList. As you see $attendanceList is array but $present is String.
When I insert like DB::table("attendance")->insert(["id"=>$attendanceList,"present"=>"FALSE"]) returns error.
What should I do? Pairing all item of $attendanceList and $present or there are another ways?
Note: I don't want to use loop if it is possible.
You can prepare array from your data and do bulk insert in one query:
<?php
$attendanceList = [18354012,18354013,18354014,18354015];
$present = "FALSE";
$insertData = array_map(
fn($el)=>['id'=>$el, 'present'=>$present],
$attendanceList
);
$db::table("attendance")->insert($insertData);
Test Laravel DB insert online
I'm not sure why you don't want to use a loop. If you want to insert four separate rows, you're going to need one:
foreach ($attendanceList as $id) {
DB::table("attendance")->insert(["id" => $id,"present" => $present]);
}
I have some id's from the ajax code posted into a php file.it is in the format of an array.
$members=$_REQUEST['members'];
$temp=explode(',',$members);
$count=0;
foreach($temp as $value) {
$count++;
}
echo $count;
print_r($temp);
upto this the code works.I want to insert the ids to the member_id field of my database table.I tried implode function,then it shows
'undefined' as error.
I'm gonna try to be as clear as possible.
I'm currently working on a project for my work, with dynamic forms where the form inputs are in a database, and created dynamically. Every input row looks something like:
Question? Radiogroup Textinput
And when the user press submit in a section of maybe 3-5 of these rows the form is serialised and managed with AJAX. So here comes the part I'm having trouble with, as of now, in my AJAX file, there is a foreach loop which loops through all $_POST values, adds them to an array and then after the array is complete all data is inserted to a SQL database. And I don't know if it's a problem since I'm not a professional DBA, but there's a lot of rows being inserted to the database, since EVERY radio group, and EVERY text input, gets their own row. And I was thinking if I at least could cut it down to every question getting their own row, but how do I then merge the radio group value with the text input value so they are on the same row but different columns.
I hope I'm being clear enough here.
Database layout today
|ID|RowID|Type|User|Value
Where Type is either Radio or Text. What I want to achieve to get half of the rows I'm gonna get this way is:
|ID|RowID|Type|User|Radio|Text
And the problem is howto achieve this when my foreach loop fills an array with a lot of rows, for each form, which as I say, is containing maybe 3-5 rows with both a radio group and a text input. This is how I do it today:
foreach($_POST as $key => $value) {
$values[] = "'{$id}', '{$user}', '{$type}', '{$value}'";
}
$mysqli->query('INSERT INTO data (rowid, user, type, value) VALUES ('.implode('),(', $values).')');
Hope someone can help me out.
Best Regards
Cisco
Solved it by doing the following:
//Only define for testing.
$_POST['rad_1'] = "yes";
$_POST['rad_2'] = "no";
$_POST['rad_3'] = "yes";
$_POST['txt_1'] = "Test";
$_POST['txt_2'] = "Test2";
$_POST['txt_3'] = "Test3";
$array = array();
foreach($_POST as $key => $value) {
$keyarr = explode('_', $key);
$type = $keyarr[0];
$id = $keyarr[1];
$value = $value;
$array[$id][$type] = $value;
}
$values = array();
foreach($array as $key => $value)
{
$values[] = implode(', ', $value);
}
$query = "INSERT INTO table_name test VALUES (" . implode ("),(", $values) . ")";
I have a table that was dynamically created using jquery. Now I am sending the inputs, that are array, of course, to a database. The fields are customCLASSE[], customQTY[], customPRICE[]... and this is my script that inserts the data within MySQL:
if ($_POST['customCLASSE']) {
foreach ( $_POST['customCLASSE'] as $key=>$value ) {
$query = $mysqli->query("INSERT INTO booking_multiday_detail (classe) VALUES ('$value')");
}
}
It is working very good but my question is: How do I insert the other inputs (customQTY[], customPRICE[]...) within the same foreach in the same time that customCLASSE is being inserted?
$key is the index of array on looping cycle.
So if your other arrays sorted as well as customCLASSE you can access other arrays' value by $key like this,
if ($_POST['customCLASSE']) {
foreach ( $_POST['customCLASSE'] as $key=>$value ) {
$customQty = $_POST['customQTY'][$key];
$customPRICE= $_POST['customQTY'][$key];
//change the query
//....
$query = $mysqli->query("INSERT INTO booking_multiday_detail (classe) VALUES ('$value')");
}
}
i am use serialize to get data from page to another page by ajax like this :-
var txtCoursesNamewith = $('#with').serialize();
and get it on php page like this :-
$txtCoursesNamewith = ($_POST['txtCoursesNamewith']);
$unserializedData = array();
parse_str($txtCoursesNamewith,$unserializedData);
when print this array its like this :-
[txtCoursesNamewith] => Array
(
[0] => qq
[1] => ff
)
i need to insert this array to my table by foreach, i am try like this :-
foreach ($unserializedData as $value => $key )
but it store in database like this " Array ", how can i store (qq,ff) on table.
You can use implode() function.
$txtCoursesName = implode(",", $txtCoursesNamewith);
Then insert $txtCoursesName as a string.
use this
foreach ($unserializedData as $key => $val ) {
//your query insert into table_name(column_name) values ($val)
}
'Array' in dbrow means you are inserting unserialized array.
instead of:
INSERT INTO table (column) VALUES ({$value})
do:
INSERT INTO table (column) VALUES ({serialize($value)})
Iterate the query in loop or construct an array and execute a single insert query
foreach ($_POST['txtCoursesNamewith'] as $key => $val ) {
//your query insert into table_name(column_name) values ($val)
}