I have to insert multiple records in mysql from php. How to pass list to mysql table.Please suggest your solution.
$course[0] = 1;
$course[1] = 'test';
$course[2] = 'test1'
$sql = "INSERT INTO temp_course(uniqueId,fullName,shortName) VALUES($course[0],'$course[1]','$course[2]')";
i tried like above. but values not inserted.
#Gopal you can try with implode() but make sure your array sequence must be same as your table column sequence given by you in query, try like below:
<?php
$course[0] = 1;
$course[1] = 'test';
$course[2] = 'test1';
$sql = "INSERT INTO temp_course(uniqueId,fullName,shortName) VALUES(".implode(',', $course).")";
Try wrapping the variables with curly brackets like this:
$course = [];
$course[0] = 1;
$course[1] = 'test';
$course[2] = 'test1';
$sql = "INSERT INTO temp_course(uniqueId,fullName,shortName) VALUES($course[0],'$course[1]','$course[2]')";
Related
I am trying to store two fields from the JSON data found here in a mysql database. First I create PHP arrays for the data I want using:
$o = file_get_contents("vixData.json");
$o = json_decode($o);
$date = [];
$close = [];
$set = $o->dataset->data;
foreach($set as $pos)
{
array_push($date,$pos[0]);
array_push($close,$pos[4]);
}
Works fine. Now I am trying to adapt This question on inserting multiple rows via a php array. First I implode my arrays:
$date = implode(",", $date);
$close = implode(",", $close);
Then try to insert to the db using:
$sql = "INSERT INTO vix (date,close) VALUES (".$date.",".$close.")";
if (mysqli_multi_query($dbc, $sql)) {
echo "VIX Load Successful";
} else {
echo "VIX Load Error";
}
I'm seeing my load error. There is no database connection issue because $dbc is used earlier in my script. Trying to debug; is my SQL INSERT statement valid? Can anyone see the issue?
Kind regards,
Just looking at it quickly, it seems your values are not wrapped in quotes in your SQL. Try this:
$sql = "INSERT INTO vix (date,close) VALUES ('".$date."','".$close."')";
Removing the concat operator (.) will result in the same.
There are two issues in your code.
As mentioned other mate you need to use quotes for Date string.
Second you can not use mysqli_multi_query() as like that.
Modified Code:
You can use multiple INSERT Statement as like that:
$o = file_get_contents("vixData.json");
$o = json_decode($o);
$date = [];
$close = [];
$set = $o->dataset->data;
foreach($set as $pos)
{
array_push($date,$pos[0]);
array_push($close,$pos[4]);
}
$sql = "";
foreach ($date as $key => $value) {
$sql .= "INSERT INTO vix (date,close) VALUES ('".$value."','".$close[$key]."'); ";
}
if (mysqli_multi_query($dbc, $sql)) {
echo "VIX Load Successful";
} else {
echo "VIX Load Error";
}
Remove the double quotes and concatinator wrapping your Values.
$sql = "INSERT INTO vix (date,close) VALUES ('$date','$close')";
So i'm trying to insert data into a MySQL table from an array that contains multiple arrays which hold data for each row of a table using the code below:
if (is_array($tbl_data)){
$sql = "INSERT INTO teshsting (agent, event , data1,data2,data3,data4,data5) values ";
$arrayValues = array();
foreach ($tbl_data as $row){
$agent = mysql_real_escape_string($row[0]);
$event = mysql_real_escape_string($row[1]);
$data1 = mysql_real_escape_string($row[2]);
$data2 = mysql_real_escape_string($row[3]);
$data3 = mysql_real_escape_string($row[4]);
$data4 = mysql_real_escape_string($row[5]);
$data5 = mysql_real_escape_string($row[6]);
$value = "($agent,$event,$data1,$data2,$data3,$data4,$data5)";
array_push($arrayValues, $value);
}
$sql .=implode(',', $arrayValues);
Quick check using var_dum($sql) produces the following:
INSERT INTO teshsting (agent, event , data1,data2,data3,data4,data5) values (NONE,QUEUESTART,,,,,)/////
The above Sql is invalid due to the multiple commas here :(NONE,QUEUESTART,,,,,)which are generated by empty fields. How can I insert single quotations inside the query to make it valid ? i.e the correct sql syntax:
INSERT INTO teshsting (agent, event , data1,data2,data3,data4,data5) values (NONE,QUEUESTART,'','','','','')
Either:
$data1 = "'" . mysql_real_escape_string($foo) . "'";
or
$value = "(...,'$data1',...)";
Just don't try both options, which would give you (...,''$data1'',...) and kill the query with syntax errors.
I have this sql query that returns no result. The table it queries has data but no results being pull. The query is put into an array.
$qry = array();
$qry[] = "SELECT events_id as 'Reference ID', event_level as 'Level', events_date as 'Date', events_time as 'Time', events_opponent as 'Opponent', events_place as 'Place', events_results as 'Results'";
$qry[] = "FROM wp_events WHERE events_id = ".$sched_id."";
$val = array();
$val = implode(" ", $qry);
$result = $wpdb->get_results($val, ARRAY_A);
i var_dump the $result but it only output Array ( ). I also tried to var_dump($val) if there is something wrong on the query but query is ok. I don't know what im missing here. please help.
There are two points in this code which can remain problematic:
Do you always have the $sched_id filled?
Does passing a complete query string to the $wpdb->get_results() return anything?
Try doing a fully complete query in phpMyAdmin to see the expected result and work the PHP code until you have the same results back.
try something like this if u want to print variable values..
<?php
$id = $_GET['value'];//value received from array[]
$N = count($id);
for($i=0; $i <N; $i++)
{
$result_h = mysql_query("SELECT * FROM `table` where id='$id[$i]'");
$pks_h = mysql_fetch_array($result_h);
echo $pks_h['mysql coloumn name'];
}
?>
or use while loop if u want to print only mysql table value.
I have a query that's retraive a list of id's. Those id's are in an array and i need to save it in table with those id's. I tried using implode to make those id's a string i could use in a where clause but i keep getting this error.
$save_food = $_POST['save_food'];
$unserializedData = array();
parse_str($save_food,$unserializedData);
foreach($unserializedData as $unserializedData1){
$query = mysql_query("insert into subscribefood (s_user_id,s_food) values ('$ft_user_id','".implode($unserializedData1, ',')."')");
}
Try this
<?php
$save_food = $_POST['save_food'];
$unserializedData = array();
parse_str($save_food,$unserializedData);
$datalist = $unserializedData['foodtype'];
foreach($datalist as $data){
$query = mysql_query("insert into subscribefood (s_user_id,s_food) values ('$ft_user_id','$data')");
}
?>
I'm having an issue inserting an array into my database. When i tried implode, it all inserted all the arrays into one row. What i intend to achieve is to insert a single array into different rows.
In the array i have fields such as bag,shoes,cloths and i wish to insert into one table but different rows.
$subjectArray[] =$_POST['subject'];
$all_subjects_to_insert = array();
foreach ($subjectArray as $p){
if(!empty($p)) $all_subjects_to_insert = $p;
}
$all_subjects_to_insert = array_unique($all_subjects_to_insert);
$final = implode(',',$all_subjects_to_insert);
$query = "INSERT INTO #__sch_subject (subject) VALUES ('$final')";
I'll appreciate any help i get. Thanks.
if $all_subjects_to_insert is multidimensional array then what you are looking for is
foreach($all_subjects_to_insert as $value)
{
$query = "INSERT INTO #__sch_subject (subject) VALUES ('{$value}')";
mysql_query($query);
}
Reference
Thanks everyone. Its now working. The separator for the implode function was incorrect. Here is the working code
$subjectArray[] =$_POST['subject'];
$all_subjects_to_insert = array();
foreach ($subjectArray as $p){
if(!empty($p)) $all_subjects_to_insert = $p;
}
$all_subjects_to_insert = array_unique($all_subjects_to_insert);
$inserted_values = implode("'),('",$all_subjects_to_insert);
print_r($finalvalues);
$query = "INSERT INTO #__sch_subject (subject) VALUES ('$inserted_values')";
With the separator the insert command looks like this
$query ="INSERT INTO #__sch_subject (Subject) VALUES ('a'),('b'),('c')";
Thanks for the help. I really appreciate.