我正在开发项目,我必须使用jquery serialize函数将数据插入到mysql数据库中。除了来自select标签的数据外,所有数据都插入良好。我花了两天但却无法理解我错在哪里。 这是我的jquery代码。
<script>
$(document).ready(function(){
$("#addfields").click(function(e){
e.preventDefault();
$.ajax({
url: "makeschedule.php",
type: "post",
data: $("#scheduleform").serialize(),
success: function(d) {
alert(d);
}
});
});
});
</script>
<form method="post">
<select class="form-control" id="fromtime" name="fromtime">
<?php
for($i=1;$i<=12;$i++)
{
echo "<option value='$i'>$i.00</option>";
}
?>
</select>
<select class="form-control" id="totime" name="totime">
<?php
for($i=1;$i<=12;$i++)
{
echo "<option value='$i'>$i.00</option>";
}
?>
</select>
<a href="#" class="" id="addfields" name="addfields"><img src="imgs/icons/add.png" height=35 width=35></a>
</form>
PHP代码是:
<?php
if($_POST)
{
$userid=$_SESSION['s_userid'];
$userid=2;
$day= mysqli_real_escape_string($_POST['day']);
$fromtime= mysqli_real_escape_string($_POST['fromtime']);
$time1= mysqli_real_escape_string($_POST['am_pm1']);
$totime= mysqli_real_escape_string($_POST['totime']);
$time2= mysqli_real_escape_string($_POST['am_pm2']);
$qry="insert into a_yogischedule(id,userid,dayofweek,starttime,endtime,date) values(NULL,'$userid','2','$fromtime','$totime',now())";
$r=$dbconn->query($qry);
if($r)
echo "Success";
else
echo "Fail";
}
?>
提前致谢。