PHP AJAX多表单未插入数据库

时间:2018-06-20 20:22:39

标签: php jquery ajax

代码正常工作,但是当我单击提交时,它不会插入数据库并且不会返回任何SQL错误。 这是我的索引代码:

<?php
require_once("functions/functions.php");
$db_handle = new DBController();
$query ="SELECT * FROM country";
$results = $db_handle->runQuery($query);
error_reporting();
?>
<html>
	<head>
		<title>Kickoff360 - Add Match Fixtures</title>
		<link rel="stylesheet" href="/style/bootstrap.min.css" >
		<link rel="stylesheet" href="/style/style.css" >
		<script src="/style/bootstrap.min.js"></script>
				<script src="/style/jquery.min.js"></script>
    </head>
	<body>

		<div class="container"><h3>Add Match Fixtures</h3>
			<div class="omenu">P/S: Use the <b>Add More</b> button to add more input fields, <b>X</b> to remove input fields</div>
			<br />

			<div class="form-group">
				<form name="add_team" class="form-inline" id="add_team">
					<div class="table-responsive">
						<table class="table table-bordered" width="100%" id="fieldset">
							<tr class="danger">
								<td width="10%" class="trH"><span>LEAGUE</span></td>
								<td width="60%" class="trH"><span>HOME & AWAY TEAMS</span></td>
								<td width="3%" class="trH"><span>DATE</span></td>
								<td width="3%" class="trH"><span>TIME</span></td> </tr></thead>
							<tr id="row1"><td width="10%">
								<select name="league[]" class="form-control" onChange="getState(this.value);">
								<option value="">Select League</option>
								<?php foreach($results as $country) { ?><option id="<?php echo $country["id"]; ?>" value="<?php echo $country["id"]; ?>"><?php echo $country["country_name"]; ?></option>
							<?php } ?></select></td><td width="60%">
							<select class="form-control" name="home[]" id="home1" class="select"><option value="">HOME TEAM</option></select>
								<span class="btn btn-danger">VS</span>
							<select class="form-control" name="away[]" id="away1" class="select"><option value="">AWAY TEAM</option></select>
						</td><td width="3%"><input type="date" name="date[]" class="form-control"></td> <td width="3%"><input type="time" name="time[]" class="form-control"></td>
	<td width="20%"><button type="button" name="add" id="add" class="btn btn-success"  style="margin-left: 1px; margin-right: -1;">Add</button><span></span><button type="button" class="btn btn-success" style="display:inline-block;" id="submit" name="submit">Submit</button></td></tr>
	</table>

					</div>
				</form>
			</div>
		</div>
	</body>
</html>

<script>

		var i=1;
		function getState(val) {
			var home = $("#home"+i+"");
			var away = $("#away"+i+"");
				$.ajax({
				type: "POST",
				url: "name.php",
				data: 'country_id='+val,
				success: function(data){
					$(home).html(data).serialize();
					$(away).html(data).serialize();
				}
				});
			}
				function selectCountry(val) {
				$("#search-box").val(val);
				$("#suggesstion-box").hide();
				}

$(document).ready(function (){


	$('#add').click(function(){
		i++;
		var home = $("#home"+i+"");
		var away = $("#away"+i+"");
		$(fieldset).append('<tr id="row'+i+'"><td width="10%"><select name="country" class="form-control" onChange="getState(this.value);"><option value="">Select Country</option><?php foreach($results as $country) { ?><option value="<?php echo $country["id"]; ?>"><?php echo $country["country_name"]; ?></option><?php } ?></select></td><td width="50%"><select name="home[]" id="home'+i+'" class="form-control"><option value="">HOME TEAM</option></select> <span class="btn btn-danger">VS</span>  <select name="away[]" id="away'+i+'" class="form-control"><option value="">AWAY TEAM</option></select></td><td width="3%"><input type="date" class="form-control"></td> <td width="3%"><input type="time" class="form-control"></td><td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>');
	});

	$(document).on('click', '.btn_remove', function(){
		var button_id = $(this).attr("id");
		$('#row'+button_id+'').remove();
	});

	$('#submit').click(function(){
		$.ajax({
			url:"name.php",
			method:"POST",
			data:$('#add_team').serialize(),
			success:function(data)
			{
				alert(data);
				//$('#add_team')[0].reset();


			}
		});
	});

});
</script>

这是我的名字。php

<?php
require_once('C:\wamp645\www\fixtures\functions\functions.php');

$db_handle = new DBController();
if(!empty($_POST["country_id"])) {
	$query = "SELECT * FROM states WHERE countryID = '" . $_POST["country_id"] . "'";
	$results = $db_handle->runQuery($query);
?>
	<option value="">SELECT TEAM</option>
<?php
	foreach($results as $state) {
?>
	<option value="<?php echo $state["name"]; ?>"><?php echo $state["name"]; ?></option>
<?php
	}
}
$connect = mysqli_connect("localhost", "root", "", "k_fixtures");
$number = count($_POST["home"]);
if($number >= 0)
{
	for($i=0; $i<$number; $i++)
	{
		$league = $_POST['league'][$i];
		$home = escapeStr($_POST["home"][$i]);
		$away = escapeStr($_POST["away"][$i]);
		$date = escapeStr($_POST['date'][$i]);
		$time = escapeStr($_POST['time'][$i]);

$error = array();
		if(empty($league)) {
	$error = "This field cannot be blank";
}
		if(empty($home)) {
			$error = "This field cannot be blank";
		}
		if(empty($away)) {
			$error = "This field cannot be blank";
		}
		if(empty($date)) {
			$error = "This field cannot be blank";
		}
		if(empty($time)) {
			$error = "This field cannot be blank";
		}
			$sql = "INSERT INTO k_fixtures SET
			league = '$league',
			team1 = '$home',
			team2 = '$away',
			dates = '$date',
			times = '$time'
			";
			$sql2 = querySql($sql); or die(mysqli_error($connect));
}
	}
	if($sql2) {
	echo "Data Inserted";
}
else
{
	echo "Data not Inserted";
}

注意:函数querySql()已在functions.php中声明。 在我使用文本输入之前,代码一直存储到我的数据库中,直到我最近添加了select选项。

2 个答案:

答案 0 :(得分:0)

$sql = "INSERT INTO k_fixtures (league,team1,team2,dates,times)
VALUES ('$league','$home','$away','$date','$time')";

我认为这是您要寻找的插入内容

答案 1 :(得分:0)

insert语句是正确的,但是将插入置于类似以下条件:-

         Data             Id         Text
0  2018-06-11  17980873.3391  bla bla bla
1  2018-06-11  17980874.4560  bla bla bla
2  2018-06-11  17980876.8560  bla bla bla