How to Store Multi Select Box in php? - php

am having select box with multiple options, like option1,option2,option3,option4,option5.Now i want to select more than one option.
For Example: Select option1,option5,option3.and click submit.after submitting the select box need to show the selected items.
in which format to be stored in mysql and what's the exact coding to do that?please help me to get rid of this problem...

You could serialize the data in and out.
Use name[] as your name which results in an array in PHP.
$_POST['name'];
serialize($name);
//now insert into DB
Do the opposite to retrieve:
unserialize($name);
But to be honest you are better off looking at more granular methods of storing the data, rather than trying to stuff it all into one field in a database. You could take the array and loop through it storing each choice in a separate row or field.
foreach($name as $value){
//insert $value into db
}

use implode
$var = implode ( "|", $_POST['selectname']);

Try this:
http://www.aleixcortadellas.com/main/2009/03/20/492/

Related

Compare Mysql Field Value

I have a table named 'jobs' with field 'categories'.
Now Values in my category field is like this
368,369,372,379,380,381
368,369,372
369,373,374,375,376
491,492
& i am getting values with search like this
'368','374','490'
Now how to compare myvalue to field value, I can't use "IN" in this stage.
In above case,
If i have values like "'368','374','490'" this, i want first three rows, beacause it contains atleast one value from my list.
how can i do this? Please suggest.
if you happy to use in programming end (i.e. using php) , you can pull those database data into an associative array. Then you can easily that array for your data manipulation. In php there are many inbuilt functions for arrays. you can use those.
http://de3.php.net/manual/en/language.types.array.php
http://www.php.net/manual/en/ref.array.php

How do you insert and retrieve an array value out of a MySQL database with PHP?

So I have a PHP array "$shared" with values [0, 1, 2] (I'm just using this as an example).
I want to insert and retrieve this array from my MySQL database, but its not working.
Currently I'm using this code to insert it:
mysqli_query($dbhandle, "INSERT INTO table (arraytest) VALUES ('$shared')");
And after the above code the value in 'table' in column 'arraytest' is 'Array' instead of [0, 1, 2] or however an array should look like in a MySQL database.
After inserting the array into my database, I want to retrieve it, and am currently using this code:
$id='2';
$result = mysqli_query($dbhandle, "SELECT arraytest FROM table");
while ($row = mysqli_fetch_array($result)) {
$shared=$row{'arraytest'};
if(in_array($id, $shared)){
echo 'Value is in the array. Success!';
}
}
I did some researching online, and have not found a way to do this specifically.
I want to insert a PHP array() into one cell in the table in the database,
and then I want to retrieve that array from the database and check if a certain value is in the array which I previously inserted into the database.
P.S. I am not trying to retrieve an entire column of values from the database as an array. This is what i have found on google when i search for an answer, and it is not what i want to do.
I have to question why you would want to do this, but the simplest answer is to serialize the array.
"INSERT INTO table (arraytest) VALUES ('" . serialize($shared) . "')"
Then you can use unserialize when you retrieve it from the database
You should properly parameterize your queries to prevent injection or errors.
Or use json_encode() to make a JSON string out of your array (and json_decode() to convert it back). That will give you a more platform-independent, aka future-proof representation of your data. Besides this will result in slightly less characters stored in your DB.
I met a couple of cases where data was represented with way, usually 'additional' information for records that was hard to press into the schema is it was changing record by record. It's a pragmatic solution, though not a very nice one. In particular, you'll have a hard time querying the information stored in this array without reading the entire table into memory.
You cannot insert PHP arrays into a db. Your problem is this:
$arr = new Array(1,2,3);
echo $arr; // prints "Array"
When using arrays in a string context in PHP, you just get the word "Array", not the contents of the array. You have to run a loop on the array to insert each individual value, e.g.
foreach($arr as $val) {
INSERT INTO yourtable VALUES ($val)
}
While storing an array like this isn't recommended, the best way is to use serialize and unserialize (for getting the data back from the database). However, I think you should just place each value is a separate cell using a foreach loop...
How to retrieve the array data from the database? I am inserted the array data using PHP implode function.
$val=implode(',',$val);
I am inserted into the MySQL database.

cakephp, how to save data in two tables?

i have a simple form with two input fields and i would like to save the data from one field inside one table and the other one inside the second table.
to save the data i use $this->Room->save($this->data)
any ideas how this can be done?
thanks
edit:
one is rooms the other one is roomates the common key i want to use is is id_rooms and id_roomates
For Room build an array like below. It is just an example, set it according to your actual fields.
$this->data['Room']['id_rooms'] = $this->data['Room']['id']
$this->data['Room']['abc'] = $this->data['Room']['xyz']
Then save data to room table: $this->Room->save($this->data).
Next build an array for second table, say Roomtitle, as below:
$this->data['Roomtitle']['id_rooms'] = $this->data['Room']['id']
$this->data['Roomtitle']['abc'] = $this->data['Room']['xyz']
and save it: $this->Roomtitle->save($this->data).

Populate PHP Combo Box using MySQL Database Field Enum Values

I would like to create a combo box (eventually operating it using jQuery Ajax), which is populated with the enum values from a database table field.
My goal is to later use these options to filter mysql_query results on a page.
I'm thinking the best way to do this is to echo the entire form and combo box with the enum values as the option tags values. Where I fall short is knowing how to call the enum values in the first place.
If you have a better suggestion on how to do this, please let me know!
Thanks.
I found the following code, which appears to do what I need. However, it seems to grab all the enum arrays from my table, and convert each array into a combo box. This isn't a problem as I was hoping to create combo boxes for each of the separate arrays anyway. However, I wish to change the order of it. Is there any way I can edit this code to control each set? Or even have it specify which field I would like to echo, and then repeat as many times as I need? I know that doing that would mean more code, and would not be as efficient.
The PHP:
$table="images";
$describe=mysql_query("describe $table");
while ($ligne=mysql_fetch_array($describe)){
extract($ligne);
if (substr($Type,0,4)=='enum'){
$liste=substr($Type,5,strlen($Type));
$liste=substr($liste,0,(strlen($liste)-2));
$enums=explode(',',$liste);
if (sizeof($enums)>0){
echo "<select name='enum'>\n";
for ($i=0; $i<sizeof($enums);$i++){
$elem=strtr($enums[$i],"'"," ");
echo "<option value='".$elem."'>".$elem."</option>\n";
}
echo "</select>";
}
}
}
Thought I should mention, the three fields that contain enums are imgFamily, imgClass, and imgGender. I would like the combo boxes in that order preferably.
You can use:
SELECT column_type FROM information_schema.columns WHERE table_name = :myTable AND column_name = :myColumn
This will return something like this:
enum('one','two','three')
Which you could parse in php. However, I think it would perhaps be better to maintain the list in code or another table than as enum values.

PHP - Pulling single value from multidimensional array

this is probably quite simple but I could do with some help.
I am trying to create a small PHP function that will display a single value form a multidimensional array when the user used two dropdown boxes to select the row and column of the array.
So, the user will make a selection from the first dropdown box, which will contain the titles of the rows, and then make a selection from a second dropdown box, which will contain the titles of the columns. Once the selections have been made, the function then needs to output the value for the specific row and column selected.
I thought I had created an array that would work but, sadly no. I have 6 rows and 6 columns in my data table.
Also, is there a JQuery or Javascript alternative?
Just looking for a few pointers to get me on my way.
Thanks in advance,
Micanio
You could either do this on the server-side or through JS.
JS: Have the script update a hidden form field with the value using the onChange() event of the drop downs. Then simply grab that hidden field when the form is posted back to the server (of source always checking for valid data).
PHP: The form will provide the two values $_POST['field1'] and $_POST['field2'] (which of course you will sanitize before using). The script could define a multidimensional array that you could feed those two values into:
$finalValue = $mdArray[$SanitizedField1][$SanitizedField2];
From there just store the $finalValue however you'd like.
$data = array();
for ($i=0;$i<6;$i++) {
$data[$i] = array();
for ($j=0;$j<6;$j++)
$data[$i][$j] = rand(0,100);
}
This should create an array similar to what you have described.
You can then access it like so...
echo $data[0][3];
Your form would need two select fields, both will values 0-5 then you can take the both of them and use them to access a value in your array.
If I understand your question correctly, you need something like a user selects a drop down list. Once selected, the option populates a second list. A real world example would be a user selects on Country to fill in a form and it will populates the States drop down list.
This kind of functionality is usually done with javascript not server side php.
http://javascript.internet.com/forms/country-state-drop-down.html

Categories