i use mongodb in php and have a problem to insert $subitems array to mongodb collection.
php code:
if($_SERVER["REQUEST_METHOD"] == "POST"){
$errors = array();
$alarm = array();
$item_name = data::test_input($_POST["item_name"]);
$folder_name = data::test_input($_POST["folder_name"]);
$subitem_num = data::test_input($_POST["subitem_num"]);
for($i=1;$i<=$subitem_num;$i++){
${"subitem_name$i"} = data::test_input($_POST["subitem_name".$i]);
${"subitem_file$i"} = data::test_input($_POST["subitem_file".$i]);
if(count($errors)==0){
$subitems = array(${"subitem_name$i"}=>${"subitem_file$i"});
}
}
if(empty($item_name)){
$errors['item_name']= "insert item";
}
if(empty($folder_name)){
$errors['folder_name']= "insert folder";
}
if(count($errors)==0){
$query = array(
"item_name" => $item_name,
"status" => 0,
"folder_name" => $folder_name,
"subitem" => $subitems
);
$result = items::insert($query);
if($result) $alarm['success_additem'] = "submit done";
}
}
i want record values to mongodb collection like this:
{ "_id" : ObjectId("542e71b333e916542a00002e"), "item_name" : "users management", "status" :0, "folder_name" : "users", "subitem" : { "a" : "a.php","b" : "b.php" },"c" : "c.php" }
how to write php code for insert to mongodb collection?
I assume your problem is that the subitem field in your document never contains more than a single key/value pair when inserted into the database.
for($i=1;$i<=$subitem_num;$i++){
${"subitem_name$i"} = data::test_input($_POST["subitem_name".$i]);
${"subitem_file$i"} = data::test_input($_POST["subitem_file".$i]);
if(count($errors)==0){
$subitems = array(${"subitem_name$i"}=>${"subitem_file$i"});
}
}
Based on that for loop, you're over-writing $subitems in each iteration. I assume you mean to assign a key within it, in which case you'd be better served with the following:
$subitems = array();
for($i = 1; $i <= $subitem_num; $i++) {
$key = data::test_input($_POST["subitem_name".$i]);
$value = data::test_input($_POST["subitem_file".$i]);
if (count($errors) == 0) {
$subitems[$key] = $value;
}
}
For the record, I have no idea why you're checking $errors here, as it's assigned once at the top of this function and doesn't appear to be modified within the for loop; however, I left it in place so it lines up with the original example you've provided.
Also, there is really no reason to use dynamically-named variables here. Fixed terms such as $key and $value make the code much more readable.
Related
I'm getting null values after I run the DBEscape($data) function that is for SQL injection protection. Can someone help?
My inputs are all multiple arrays, ex: name="quote[][dt_flight]", name="quote[][acft]", etc.
Method is POST.
function DBEscape($data){
$link = DBConect();
if(!is_array($data)){
$data = mysqli_real_escape_string($link,$data);
}
else {
$arr = $data;
foreach ($arr as $key => $value){
$key = mysqli_real_escape_string($link, $key);
$value = mysqli_real_escape_string($link, $value);
$data[$key] = $value;
}
}
DBClose($link);
return $data;
}
function DBCreate($table, array $data, $insertId = false){
$table = DB_PREFIX.'_'.$table;
$data = DBEscape($data);
var_dump($data);
$fields = implode(", ", array_keys($data));
$values = "'".implode("', '", $data)."'";
$query = "INSERT INTO {$table} ({$fields}) VALUES ({$values});";
var_dump($query);
return DBExecute($query, $insertId);
}
if(isset($_POST["quote"]) && is_array($_POST["quote"])){
foreach($_POST["quote"]["dt_flight"] as $key => $text_field){
$last_id = DBCreate('quote',$_POST['quote'],true);
$i++;
}
}
The connection works since it is inserting the rows into the tables. I used vardump before and after the DBEscape to figure out that it is deleting the values, the keys are fine.
PS: The proposed answer is for a single variable not an array.
As you can see in your var_dump-result, the data you sent to DBCreate and thus to DBEscape looks like
array(
'dt_flight' => array(0 => '2018-06-13'),
'acft' => array(0 => 'VQ-BFD',
// and so on
)
Therfore the data you sent to
// $value = array(0 => '2018-06-13') here
$value = mysqli_real_escape_string($link, $value);
And well, mysqli_real_escape_string doesn't like arrays very much, thus will return NULL and thus inserting empty data in your table.
You most likely want to resolve this error within your foreach($_POST["quote"]["dt_flight"]) loop, since I suppose you sent multiple flight-data:
foreach($_POST["quote"]["dt_flight"] as $key => $text_field) {
// $key would be 0, for $_POST["quote"]["dt_flight"][0] = '2018-06-13'
$keyData = [];
foreach($_POST["quote"] as $field => $allFieldValues) {
// Walk over every field, and add the value for the same $key
if (is_array($data) && isset($allFieldValues[$key])) {
// Would add for example $keyData['acft'] = $_POST['quote']['acft'][0] = 'VQ-BFD';
$keyData[$field] = $allFieldValues[$key];
}
}
var_dump($keyData);
// Would look like array(
// 'dt-flight' => '2018-06-13',
// 'acft' => 'VQ-BFD',
// and so on
// )
$last_id = DBCreate('quote',$keyData,true);
$i++;
}
Although this is not part of your question, I really suggest you also take care of my comment on your question about mysqli_real_escape_string not being a safe way to escape column-names (or table-names and so on). For example with following solution:
function DBCreate($table, array $data, $insertId = false) {
// For each table the known columns
$columns = array( 'quote' => array('dt_flight', 'acft', '...') );
// Verify valid table given
if (!isset($columns[$table])) {
throw new InvalidArgumentException('No such table: ' . $table);
}
// Remove everything from data where the key is not in $columns[$table]
// = Remove everything where the column-name is non-existing or even an attempt to hack your system
$data = array_intersect_key($data, array_fill_keys($columns[$table], null));
if (!count($data)) {
throw new InvalidArgumentException('No (valid) data given at all');
}
// Next, continue with your implementation
}
This is how my json looks like ["Chicken",{"quantity":"1"},"Froggies",{"quantity":"2"},"Fryies",{"quantity":"3"}].
Is there a way that i can get the data out the results like
Chicken : 1, Froggies:2, Fryies:3
I tried to use implode to get this done but i get an error saying array to string conversion,
Below is my code
foreach($request->get('item_id') as $key => $id)
{
$selected_item = Item::all()->where('id',$id);
foreach($selected_food as $select)
{
$food_selected[]= $select->name ;
$food_selected[] = ['quantity' => $request->get('quantity')[$key]];
}
}
$query ="Your items are ".implode(',',$food_selected)."";
Maybe array of objects would be more useful in that situation, which you could get this way:
$arr = [];
foreach ( $request->get('item_id') as $key => $id ) {
$selected_item = Item::all()->where('id', $id);
foreach ( $selected_item as $select ) {// $selected_item or $selected_food here
/*
$obj = new stdClass;
$obj->{$select->name} = $request->get('quantity')[$key];
$arr[] = $obj;*/
$arr[$select->name] = (int) $request->get('quantity')[$key];
}
}
$query = '';
foreach ( $arr as $k => $v ) {
$query .= ' '.$k.': '.$v.',';
}
$query = rtrim($query, ',');
$query = ltrim($query);
$query = "Your items are ".$query;
I assume that the ID is unique key for an Item and your Item::all()->where('id',$id) will return only one record. If this is true, the second loop is unnecessary.
Based on this assumption, I come to this code:
$result = collect($request->get('item_id'))
->map(function($itemId, $itemKey) use ($request) {
$item = Item::find($itemId);
return $item->name . ' : ' . $request->get('quantity')[$itemKey];
})->implode(',');
// $result contains the string: "Chicken : 2, Fries : 1"
For explanation:
Cast the array into a collection
Use map to loop over it
Find the Item by its ID
Return the name and the quantity (this returns a collection)
Implode the collection
I have written following code in php and am using mongo db as database
$projection = array("broadcast_id" => 1,"studentList" => 1);
$query = array('broadcast_id'=> $broadcast_id);
$count = $this->collection->find($query)->count();
$cursor = $this->collection->find($query,$projection);
$result = array();
foreach($cursor as $row)
{
$idstring = trim($row["studentList"]);
$idstring = preg_replace('/\.$/', '', $idstring);
$idarray = explode('|', $idstring);
foreach($idarray as $studentId)
{
$this->StudentCollection = $this->db->studentTbl;
$StudentCursor= $this->StudentCollection->find(array("student_id" => $studentId));
if($StudentCursor->count() > 0)
{
foreach ($StudentCursor as $k => $srow) {
array_push($result, $srow);
}
}
else
{
array_push($result, array("datanotfound"=>1));
}
}
}
return json_encode($result);
After fetching "studentList" from broadcastTbl table and $idstring has "5042|5043|5044" values which are student ids of studentTbl. Now I am trying to fetch corresponding students details by splitting them one by one on the basis of "|". After that I am trying to push them in array $result.
It always displays $StudentCursor->count() as "1" and never enter in else block even if find() query fails to find record and then it displays output as [] i.e it always stay in if statement !!!
Please help me in tracking out what is wrong in the code and writting it efficiently!!!
I'm wondering if there is a quick way to do the following:
I have a PHP object with multiple attributes. For example
$person;
$person->height = 165;
$person->name = 'john';
I would like to dump this into an SQLite MySQL DB while creating each column automatically from the attribute names of the PHP object. So for example, if I were to dump the above object into mySQL, it would look something like this:
Table : people
Name(Column, VARCHAR) - "John"
Height(Column, INT) - 165
The reason I am asking this is because the number of attributes is growing constantly, and having to create and manage the table columns manually is a lot of work. I am wondering if there is an automated way of doing this.
First you need to convert object into array and then you can iterate through it and can create table and insert values in it.
Something like below:
Step 1: Convert object to array
Step 2: Get keys(fields) and values out of array
Step 3: Generate sql queries
<?php
//Step 1: convert object to array
//$persion = (array) $yourObject;
//Step 2: get keys(fields) and values out of array
$person = array(
"height" => "165",
"name" => "john",
"age" => "23"
);
function data_type($val) {
if(is_numeric($val)) {
return "int";
}
else {
return "varchar(15)";
}
}
//Step 3: sql query, only including sql query
function create_table($person) {
$create = "CREATE TABLE IF NOT EXISTS people";
$ctr = 0;
foreach($person as $key => $value) {
if($ctr == 0) {
$field_query = $key." ".data_type($value);
} else {
$field_query .= ", ".$key." ".data_type($value);
}
$ctr++;
}
echo $create .= " (".$field_query.")";
echo "<br/>";
}
create_table($person);
function insert_table($person) {
$ctr = 0;
foreach($person as $key => $value) {
if($ctr == 0) {
$field_query = $key;
$value_query = $value;
} else {
$field_query .= ", ".$key;
$value_query .= ", ".$value;
}
$ctr++;
}
echo $insert = "INSERT INTO people"." (".$field_query.") VALUES (".$value_query.")";
}
insert_table($person);
?>
Hope this will help you in some way(y).
I'm trying to create a single array of status id's and their names pulled from the database. My array needs to come out looking like this
$options = array("1" => "Active", "2" => "Inactive");
I've been trying to use array_push and array merge but it keeps going wrong. Here is what I have so far...
$allStatus = implode(',', array('1', '2'));
$statuses = $clCont->getAllRecords('*', '`status` WHERE idstatus IN ('.$allStatus.')');
$status = mysqli_fetch_assoc($statuses);
$statusOpt = array();
do
{
array_push($statusOpt[$status['idstatus']],$status['statusName']);
}
while ($status = mysqli_fetch_assoc($statuses));
$rows = mysqli_num_rows($statuses);
if($rows > 0)
{
mysqli_data_seek($statuses, 0);
$status = mysqli_fetch_assoc($statuses);
}
print_r($statusOpt);
Ok, let's try to modify your code:
$allStatus = implode(',', array('1', '2'));
$statuses = $clCont->getAllRecords('*', '`status` WHERE idstatus IN ('.$allStatus.')');
// you don't need to fetch first item.
// $status = mysqli_fetch_assoc($statuses);
// and use a do-while loop
// use just while:
$statusOpt = array();
while ($status = mysqli_fetch_assoc($statuses))
{
$id = $status['idstatus'];
// The first time you try to put a value in a
// $statusOpt[$status['idstatus']] it's not exists,
// so you should define it explicitly:
if (!isset($statusOpt[$id])) {
$statusOpt[$id] = array();
}
// after that it should be okay:
array_push($statusOpt[$id],$status['statusName']);
}
print_r($statusOpt);