首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHP AJAX多表单不插入到数据库

PHP AJAX多表单不插入到数据库
EN

Stack Overflow用户
提问于 2018-06-21 04:22:39
回答 2查看 76关注 0票数 0

代码运行正常,但当我单击submit时,它不会插入到数据库中,也不会返回任何sql错误。这是我的索引代码:

代码语言:javascript
复制
<?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>

这是我的name.php

代码语言:javascript
复制
<?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选项。

EN

回答 2

Stack Overflow用户

发布于 2018-06-21 04:42:39

代码语言:javascript
复制
$sql = "INSERT INTO k_fixtures (league,team1,team2,dates,times)
VALUES ('$league','$home','$away','$date','$time')";

我想这就是你要找的嵌入物

票数 0
EN

Stack Overflow用户

发布于 2018-06-21 07:49:04

insert语句是正确的,但将insert放入如下条件中:-

代码语言:javascript
复制
if($error == ''){
     $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";
     }
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50956464

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档