0

After reading the posts with similar problems with $wpdb->insert I can't find the proper solution for my problem.

I have a template page where I send (via POST) the data to another template page to add rows to a custom table in wordpress database.

One of the values comes from a select, which one is received and processed unless it is not the last option of the select. So weird, I’ve enabled the wpdb->show_Errors() and when the last option is selected it shows:

WordPress database error: [] SHOW FULL COLUMNS FROM `pagos`.

This only happens when I choose the last option of my select, I made it work adding an extra option in the last place and hiding it but is not a proper solution. Here is my code, and thanks beforehand.

PS: As you can see I echo some variables to see what's going on and the results are the ones I wrote before. THX.

Page 1 code:

<div id="divPago" class="content-container">
<form id ="formAgregarPago" method="POST" action= "<?php echo $agregarPagoURL; ?>" >
    <input id="pagoNombre" name="pagoNombre" type="text" placeholder="Nombre">
    <select id="pagoTipo" name="pagoTipo">
      <option value = "Pago Evento">Pago Eventos</option>
      <option value  = "Pago Anual">Pago Anual</option>
      <option value  = "Pago Regular" hidden >Pago Regular</option>
    </select>   
    <input id="pagoMonto" name= "pagoMonto" type="text" placeholder="Monto">
    <button id="botonAgregarPago">Agregar</button>
    <button id="botonCancelarPago">Cancelar</button>    

</form>

Page 2 code:

if( isset($_POST['pagoNombre'] )  && isset($_POST['pagoTipo'])  && isset($_POST['pagoMonto']) ){  

    global $wpdb;

    $nombre =$_POST['pagoNombre'];
    $tipo = $_POST['pagoTipo'];
    $monto = $_POST['pagoMonto'];

    $wpdb->show_errors();

    $result = $wpdb->insert('pagos', array(
        "id"=>"",
        "nombre" => $nombre, 
        "tipo" => $tipo, 
        "monto" => $monto), 
    array("%d","%s", "%s", "%d"));

    echo "<br>Error:".$wpdb->print_error();
    echo "<br>Result:".$result;
    echo "<br>Last Error:".$wpdb->last_error;

    echo "<br>Var_DUMP:".var_dump($result);
    echo "</div>";
   }
   else{
     echo ("No llegan los datos !!!");
   }
4
  • Why do you have hidden on the last option? Does it work if you remove that? – Jacob Peattie Feb 17 '20 at 0:48
  • I hide that to make it work, the problem is with the last option. The ones that are not hidden can be selected and inserted to database without problem, dirty way to fix it. – Ernesto Alonso Feb 17 '20 at 9:07
  • What is the structure of your table? What type of column is tipo? – Jacob Peattie Feb 17 '20 at 10:41
  • The column tipo is varchar that's why I specify in the format argument %s – Ernesto Alonso Feb 18 '20 at 21:57
-1

The ID can't be empty. If you have created your table and set your ID as auto increment, you don't have to insert it, it will be generated automatically.

So all you have to do is the following:

$result = $wpdb->insert('pagos', array(
        "nombre" => $nombre, 
        "tipo" => $tipo, 
        "monto" => $monto), 
    array("%s", "%s", "%d"));
1
  • No luck with that, I've changed the code and the problem persists. It works with the other options in the select but the error is with the last one. Thanks for your recomendation. – Ernesto Alonso Feb 17 '20 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.