我試圖插入數據到數據庫使用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>
哪裏是你的wba.php文件?與我們分享 –
您通過ajax傳遞的唯一變量就是狀態。所以在你的PHP中,你只能通過這種方式獲得狀態... $ _POST ['status'] – Roy
@MdMonjurUlHasan我已經添加了WBA文件代碼。請檢查 –