douxitao8170 2015-07-16 05:31
浏览 70

如何检查请求是否成功

i am using contenteditable property in p tag .. the code is

<p contenteditable="true" id="Option1_<?php echo $i ?>" style="width:98%;border:4px thin black; background-color:#D6D6D6;font-size:18px;color:black;padding:3px "><?php echo '&nbsp;'.'A.'.'&nbsp;&nbsp;&nbsp'.$question1['Option1'];?></p> 
  <p  contenteditable="true" id="Option2_<?php echo $i ?>" style="width:98%;border:4px thin black; background-color:#D6D6D6;font-size:18px;color:black;padding:3px "><?php echo '&nbsp;'.'B.'.'&nbsp;&nbsp;&nbsp'.$question1['Option2'];?></P> 

and jquery to make a request to make request document).ready(function(){

$("p[contenteditable=true]").blur(function(){
       var msg = $(".alert");
       var newvalue = $(this).text();
       var field = $(this).attr("id");
       $.post("ajax.php",field+"="+newvalue,function(d){
           var data = JSON.parse(d);
           msg.removeClass("hide");
            if(data.status == '200'){
                msg.addClass("alert-success").removeClass("alert-danger");
            }else{
                msg.addClass("alert-danger").removeClass("alert-success");
            }
           msg.text(data.response);
           setTimeout(function(){msg.addClass("hide");},3000);//It will add hide class after 3 seconds
       });
   });
});

and then php to update my mysql database on receiving the request

    <?php
$response = NULL;
$status = http_response_code(406);
if(!empty($_POST)){
session_start();
 $mock_test_name=$_SESSION['mock_test_name'];
$num_of_sections = $_SESSION['num_of_sections'];
$school_name = $_SESSION['school_name'];
$class_name = $_SESSION['class_name'];
$section_name = $_SESSION['section_name'];
$con = mysqli_connect("localhost","root","","onlinetest");
      if (!$con)
      {
      die('Could not connect: ' . mysqli_error());
      }
      $table_space = "$school_name $class_name $section_name $mock_test_name";
      $table = str_replace(" ", "_", $table_space);
      $table_space1 = "$school_name $class_name $section_name";
      $table1 = str_replace(" ", "_", $table_space1);
      $table_space2 = "$table1 $table";
      $table2 = str_replace(" ", "_", $table_space2); 
      $table2 = strtolower($table2);
    foreach($_POST as $key=>$value){
         $key = strip_tags(trim($key));
        $value = strip_tags(trim($value));
        $explode = explode("_",$key);
        $user_id = $explode[1];
        $field_name = $explode[0];
        if(isset($user_id)){
            $update = false;
            $selectData = mysqli_query($con,"SELECT " + $field_name + " FROM " + $table2 + " WHERE question_id='" + $user_id + "'"); //Selecting data from MySql
            $result = mysqli_fetch_assoc($selectData); //Fetching Data
            if($result[$field_name]!==$value){ //Checking if the Value is modified
                $update = mysqli_query($con,"UPDATE" + $table2+ "SET" + $field_name+"="+$value+ "WHERE question_id='"+$user_id+"'"); //Updating MySQL if value is Modifie
            }
            //Update the users Table
            if($update){
                $response = "User Details Updated";
                http_response_code(200); //Setting HTTP Code to 200 i.e OK
            }else{
                $response = "Not Modified";
                http_response_code(304); //Setting HTTP Code to 304 i.e Not Modified
            }
        }else{
            $response = "Not Acceptable";
        }
    }
}
echo json_encode(array(
    "status"=>$status,
    "response"=>$response
));
?>

But i think the request is not made properly as the database is not getting updated.. Please tell me how to check if a request has been made... or am i making error somewhere in writing code ??

  • 写回答

2条回答 默认 最新

  • doukou4066 2015-07-16 05:36
    关注

    Your mysqli_query function is formed incorrectly. You have to escape out of the parentheses in order to drop in variables.

    You need to do something like

    mysqli_connect($con, "SELECT " + var1 + " FROM " + var2);
    

    or you'll end up making a query for

    EDIT: For a more apt example, the line

    $selectData = mysqli_query($con,"SELECT $field_name FROM $table2 WHERE question_id='$user_id'");
    

    should be

    $selectData = mysqli_query($con,"SELECT " + $field_name + " FROM " + $table2 + " WHERE question_id='" + $user_id + "'");
    

    You'll notice the main difference, especially in coloring between the two, signifying that in the first one, the variables $field_name, $table2, and $user_id are all being interpreted as part of the query. You don't want the NAME of the variable, you want the VALUE of it instead, so you need to concatenate the strings together.

    This is just one of the multiple similar fixes you'll need to do for your multiple queries. Every place the editor is marking the thing you're trying to use as a variable as part of the string, take the same steps to concat the strings.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大