dqxboe2628 2017-04-13 20:01
浏览 61
已采纳

Jquery ajax更新mysql表

in my program there is mysql table 'company'.companyid and activation_code are some of columns in that table.i show that table values in a html table where includes comapproval.php page.that html table each row has a accept button and when i click that button company table should be updated and that row remove from html table.update code is in 'approvecompany.php' page.i used jquery ajax for this.when i cliick accept button i get only success alert.but i could not do updaet table.even sucessfull alert get only for first row if html table.i am new for ajax.help me to solve this.

comapproval.php

<table class="table table-striped table-bordered table-list">
                    <thead>

                    <tr>
                        <th>Action</th>
                        <th>ID</th>
                        <th>Registration number</th>
                        <th>Company Name</th>
                        <th>Email</th>
                    </tr>
                    </thead>
                    <tbody>

                    <?php
                    $conn = mysqli_connect("localhost", "root", "", "internship");
                    if (mysqli_connect_errno()) {
                        echo "Failed to connect to MySQL: " . mysqli_connect_error();
                    }
                    $sql = "";

                    $sql = "select * from company where activation_code=0";

                    $res = mysqli_query($conn, $sql);

                    while ($row = mysqli_fetch_assoc($res)):

                    ?>

                    <tr>
                        <td align="center"><input type="submit" class="btn btn-default" value="Accept" id="accept" name="accept"></input></td>
                        <td><?php echo $row['companyid']; ?></td>
                        <td><?php echo $row['government_reg_no']; ?></td>
                        <td><?php echo $row['company_name']; ?></td>
                        <td><?php echo $row['email']; ?></td>
                    </tr>

                    <?php
                        endwhile
                    ?>

                    </tbody>
                </table>
<script>
            $("#accept").click(function () {
                $.ajax({
                   type:"POST",
                   url:"approvecompany.php",
                    data:{comid:$('<?php $row['companyid']?>').val()},
                    success:function () {
                        alert('Successfully approved');
                        window.location.reload(true);
                    }

                });
            });

    </script>

approvecompany.php

<?php
$conn=mysqli_connect("localhost","root","","internship");

$comid=$_POST['comid'];

if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="update company set activation_code=1 where companyid=$comid";

if ($conn->query($sql) === TRUE) {

?>

<?php
}

else{
?><script>alert("Error...")</script><?php
}
?>
  • 写回答

1条回答 默认 最新

  • dongpao1083 2017-04-13 20:31
    关注

    Try Rewriting comapproval.php as below:

    <table class="table table-striped table-bordered table-list">
        <thead>
            <tr>
                <th>Action</th>
                <th>ID</th>
                <th>Registration number</th>
                <th>Company Name</th>
                <th>Email</th>
            </tr>
        </thead>
        <tbody>
    
        <?php
            $conn = mysqli_connect("localhost", "root", "", "internship");
            if (mysqli_connect_errno()) {
                echo "Failed to connect to MySQL: " . mysqli_connect_error();
            }
            $sql = "";
    
            $sql = "select * from company where activation_code=0";
    
            $res = mysqli_query($conn, $sql);
    
            while ($row = mysqli_fetch_assoc($res)):
    
        ?>
    
            <tr>
                <td align="center">
                    <button type="submit" class="btn btn-default myButton" value="<?php echo $row['companyid']; ?>" id="accept" name="accept">Accept</button>
                </td>
                <td><?php echo $row['companyid']; ?></td>
                <td><?php echo $row['government_reg_no']; ?></td>
                <td><?php echo $row['company_name']; ?></td>
                <td><?php echo $row['email']; ?></td>
            </tr>
    
        <?php
            endwhile
        ?>
    
        </tbody>
    </table>
    <script>
        $(".myButton").click(function () {
            var company_id = $(this).val();
            $.ajax({
                type:"POST",
                url:"approvecompany.php",
                data:{ comid: company_id },
                success:function () {
                    window.location.reload(true);
                }
    
            });
        });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 如何提取csv文件中需要的列,将其整合为一篇完整文档,并进行jieba分词(语言-python)
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题