加载时的AJAX数据插入

时间:2017-12-27 16:26:19

标签: php jquery mysql ajax

我正在尝试使用php将数据插入数据库,但我不知道如何做到这一点,事实上我也搜索谷歌,但没有适当的解决方案。

我的Ajax代码是:

if(window.location == "path/to/file") {
    $(window).on('load',function() {
        var id = $("#id").val();
        var cd = $("#cd").val();
        var ed = $("#ed").val();
        var status = $("#status").val();

        if(ed == cd){
            status = "Stopped";
            console.log(status);
            $.ajax({
                type: "POST",
                url: "wba.php",
                data: "status=" + status,
                success: function(data) {
                   alert("sucess");
                }
            });
        }
        else{
            console.log('Running');
        }
    });
}

现在我不知道如何从ajax获取数据并使用php和我的数据库表结构将其插入到我的数据库中

INSERT into table_name(id, cd, ed, status) VALUES();

我的WBA.php文件代码在下面的文件中提到我首先从表中获取数据,然后当前时间变为等于结束时间我希望此页面自动将广告状态从运行更改为已停止并更新值在数据库中没有刷新页面,甚至没有点击任何按钮。

<div id="main-content">
<div class="casteform">
<?php
    $query = "SELECT * FROM advertisements WHERE ad_category = 'weddingbellsad'";
    $run = mysqli_query($con, $query);
    if(mysqli_num_rows($run) > 0){
        while ($row = mysqli_fetch_assoc($run)) {
            $ad_id = mysqli_real_escape_string($con, $row['ad_id']);
            $ad_company = mysqli_real_escape_string($con, $row['ad_company']);
            $ad_start_date = mysqli_real_escape_string($con, $row['ad_start_date']);
            $ad_duration = mysqli_real_escape_string($con, $row['ad_duration']);
            $ad_reminder_days = mysqli_real_escape_string($con, $row['ad_reminder_days']);
            $ad_reminder_date = mysqli_real_escape_string($con, $row['ad_reminder_date']);
            $ad_end_date = mysqli_real_escape_string($con, $row['ad_end_date']);
            $ad_payment_method = mysqli_real_escape_string($con, $row['ad_payment_method']);
            $ad_payment_amount = mysqli_real_escape_string($con, $row['ad_payment_amount']);
            $ad_category = mysqli_real_escape_string($con, $row['ad_category']);
            $ad_status = mysqli_real_escape_string($con, $row['ad_status']);
            $ad_current_date = date('Y-m-d h:i:s');
        ?>
            <div class="adrow">
                <h4>Company Name: <?php echo $ad_company . " ( " . $ad_id . " ) "; ?> <small><?php echo $ad_status ?></small></h4>
                <ul>
                    <li><span>Starting Date:</span> <?php echo $ad_start_date; ?></li>
                    <li><span>Duration:</span> <?php echo $ad_duration . " Days"; ?></li>
                    <li><span>Reminder:</span> <?php echo "Before ". $ad_reminder_days . " Day(s)"; ?></li>
                    <li><span>Reminder Date:</span> <?php echo $ad_reminder_date; ?></li>
                    <li><span>Ending Date:</span> <?php echo $ad_end_date; ?></li>
                    <li><span>Payment Method:</span> <?php echo $ad_payment_method; ?></li>
                    <li><span>Payment Amount:</span> <?php echo "Rs - " . $ad_payment_amount; ?></li>
                    <li><span>Ad Category:</span> <?php echo $ad_category; ?></li>
                </ul>
                <form>
                    <input type="text" id="id" value="<?php echo $ad_id; ?>" />
                    <input type="text" id="cd" value="<?php echo $ad_current_date; ?>" />
                    <input type="text" id="ed" value="<?php echo $ad_end_date; ?>" />
                    <input type="text" id="status" value="<?php echo $ad_status; ?>" />
                </form>
            </div>
        <?php
        }
    }
    else{
        header("location: view-wedding-bell-ad.php?error=".urlencode("No advertisements found so far."));
        exit();
    }
?>
</div>

1 个答案:

答案 0 :(得分:1)

要通过ajax传递多个值,您需要调整.ajax这样的调用(请注意data更改):

$.ajax({
    type: "POST",
    url: "wba.php",
    data: {
        "id" : id,
        "cd" : cd,
        "ed" : ed,
        "status" : status
    },
    success: function(data) {
        alert("sucess");
    }
});

然后在你的 wba.php 的PHP方面,你可以通过以下方式获得这些价值:

$_POST['id']
$_POST['cd']
$_POST['ed']
$_POST['status']

对于所有其他数据库交互等,我们需要您自己尝试,并找出您的目标和所需的结果。它有点宽泛,超出了StackOverflow的范围,让别人可以为你写这一切。如果您需要进一步的帮助,您可以随时提出另一个问题来涵盖您遇到的特定问题。

但重要的是:确保您使用&#34;准备好的陈述&#34; !因为这将有助于保护您免受最常见的攻击媒介之一。

示例启动器(http://php.net/manual/en/mysqli-stmt.bind-param.php):

$stmt = $mysqli->prepare("INSERT into table_name(id, cd, ed, status) VALUES(?, ?, ?, ?)");
$stmt->bind_param('ssss', $_POST['id'], $_POST['cd'], $_POST['ed'], $_POST['status']);
$stmt->execute();