index.php
<form action="../inc/q_camp.php" method="POST" class="modalForm">
<h3>Campaign</h3><br>
<label>Name</label><br>
<input type="text" name="campName" required><br>
<label>Customer</label><br>
<select name="client" required>
<option value="1">Abc</option>
</select><br>
<label>Start in</label><br>
<input type="date" name="campStart" id="cStrt" required><br>
<label>End in</label><br>
<input type="date" name="campStop" required><br>
<input type="button" name="toStore" value="Next">
</form>
ajax.js
/*This function submit ".modalForm"*/
function submitForm1(){
return $.ajax({
type: "POST",
url: "../inc/q_camp.php",
data: $(".modalForm").serialize()
});
}
/*When the button with name "toStore" is pressed, call submitForm1*/
$("input[name=toStore]").click(function(){
submitForm1();
});
q_camp.php
/*Submit the campaign*/
if(!empty($_POST['campName']) || !empty($_POST['client']) ||
!empty($_POST['campStart']) || !empty($_POST['campStop']))
{
$sql = "INSERT INTO campaigns(cmp_name, customer_id, cmp_start, cmp_stop) VALUES (:cName,:cId,:cStr,:cStp)";
$query = $db->prepare($sql);
$query->bindParam('cName', $_POST['campName']);
$query->bindParam('cId', $_POST['client']);
$query->bindParam('cStr', $_POST['campStart']);
$query->bindParam('cStp', $_POST['campStop']);
$query->execute();
}else print_r("Failed");
即使不满足php if语句,ajax函数也会插入数据库中...如果我让“ campName”输入为空,则会出现“ Failed”,但该行会像“ cmp_id 1”一样插入数据库中cmp_name NOTHING,customer_id 1,cmp_start 00-00-0000,cmp_stop 00-00-0000”。
在数据库中,所有字段都设置为NOT NULL。
我想了解即使不满足PHP的if语句,AJAX如何也可以访问SQL查询,即使列设置为null也如何插入行以及我在做什么错。