0

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?

2
  • ok, I found the solution by myself. For everyone in need I change maybe_serialize() with implode() Commented Apr 27, 2018 at 11:20
  • In wordpress, adding array in database will store data in serialize format of that array. Commented Apr 27, 2018 at 11:21

1 Answer 1

1

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 );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.