我在p标签中使用contenteditable属性。代码是

<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>


和jquery发出请求
文档).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
       });
   });
});


然后在收到请求后用php更新我的mysql数据库

    <?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
));
?>


但是我认为请求没有正确提出,因为数据库没有更新..请告诉我如何检查请求是否已...或者我在编写代码时出错了?

最佳答案

您的mysqli_query函数格式错误。您必须转出括号才能放入变量。

你需要做类似的事情

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


否则您最终将查询

编辑:
对于更合适的示例,该行

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


应该

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


您会注意到主要的区别,特别是两者之间的着色,这表明在第一个区别中,变量$ field_name,$ table2和$ user_id都被解释为查询的一部分。您不需要变量的名称,而需要变量的值,因此需要将字符串连接在一起。

这只是您针对多个查询所做的多个类似修复之一。编辑器在每个要标记的内容中将要用作变量的内容标记为字符串的一部分,请执行相同的步骤来连接字符串。

10-07 15:19