dongzhiyong8577 2018-10-06 07:30
浏览 77
已采纳

使用以搜索表分隔的Ajax数据逗号构建SQL查询

I'm trying to pass a string of data from a field which is separated by commas via ajax to a php file which does a simple search.

The data I am passing for example is: var dataString = '1,2,3,4' as post. I'm now trying to get the value in my PHP file and make a query to my database but the dataString post value is empty once it gets into the PHP file. Can anyone explain where I'm wrong here?

Also, is there a better way to generate my SQL query?

Code:

var dataString = "1,2,3,4";

      $.ajax({ /* THEN THE AJAX CALL */
        type: "POST", /* TYPE OF METHOD TO USE TO PASS THE DATA */
        url: "includes/search.php", /* PAGE WHERE WE WILL PASS THE DATA */
        data: dataString, /* THE DATA WE WILL BE PASSING */
        success: function(result){ /* GET THE TO BE RETURNED DATA */
         alert(result);
        }
      });

**PHP **

if(isset($_POST['dat'])){
    $dat = $_POST['img'];
    $types = explode(",", $dat);
    $size = sizeof($types);

    $loc = '30';

    $ini = $types[0];

    $query = "SELECT `location_images`.`image_link` FROM `location_images` INNER JOIN `image_type` ON `location_images`.`id` = `image_type`.`image_id` WHERE `location_images`.`location_id` = :loc AND (`image_type`.`dt_id` = '".$ini ."'";

    for($i=1; $i<$size; $i++){
        $query .= " OR `image_type`.`dt_id` = '".$types[$i]."'";
    }

    $query .= ")";
    echo $query; die;
}
  • 写回答

1条回答 默认 最新

  • douzhong6480 2018-10-06 07:37
    关注

    You need to specify an object to $.ajax for the data parameter, if you specify a string (as you have done) it is assumed to be a query string. So change your AJAX call to

      $.ajax({ /* THEN THE AJAX CALL */
        type: "POST", /* TYPE OF METHOD TO USE TO PASS THE DATA */
        url: "includes/search.php", /* PAGE WHERE WE WILL PASS THE DATA */
        data: { dat: dataString }, /* THE DATA WE WILL BE PASSING */
        success: function(result){ /* GET THE TO BE RETURNED DATA */
         alert(result);
        }
      });
    

    Also, in your PHP, immediately after the if(isset($_POST['dat'])){ you need to change

    $dat = $_POST['img'];
    

    to

    $dat = $_POST['dat'];
    

    In terms of generating your SQL query, your current method leaves you open to SQL injection. I would do something like this:

    $query = "SELECT `location_images`.`image_link` 
              FROM `location_images` 
              INNER JOIN `image_type` ON `location_images`.`id` = `image_type`.`image_id` 
              WHERE `location_images`.`location_id` = :loc AND
                   (`image_type`.`dt_id` = :type0";
    for ($i = 1; $i < $size; $i++) {
        $query .= " OR image_type`.`dt_id` = :type$i";
    }
    $query .= ")";
    

    Then you can bind the type values at the same time as you bind :loc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳