使用javascript AJAX函数通过按钮更新数据库

时间:2014-07-15 15:30:03

标签: 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);
}
?>

1 个答案:

答案 0 :(得分:0)

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

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

而不是

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