简体   繁体   English

我想从 ajax 中的 sql 数据中获取数据但不起作用

[英]i want to get data from sql data in ajax but not working

i am working withphp ajax but it not responding, but once i remove a row it works help.我正在使用php ajax,但它没有响应,但是一旦我删除了一行,它就会起作用。

this is my code search_query.php这是我的代码 search_query.php

if (isset($_POST['user_post'])) {
   $post_user = array();
   $user_post = $con->prepare("SELECT * from post where poster_id= ?");
   $user_post->execute([$id]);
   while($row = $user_post->fetch(PDO::FETCH_ASSOC)) {
       $post_user[] = '<div id="thread" class="card promoting-card thread mt-2">
       <div class="card-body d-flex flex-row">
           <img src="http://localhost/lasu/img/user-profile.png" class="rounded-circle mr-3" height="50px" width="50px" alt="avatar">
           <div>
               <h4 class="card-title font-weight-bold mb-2" style="text-align:left;"> '.strtoupper(user_detail($row['poster_id'], "username")).'</h4>
               <p class="card-text"><i class="far fa-clock pr-2"></i>'. time_elapsed_string($row['reg_date'], $full = false).' </p>
                <p>'.$row['post_content'].'</p>
          </div>
      </div>
   </div>';
    }
   echo json_encode($post_user);
   }

and this is javascript这是 javascript

 $(function(){
    var search_data = new FormData();
    search_data.append("user_post", 'post');
    search_data.append("user_post_id", <?php echo get_users_page_id(); ?>);
    $.ajax({  
              url:"search_query.php",  
              type: 'POST', 
              data:search_data,  
              contentType: false,
              processData: false,
              dataType: 'json',  
              success:function(data)
              {
                if (data.length < 1) {
                    $('#results').append('<font>No Post</font><br>');
                }else{
                    for (var i = 0; i < data.length; i++) {
                        $('#results').append(data[i]+'<br>');
                    }
                }
              } 
          })
        })

It works if i remove $row['post_content'] in the php code and i tried it in a plain page it worked pls help如果我在 php 代码中删除$row['post_content']并且我在普通页面中尝试它,它会起作用,请帮助

Maybe you forgot to get the id?也许您忘记获取ID?

if (isset($_POST['user_post'])) {
   $post_user = array();
   $user_post = $con->prepare("SELECT * from post where poster_id= ?");
   // here
   $id = $_POST['user_post_id'];
   $user_post->execute([$id]);
   while($row = $user_post->fetch(PDO::FETCH_ASSOC)) {
       $contents = highlight_contents($row['post_content']);
       $post_user[] = '<div id="thread" class="card promoting-card thread mt-2">
       <div class="card-body d-flex flex-row">
           <img src="http://localhost/lasu/img/user-profile.png" class="rounded-circle mr-3" height="50px" width="50px" alt="avatar">
           <div>
               <h4 class="card-title font-weight-bold mb-2" style="text-align:left;"> '.strtoupper(user_detail($row['poster_id'], "username")).'</h4>
               <p class="card-text"><i class="far fa-clock pr-2"></i>'. time_elapsed_string($row['reg_date'], $full = false).' </p>
          </div>
      </div>
   </div>';
    }
   echo json_encode($post_user);
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM