hi im trying to fill a database table from the code below, but afterwards it fills the table coloumn just saying "Array" is there anyway to get the values inside this array and make them print in the table.
$query_new = "INSERT INTO red_message (message) VALUES ('$attributes')";
$result = mysql_query($query_new, $link_local);
P.S i use the print_r once it returns 1.. so print_r diont work either.. can anybody help me to get the values inside this $attributes array
Do you mean the implode()?
http://php.net/manual/en/function.implode.php
Example code from php.net:
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
echo $comma_separated; // lastname,email,phone
try to use json_encode. If your array will be of several steps it is better to initially encode rhis array and then insert into the database. You also keep structure of the array.
Example:
$farr=array('user1'=>array('name'=>$name,'phone'=>$phone),'user2'=>array('name'=>$name,'phone'=>$phone));
$sarr=json_encode($farr);
mysql_query("Insert INTO .......");
After:
$query=mysql_query("SELECT ......");
$res=mysql_fetch_assoc($query);
$finaly=json_decode($res, true);
print_r($finaly);
This simply means that $attributes is of Array type. You should make sure that $attributes is of String type (which you can achieve with implode()) before using it in mysql_query().
Related
I'm currently working on a personnal project and I would to register the data provided by the user.
The data looks like that :
$array = (1,2,3,4,5);
I would like to register this array in ONE SINGLE table column. I would like to do that (I know this request will not work):
INSERT INTO `test`(`user_data`) VALUES ($array)
What can I do in order to do that ?
Thanks
To do so
use implode
$array = (1,2,3,4,5);
$str = implode(",",$array);
INSERT INTO `test`(`user_data`) VALUES ($str);
or
use json_encode
$array = (1,2,3,4,5);
$json_str= json_encode($array);
INSERT INTO `test`(`user_data`) VALUES ($json_str);
I have an array containing the names of form input names:
$minmax = array('bed_min', 'bed_max', 'rec_min', 'rec_max', 'bath_min', 'bath_max', 'value_min', 'value_max');
The names are identical to the corresponding columns in a database. Instead of using an sql query like so:
$bed_min=$_POST['bed_min'];
$bed_max=$_POST['bed_max'];
$rec_min=$_POST['rec_min'];
$rec_max=$_POST['rec_max'];
$bath_min=$_POST['bath_min'];
$bath_max=$_POST['bath_max'];
$value_min=$_POST['value_min'];
$value_max=$_POST['value_max'];
$query = "UPDATE first_page SET bed_min='$bed_min', bed_max='$bed_max', rec_min='$rec_min', rec_max='$rec_max', bath_min='$bath_min', bath_max='$bath_max', value_min='$value_min', value_max='$value_max', WHERE email_address='$email' ";
Is there a way to automate all this into a smaller lump of code? I know the POST values should not be added to the query diectly, so maybe a loop to assign the POST values to a corresponding array of variables using something like:
foreach ($minmax as $var){
$var = $_POST[$var]
}
(nb i dont think this snippet will work but ive added it because I think with a bit of editing it might!)
After the list of variables have been assigned the POST values, do the update in the $query using two arrays, one with the list of values and one with the list of database columns. Again I dont know how this will work, so pointers would be helpful!
You don't really need the $minmax array of form input names since you can get those from the keys of $_POST.
If the values are all numbers, like they seem to be, then you could do it all in one line like this:
$query = "UPDATE first_page SET " . vsprintf(implode("='%d', ", array_keys($sanitized_POST))."='%d'", array_values($sanitized_POST))." WHERE email_address='$email'";
That's assuming you have already sanitized the items from $_POST into a new array named $sanitized_POST. I know you said in the above comment to ignore sanitization, but I thought I'd add it so you know I'm not suggesting to use the values straight from $_POST.
You could sanitize the $_POST array with something like this:
$sanitized_POST = array_map(function($item) {
return mysqli::real_escape_string($item);
}, $_POST);
Honestly though, you should try to come up with a solution that uses prepared statements.
On a side note, if you have the sanitized post array, then this one line will essentially do what Quixrick has done with variable variables in one of the other answers:
extract($sanitized_POST);
If you assume that all of the values in post have the same names (array keys) as your columns, then something like this could work for you:
$query = "UPDATE first_page SET ";
foreach ($_POST as $key => $var){
$query .= " $key='$var',";
}
$query = rtrim($query,',') . " WHERE email_address='$email' ";
You probably want to use 'Variable Variables' here. Basically, you'd use a double dollar sign $$ to create a variable with the name of the array value.
$_POST['bed_min'] = 'Rick';
$minmax = array('bed_min', 'bed_max', 'rec_min', 'rec_max', 'bath_min', 'bath_max', 'value_min', 'value_max');
foreach ($minmax as $var){
$$var = $_POST[$var];
}
print "<BR>BED MIN: ".$bed_min;
This outputs:
BED MIN: Rick
I am trying to retrieve an array from a mySQL db and print the output to the screen.
The array is encoded prior to writing to the db with the following:
$itemsarray = json_encode($items);
It is then INSERTed into the db with the following query:
mysqli_query($con, "INSERT INTO pages (email, date, items, title, url, expiry) VALUES ('$email', '$timestamp', '$itemsarray', '$title', '$new_url', '$expiry')")
When I retrieve the value for $itemsarray onto the screen, the output appears like this: ["forks","knives","spoons","plates","cups","mugs","napkins"].
I'm familiar with PHP and relatively new to actually writing any MySQL queries so it took me a while to just get it written and returned, so please be easy on me here. :)
Do I need to write a function of some sort that strips out the " " and [ ], or do I need some further arguments within my JSON function?
Per #Mark Baker 's recommendation, this is what worked:
$items_list = $row['items'];
$list = json_decode($items_list);
$comma_separated = implode(", ", $list);
echo $comma_separated;
It outputs something like this:
List Contents: forks, knives, spoons, plates, cups, mugs, napkins
Thanks Mark!
I'm passing an html array to php using jquery serializearray() function.
In php I can access the array using $_POST like
$a = $_POST['htmlarray']
The html array, however, is an array of arrays like so
htmlarray[] = [[1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,16,17,18]]
I want to format the variable $a so that I can insert all the html array values in one single insert query like
INSERT INTO table
(val1, val2, val3, val4, val5, val6)
VALUES
(1,2,3,4,5,6),
(7,8,9,10,11,12),
(13,14,15,16,17,18)
I know I have to use an implode function, can anyone show how this could be done.
I'm not quite sure what an html array is, but try the following:
$a = $_POST['htmlarray'];
// unserialize $a
// build sql query up to '...VALUES '
foreach ($a as $row) {
$sql .= '(';
$sql .= implode(',', $row);
$sql .= ')',
}
This should iterate through the arrays and append all your rows to the string. Note, however, that this code does not take care of SQL Injections at all! Not to be meant for production.
Iam trying to insert Array data (ie., checkbox values) into Mysql db (using Phpmyadmin)
when I try to store, iam getting as 'Array' for the field "forms" in db..
please someone tell me what changes I have to do for the below code, so I can store all the array values (seperated with commas in my db)
here is the code:
if(isset($_POST['forms']) && $_POST['forms']!=''){
$table = $wpdb->prefix . "eshop_orders";
$forms=$wpdb->escape($_POST['forms']);
$query1=$wpdb->query("UPDATE $table SET forms='$forms' where checkid='$checkid' limit 1");
}
Use serialize on the array before inserting the values, and unserialize when you call the values from memory.
For example:
$data = serialize($_POST['forms']);
You can change the array value to string using comma seperator and store it to db.
In Php there is an implode function to convert the array value to string using seperator.
$value = implode(",", $forms);
echo $value;
http://php.net/manual/en/function.implode.php