【问题标题】:I receive an error, but I do not know what it is我收到一个错误,但我不知道它是什么
【发布时间】:2026-02-12 07:05:02
【问题描述】:

我必须用ID选择的mysql服务器的元素组成一个单行表,该表运行完美(显示在html文件中)。
我通过ajax函数在javascript文件中执行它,当我调用该函数时,我的javascript崩溃了,请帮助我

 <?php
    $conexion = mysqli_connect("localhost", "root", "", "db_dinagri");

    $salida = "";
    $query = "SELECT * FROM productos ORDER By ID";
    $cantidad = 0;

    //$Unidad = $fila['ProductU'];

    if(isset($_POST['e']) && isset($_POST['c'])){
        $q = $conexion->real_escape_string($_POST['e']);
        $query = "SELECT ID, ProductN, ProductP, ProductU, ProductS FROM productos 
                  WHERE ID LIKE '%".$q."%'";
        $cantidad = $_POST['c'];
    }

    $resultado = $conexion->query($query);
    $salida.="<table class='table table-sm' id='tabla' autofocus>
                    <thead class='thead-light'>
                        <tr>
                               <th scope='col'>Cantidad</th>
                            <th scope='col'>Unid.</th>
                            <th  scope='col'>ARTICULO</th>
                            <th  scope='col'>P. Unitario</th>
                            <th>TOTAL</th>
                        </tr>
                    </thead>
                    <tbody>";
    $fila = $resultado->fetch_assoc();
                  $salida.="<tr>
                        <td>".$cantidad."</td>
                        <td>".$fila['ProductU']."</td>
                        <td>".$fila['ProductN']."</td>
                        <td>".$fila['ProductP']."</td>
                        <td>40</td>
                     </tr>";
    //if($resultado->num_rows > 0){
      //  $i
        //for($i=1; $i<4; $i++){
        //  echo "hola";
        //}
        //while($fila = $resultado->fetch_assoc()){


        //}

        $salida.="</tbody</table>";


     echo $salida;
    $conexion->close();

    ?>

【问题讨论】:

  • 打开开发者控制台。解释“崩溃”。
  • 它只是告诉我问题是当调用 ajax 函数并且它没有显示任何 php 问题,但是 javascript 崩溃了,它消除了 ajax 函数并且它工作得很好,问题是 php ,并且控制台没有给我php的错误
  • 我没有看到任何 javascript
  • function order(e, c){ $.ajax({ url: 'order.php', type: 'POST', dataType: 'html', data: {e: e, c: c}, }) .done(function(r){ $('#order').html(r); }) .fail(function(){ console.log("error"); }) } order(1, 5);
  • javascript 崩溃是什么意思?

标签: php jquery mysql


【解决方案1】:

每当您对服务器进行 ajax 调用时,您都不会收到任何 php 错误的消息,因为它变成了内部问题,并且在您的控制台日志中它会显示内部错误。最好一步一步地做一些小的检查,比如首先检查你传递的数据是否到那里,你的查询是否正确等等。 在您的情况下,查询似乎是正确的,您的 ajax 调用是正确的,我猜从查询检查中获取可能有错误,也不需要在 php var 中使用 “.$cantidad。” 将其更改为 “$候选人” 如果错误仍然存​​在,那么尝试在while循环中使用html代码形成表格的所有结果,因为您的查询可能会获取多于一行并将ajax调用中的“dataType”更改为“text”。

【讨论】:

  • order 文件在 index.php 文件中创建表,当发生这种情况时文件 js 停止。也就是说,数据正确传递到 order.php 文件,但是当它通过返回响应时文件js(它到达index.php)并且文件js停止
  • 所以您想说您正在从 ajax 调用中获得响应,但问题出在 html 文件中?请认真阐述错误。您在问题中提出的问题意味着存在问题php 代码....你的 ajax 似乎只是正确的
  • 我认为问题可能出在 mysql 但不完全在哪里,因为我收到了答案 ajax。
  • 用 mysqli_real_escape_string($conexion, $_POST['e']); 替换你的 real_escape_string;
  • 但是js文件还是崩溃了