简体   繁体   中英

Inserting and retrieving array data into and from MySQL using PHP pdo

Please be gentle with me, I'm very new to PHP and MySQL.
I'm trying to insert and retrieve data to and from MySQL using PDO. here is the HTML form which sends the data to my PHP using POST

<FORM ACTION="index.php" METHOD=POST>

Update# : 
<INPUT TYPE=TEXT NAME="update_num" align="left" LENGTH=2 required >  
<P>

<P>
ETO     :
<INPUT TYPE=TEXT NAME="eto" LENGTH=30 >  
</P>    

<P>
CAD#    :
<INPUT TYPE=TEXT NAME="cad" LENGTH=30 >  
</P>    

<p>
CMS:
</p>
<h6>Hold Down control(CTRL) key to select multiple CMSs</h6>
<SELECT NAME = "cms[]" multiple>
<option></option>>
<option>#1</option>>
<option>#2</option>>
<option>#3</option>>
<option>#4</option>>
</SELECT>
</p>
<INPUT TYPE=SUBMIT VALUE="Submit Form" align="center">

</FORM>
</INPUT>

I made the modifications according to this post from Stackoverflow : Getting All $_POST From Multiple Select Value

My PHP code looks like this:

$sql = "INSERT INTO dutypage (update_num, eto, cad, cms) VALUES (:update_num, :eto, :cad, :cms)";
$stmt = $conn->prepare($sql);

$stmt->bindParam(':update_num', $_POST['update_num']);
$stmt->bindParam(':eto', $_POST['eto']);
$stmt->bindParam(':cad', $_POST['cad']);
$stmt->bindParam(':cms', $_POST['cms']);

$stmt->execute(); 

my data get successfully inserted but the cms column now just displays Array

MYSQL屏幕截图

I use the following code to retrieve the data

$query="SELECT cms FROM dutypage ORDER BY dateOf DESC";
$data=$conn->query($query);
$result = $data->fetchAll(PDO::FETCH_ASSOC);
var_dump($result);
foreach ($result as $output) {
    echo $output['cms'];
    echo "<br>";
}

and I only get this:

array(1) { [0]=> array(1) { ["cms"]=> string(5) "Array" } } Array

Are my selected options in the form actually being passed into the array in the MySQL?

PHP trys here to convert the $_POST["cms"] array into a string, which is basicly impossible. So PHP output the string "Array".
What I recommend you to do if you want to save arrays into dbs, JSON encode them using the json_encode function.
So the bindParam of :cms should look like this:
$stmt->bindParam(':cms', json_encode($_POST['cms']));
And to get your array back when fetching, simply use the json_decode function on the string.

Have a great day.

Since you have set the cms as multiple select form

<SELECT NAME = "cms[]" multiple>
<option></option>
<option>#1</option>
<option>#2</option>
<option>#3</option>
<option>#4</option>
</SELECT>

The value of $_POST['cms'] will be an array containing the selected values.

You can do:

1) Depending on the mysql You are running, You can setup the "cms" column to JSON type and store the value in JSON format

2) You can store the value as text and do manipulations after reading the values. (Not recommended).

$stmt->bindParam(':cms', json_encode($_POST['cms']));

3) You can create a new one to many table, that will contain update_num and cms. For every chosen value, make a new insert in that table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM