Im trying to insert the following array into my table to no avail. I tried several examples but none worked.
Each POST array contains two strings from an input form (each string from an input row) and the $data keys are of the same name of the columns in the table.
How do I insert this data into two rows with a single query?
$data = array(
'user_id' => $_POST['user_id'],
'order' => $_POST['order'],
'type' => $_POST['type'],
'series' => $_POST['series'],
'repetition' => $_POST['repetition'],
'load' => $_POST['load'],
'pause' => $_POST['pause']
);
EDIT.: Could swear I had copied the query.
$columns = implode(',',array_keys($data));
$values = implode(',',array_values($data));
$query = " INSERT INTO userdata ($columns) VALUES ($values)";
You can insert this type of array like this
$fields=" ".implode(",",array_keys($data))." ";
$data="'".implode ("','",$data)."'";
$query ="INSERT INTO table_name ($fields) VALUES ($data)";
if($query){
mysql_query($query);
}
Related
I'm using LIKE query but when specialist_id's order change it say no records found,
if specialist_id array have [1,2,3] in it then all the users with three specialties should be in results regardless of sequence, sequence array may have [3,2,1] or [2,1,3] but the users with these three specialties should be in results, here is my code with LIKE query:
$field = $this->User->find(
'all', array(
'conditions'=>array(
'User.specialist_id LIKE' =>'%'.$value.'%',
'User.role'=>'careproviderRole',
'User.gender'=>$this->request->data['gender'])
)
);
foreach ($field as $key => $value)
{
$field[$key]['Specialist'] = $user;
}
I have also tried with FIND_IN_SET:
$field = $this->User->find(
'all', array(
'conditions' => array(
'User.role' => 'careproviderRole',
'FIND_IN_SET(\''.$this->request->data['specialist_id'].'\', User.specialist_id)')
)
);
When you use LIKE with mask '%some%', it find all results with substring 'some'.
If you need to get some specific set of ids from table (1, 2 or 3), then use sql operator IN.
Example:
SELECT `specialist_id` FROM `User` WHERE `specialist_id` IN (1,2,3);
In your code it will be:
$field = $this->User->find(
'all', array(
'conditions'=>array(
'User.specialist_id' => $arrayOfSpecialistIds,
'User.role'=>'careproviderRole',
'User.gender'=>$this->request->data['gender'])
)
Using the below, I echo a JSON array of the results. But this requires that I identify the column names which I'd like to return from the SQL query:
$new_sql = "SELECT TOP 200 * FROM STracker ORDER BY [ID] DESC";
$check_statement = sqlsrv_query($conn, $new_sql);
$data = array();
while($row = sqlsrv_fetch_array($check_statement, SQLSRV_FETCH_ASSOC)) {
$data['data'][] = array(
'id' => $row['ID'],
's_reference' => $row['s_reference'],
'reference' => $row['reference'],
'customer_name' => $row['customer_name']
);
}
Is there any way to create that array information, but return all of the columns returned by the query dynamically? So by using SELECT * FROM, all of the column data is returned in the array but without me needing to write out all of these individually? (the below)
'id' => $row['ID'],
's_reference' => $row['s_reference'],
'reference' => $row['reference'],
'customer_name' => $row['customer_name']
Ok I forgot to add that I'd tried this:
$data['data'][] = array($row);
Which is clearly wrong, and after using the following, it works perfectly!
$data['data'][] = $row;
Can't figure out why this code isn't working:
$update_SQL = $db->prepare($SQL_update);
$update_SQL->execute([$SQL_values]);
And these are dumps of the two strings being inserted into those statements:
$SQL_update = UPDATE laptops SET asset_tag = :asset_tag WHERE id = :id
$SQL_values = 'asset_tag' => 5544, 'id' => 23
You missed : in your code:-
$update_SQL = $db->prepare($SQL_update);
$update_SQL->execute([':asset_tag' => 5544, ':id' => 23]);
So actually what you have to do is:-
$SQL_values =[':asset_tag' => 5544, ':id' => 23]; // create array like this
$update_SQL = $db->prepare($SQL_update);
$update_SQL->execute($SQL_values); // pass that array
Or
$SQL_values =['asset_tag' => 5544, 'id' => 23]; // create array like this
$update_SQL = $db->prepare($SQL_update);
$update_SQL->execute($SQL_values); // pass that array
Note:- execute won't accept a string, it must be an array.
i want to update a single column (tuple) within attribute in table. i have the array shown below in controller.
$promotion_info = array(
'category' => $this->input->post('category'),
'rank' => $this->input->post('rank'),
'date_of_start' => $date_of_promotion
);
i passed it to model as shown below:-
$this->insert_model->form_promotion($fno, $promotion_info);
on model i want to update column with date_of_promotion. Then inserting a new row (attribute) with the value contained in array.
how can i update that column with the single value in array
Following docs:
$this->db->set('field', 'field+1', FALSE);
$this->db->where('id', 2);
$this->db->update('mytable'); // gives UPDATE mytable SET field = field+1 WHERE id = 2
$this->db->set('field', 'field+1');
$this->db->where('id', 2);
$this->db->update('mytable'); // gives UPDATE `mytable` SET `field` = 'field+1' WHERE `id` = 2
In your case it could be something like:
$array = array(
'category' => $name,
'rank' => $title,
'date_of_start' => $status
);
$this->insert_model->form_promotion($fno, $data);
In model:
$this->db->set($data);// this is case if you need to update all 3 values
//if you need to update only one value from the array than
//$this->db->set('date_of_start', $data['date_of_start']);
$this->db->where('date_of_start', "SOME_EXISTING_DATE_FROM_TABLE");
$this->db->insert('mytable');//
So, I want to have table with users name, grades and subjects. Table will display only his grades. So I'm generating subject in foreach loop and reading grades depending on his id.
For subject I want to have an array which will contain infos about subject (teacher, classroom, etc.)
For now I have this array:
$subjects = array();
$getSubjects = mysqli_query($con, "SELECT * FROM predmeti");
while ($subject = mysqli_fetch_array($getSubjects)) {
$subjects[]= array(
$subject['subject_name'] => array(
'id' => $subject['id'],
'name' => $subject['name'],
'teacher' => $subject['teacher'],
'short_name' => $subject['short_name'],
'classroom' => $subject['classroom']
)
);
I know this isn't right. I can't get data for each subject.
Could you please help me?
You're accessing the columns by name while mysqli_fetch_array() returns only an integer indexed array. Have you tried mysqli_fetch_assoc()?