最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

database - $wpdb->insert not working for last select option

programmeradmin6浏览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 !!!");
   }

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 !!!");
   }
Share Improve this question edited Feb 17, 2020 at 0:47 Jacob Peattie 44.2k10 gold badges50 silver badges64 bronze badges asked Feb 16, 2020 at 11:47 Ernesto AlonsoErnesto Alonso 12 bronze badges 4
  • Why do you have hidden on the last option? Does it work if you remove that? – Jacob Peattie Commented Feb 17, 2020 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 Commented Feb 17, 2020 at 9:07
  • What is the structure of your table? What type of column is tipo? – Jacob Peattie Commented Feb 17, 2020 at 10:41
  • The column tipo is varchar that's why I specify in the format argument %s – Ernesto Alonso Commented Feb 18, 2020 at 21:57
Add a comment  | 

1 Answer 1

Reset to default -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"));
发布评论

评论列表(0)

  1. 暂无评论