加载AJAX数据插入
我试图插入数据到数据库使用PHP,但我不知道如何做到这一点,其实我有搜索谷歌,但没有适当的解决方案。加载AJAX数据插入
我的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>
回答:
要通过Ajax传递多个值,则需要调整.ajax
呼叫这样(注意data
变化):在
$.ajax({ type: "POST",
url: "wba.php",
data: {
"id" : id,
"cd" : cd,
"ed" : ed,
"status" : status
},
success: function(data) {
alert("sucess");
}
});
,然后在PHP的身边你wba.php你在这些值得到:
$_POST['id'] $_POST['cd']
$_POST['ed']
$_POST['status']
至于所有其他数据库交互等,我们需要你自己尝试一下,找出你的目标和需要的结果。它有点宽泛,超出了StackOverflow的范围,因为别人现在可以为你编写所有这些。如果您需要进一步帮助,您随时可以提出其他问题以涵盖您遇到的具体问题。
但是重要的是:确保您使用“准备好的语句”!这将有助于保护您免受最常见的攻击媒介之一的侵害。
一个例子起动器(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();
以上是 加载AJAX数据插入 的全部内容, 来源链接: utcz.com/qa/266294.html