[mysql]更新数据库与 onClick (新手)

发布时间: 2017/4/9 20:10:47
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

谢谢你的帮助强大的约翰。却不能得到我的数据库更新。如果你有时间请看看我的两个文件,并告诉我 F 是什么错了我的 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>
function bye(){
    alert('bye');
}
$(document).ready(function(){
    function hire(){
        $.ajax({
            url: "update.php",
            type: "POST",
            data: {uid: 12341234}, //this sends the user-id to php as a post variable, in php it can be accessed as $_POST['uid']
            success: function(data){
                data = JSON.parse(data);
                //update some fields with the updated data
                //you can access the data like 'data["driver"]'
            }
        });
    }
});
</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" value="Hire!" onClick="hire();"</form></td>
        </tr>
          <tr>   
            <td>Chief Designer:</td>
            <td><i><?php echo "Level $chief_designer"; ?></i></td>
        </tr>
          <tr>               
            <td>Commercial Director:</td>
            <td><i><?php echo "Level $commercial_director"; ?></i></td>
        </tr>
          <tr>  
            <td>Pit Crew:</td>
            <td><i><?php echo "Level $pit_crew"; ?></i></td>
        </tr>
        <tr>  
            <td>Technical Director:</td>
            <td><i><?php echo "Level $technical_director"; ?></i></td>
        </tr>


</table>



<?php
include("footer.php");
?>

我 test1.php 文件

<?php
include("functions.php");
connect();
if(isset($_POST['uid'])){
     connect();
    mysql_query("UPDATE  `staff` SET  `driver` =  '3' WHERE `id`='".$_SESSION['uid']."'") or die(mysql_error());
$query = mysql_query("SELECT FROM `staff` WHERE `id`='".$_POST['uid']."'")or die(mysql_error());
    $results = mysql_fetch_assoc($query);
    echo json_encode($results);

}

?>

其实际工作人员的我想要更新但驱动程序也是在同一个表中

解决方法 1:

使用jQueryAjax:

$(document).ready(function(){
    $("#button").click(function(){
        $.ajax({
            url: "update.php",
            type: "POST",
            data: {uid: 12341234}, //this sends the user-id to php as a post variable, in php it can be accessed as $_POST['uid']
            success: function(data){
                data = JSON.parse(data);
                //update some fields with the updated data
                //you can access the data like 'data["driver"]'
            }
        });
    });
});

//the button
<input type="button" id="button" value="Hire!"/>

//update.php
if(isset($_POST['uid'])){
    //connect to the db etc...
    mysql_query("UPDATE  `staff` SET  `driver` =  '3' WHERE `id`='".$_POST['uid']."'") or die(mysql_error());

    //this'll send the new statistics to the jquery code
    $query = mysql_query("SELECT FROM `staff` WHERE `id`='".$_POST['uid']."'")or die(mysql_error());
    $results = mysql_fetch_assoc($query);
    echo json_encode($results);
}

与"更新某些字段"在 success 函数的 ajax 调用,我的意思是你可以更新你只是改变了在数据库中,该页面的统计数字,以便在网页上统计自动更改和页本身上,这两个数据库中

编辑
把这放在页眉中︰

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
    function bye(){
        alert('bye');
    }
    $(document).ready(function(){
        $("#button").click(function(){
            $.ajax({
                url: "test1.php",
                type: "POST",
                data: {uid: 12341234}, //this sends the user-id to php as a post variable, in php it can be accessed as $_POST['uid']
                success: function(data){
                    data = JSON.parse(data);
                    //update some fields with the updated data
                    //you can access the data like 'data["driver"]'
                }
            });
        });
    });
</script>

然后给你的按钮的 id '按钮' 像这样︰

<form><input type="button" id="button" value="Hire!"/></form>

请确保 url 参数的 ajax 请求 ( url: "test1.php", ) 指向正确的 php 页面。

我希望这能帮助 !

编辑
这是代码,如果你想要选择一个驱动程序︰

<form><input type="button" class="button" id="3" value="Hire!"/></form> //the id is different for every guy

Javascript 代码︰

$(document).ready(function(){
    $(".button").click(function(e){
        $.ajax({
            url: "test1.php",
            type: "POST",
            data: {colID: e.target.id}, //this sends the user-id to php as a post variable, in php it can be accessed as $_POST['uid']
            success: function(data){
                data = JSON.parse(data);
                //update some fields with the updated data
                //you can access the data like 'data["driver"]'
            }
        });
    });
});

正如你所看到的我改变了 #. ,我添加了参数 e 回调函数和我补充说︰ colID 到此数据对象。
这将是新的 PHP 代码︰

if(isset($_POST['colID'])){
    //connect to the db etc...

    $colID = mysql_real_escape($_POST['colID']); //for security
    $userID = mysql_real_escape($_SESSION['uid']);
    mysql_query("UPDATE  `staff` SET  `$colID` =  'newValue' WHERE `id`='$userID'") or die(mysql_error());

    //this'll send the new statistics to the jquery code
    $query = mysql_query("SELECT FROM `staff` WHERE `id`='$userID'")or die(mysql_error());
    $results = mysql_fetch_assoc($query);
    echo json_encode($results);
}

我希望这是你的意思,而:)

官方微信
官方QQ群
31647020