javascript - 通过按钮使用 javascript AJAX 功能更新数据库

标签 javascript php jquery mysql ajax

我希望在单击“雇用”按钮时更新我的​​数据库表,但我找不到问题。我没有收到任何错误,但我的表格不会更新我希望有人可以检查我下面的脚本并能够指出我的错误。

这是我的 Hire_staff.php 文件

<?php
session_start();
include("header.php");
?>
<?php
$chief_aerodynamicist = $staff['chief_aerodynamicist'];
$chief_designer = $staff['chief_designer'];
$commercial_director = $staff['commercial_director'];
$pit_crew = $staff['pit_crew'];
$technical_director = $staff['technical_director'];




?>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">


    $(document).ready(function(){
        $(".button").click(function(e){
            $.ajax({
                url: "hire.fire.php",
                type: "POST",
                data: {colID: e.target.id},
                success: function(data){
                    data = JSON.parse(data);

                }
            });
        });
    });

</script>
</head>

<center><h2>You can hire new staff here</h2></center>

<table cellpadding="3" cellspacing="5">
        <tr>
            <td>Chief Aerodynamicist:</td>
            <td><i><?php echo "Level $chief_aerodynamicist"; ?></i></td>
            <td><form><input type="button" class="button" id="chief_aerodynamicist" value="Hire!"/></form></td>
        </tr>
          <tr>   
            <td>Chief Designer:</td>
            <td><i><?php echo "Level $chief_designer"; ?></i></td>
            <td><form><input type="button" class="button" id="chief_designer" value="Hire!"/></form></td>
        </tr>
          <tr>               
            <td>Commercial Director:</td>
            <td><i><?php echo "Level $commercial_director"; ?></i></td>
            <td><form><input type="button" class="button" id="commercial_director" value="Hire!"/></form></td>
        </tr>
          <tr>  
            <td>Pit Crew:</td>
            <td><i><?php echo "Level $pit_crew"; ?></i></td>
            <td><form><input type="button" class="button" id="pit_crew" value="Hire!"/></form></td>
        </tr>
        <tr>  
            <td>Technical Director:</td>
            <td><i><?php echo "Level $technical_director"; ?></i></td>
            <td><form><input type="button" class="button" id="technical_director" value="Hire!"/></form></td>
        </tr>


</table>



<?php
echo "$colID";
include("footer.php");
?>

和我的hire.fire.php

<?php
include("functions.php");
connect();
if(isset($_POST['colID'])){
    $colID = mysql_real_escape($_POST['colID']); 
    $userID = mysql_real_escape($_SESSION['uid']);
    mysql_query("UPDATE  `staff` SET  `$colID` =  '6' WHERE `id`='$userID'") or die(mysql_error());


   $query = mysql_query("SELECT FROM `staff` WHERE `id`='$userID'")or die(mysql_error());
    $results = mysql_fetch_assoc($query);
    echo json_encode($results);
}
?>

最佳答案

我不知道这是否有帮助,但是一旦我遇到了同样问题的类似查询。我只是去掉了反引号(``),它就起作用了。我会尝试使用:

UPDATE staff SET $colID = '6' WHERE id = '$userID'

而不是

UPDATE `staff` SET `$colID` = '6' WHERE `id` = '$userID'

关于javascript - 通过按钮使用 javascript AJAX 功能更新数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24762264/

相关文章:

javascript - 删除点击时的值

php - Laravel 5.2 使用 AJAX 更新表并添加新记录,无需重新加载页面

jquery - document.getelementbyid 无法获取文本内容

javascript - 类型错误 : Cannot read property 'map' of null when binding tags json data which might be null

javascript - 表单验证 - 单选循环

javascript - 函数显示和隐藏表单元素

php - 为什么这个 PHP 代码不起作用?

echo 'html code' 中的 php 变量

jquery - css 显示属性干扰我的 jquery 切换

javascript - 如何让onclick();收听多个按钮