Francesco Tonti
Francesco Tonti

Reputation: 1

store array in database Wordpress

I have this form with a select.

<form>[...]
<select name="quantity[]">
    <?php
    for ($i=1; $i <20; $i++) {
    ?>
   <option value="<?php echo $i ?>"><?php echo $i ?></option>
    <?php } ?>
</select>[...]
</form>

I'm trying to store the quantity data in the database using $wpdb.

global $wpdb;

    $table_name=$wpdb->prefix.'prenotazione_eventi';

    $wpdb_quantity = maybe_serialize( $_POST[ 'quantity'] );

    $query = "INSERT INTO $table_name (quantity) VALUES (%s)";

    $prepare_query = $wpdb->prepare( $query, $wpdb_quantity);

    $result = $wpdb->query( $prepare_query );

When I submit the form the data is saved in the database, but in this sintax a:1:{i:0;s:2:"10";}. What I want is that the data have only the number that I choose I the form, "10". How do that?

Upvotes: 0

Views: 2181

Answers (1)

Vel
Vel

Reputation: 9341

Try this code.

use json_encode instead of maybe_serialize.

global $wpdb;

$table_name=$wpdb->prefix.'prenotazione_eventi';

$wpdb_quantity = json_encode( $_POST[ 'quantity'] );

$query = "INSERT INTO $table_name (quantity) VALUES (%s)";

$prepare_query = $wpdb->prepare( $query, $wpdb_quantity);

$result = $wpdb->query( $prepare_query );

Upvotes: 1

Related Questions