简体   繁体   English

Laravel:AJAX不更新数据库表

[英]Laravel : AJAX not updating Database table

What i'm trying to achieve here is whenever the user clicks on a radio button, it changes the task's status accordingly 我要在这里实现的目标是,每当用户单击单选按钮时,它就会相应地更改任务的状态

<input type="radio" name="status"
<?php if($task->status == 'todo'){echo('checked');}?>
onchange = "change('todo')">Todo

<input type="radio" name="status"
<?php if($task->status == 'doing'){echo('checked');}?>
onchange = "change('doing')">Doing

<input type="radio" name="status"
<?php if($task->status == 'done'){echo('checked');}?>
onchange = "change('done')">Done

<script>
  function change(status){
  $.ajax({
      url: "/changeStatus.php/",
      type: "POST",
      data: { 'status': status, 'task_id': '<?php echo($task->id); ?>' },                   
  });
  }
</script>

and in my "changeStatus.php" file 并在我的“ changeStatus.php”文件中

<?php
    dd($_GET['status']);
    $con=mysqli_connect("127.0.0.1","root","","project_management");
    // Check connection
    if (mysqli_connect_errno()){
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $status = $_POST['status'];
    $task_id = $_POST['task_id'];
    dd($status,$task_id);
    $sql = "UPDATE 'tasks' SET 'status' = '$status' WHERE 'id' = 'task_id'";

    if (!mysqli_query($con,$sql))
    {
        die('Error: ' . mysqli_error($con));
    }

    mysqli_close($con);

?>

the problem is that the Database is not updated when i click on the radio buttons, and there's no error too! 问题是当我单击单选按钮时,数据库没有更新,也没有错误! any help is appreciated! 任何帮助表示赞赏!

note: the reason why "laravel" is in the tag; 注意:标签中为何有“ laravel”的原因; is because the error when i changed it to using POST method (which should be the correct method) is returning an error in my app.js file (from laravel). 是因为当我将其更改为使用POST方法(应该是正确的方法)时发生的错误在我的app.js文件(来自laravel)中返回了错误。

conclusion : Thanks to the answer by Loren bellow (apporoved), i also made some changes to the code, as follows -in my changeStatus.php i removed the dd, and var_dump function; 结论:感谢Loren bellow(已批准)的回答,我还对代码进行了一些更改,如下所示-在我的changeStatus.php中,我删除了dd和var_dump函数; as it was creating errors. 因为它会产生错误。 -changed the submission method from GET to POST -and fixed the query into: -将提交方法从GET更改为POST-并将查询固定为:

$sql = "UPDATE tasks SET status = '$status' WHERE id = '$task_id'";

It seems to me that your problem is here: 在我看来,您的问题出在这里:

$sql = "UPDATE 'tasks' SET 'status' = '$status' WHERE 'id' = 'task_id'";

should be 应该

$sql = "UPDATE 'tasks' SET 'status' = '$status' WHERE 'id' = '$task_id'";

Note: This is not the Laravel way to edit a database entry and is a very insecure way to run database queries. 注意:这不是Laravel编辑数据库条目的方法,而是运行数据库查询的非常不安全的方法。 Please, please, validate status and task_id so you don't get database injection attacks. 请验证状态和task_id,以免受到数据库注入攻击。 (It does, however, seem to be a Laravel view.) (但是,这似乎是Laravel的观点。)

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

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