我引用了这个问题,但无法从中获取任何可操作的内容以帮助解决我的问题。
PHP INSERT a variable number of records to mysql from a html form
我有一个表单,用于添加无限数量的字段来记录bin权重(jquery)。我无法正确地将数据输入到mysql数据库中。我已经构建了表来建立多对多关系,允许使用唯一的bin id记录bin权重,同时将其重新绑定到接收静态输入的整个作业表中。以下是相关表格。 jobNumber是jobOpen表的主键,它不是AutoIncrementing,而是唯一的。
CREATE TABLE IF NOT EXISTS `Production`.`jobCode` (
`jobCodeID` INT NOT NULL,
`jobCodeDesc` VARCHAR(45) NOT NULL,
PRIMARY KEY (`jobCodeID`));
CREATE TABLE IF NOT EXISTS `Production`.`jobOpen` (
`jobNumber` VARCHAR(10) NOT NULL,
`jobCreationDate` DATE NOT NULL,
`Receiving_recID` INT NOT NULL,
`jobType` VARCHAR(45) NOT NULL,
`jobCode_jobCodeID` INT NOT NULL,
PRIMARY KEY (`jobNumber`),
UNIQUE INDEX `jobNumber_UNIQUE` (`jobNumber` ASC),
INDEX `fk_jobOpen_Receiving1_idx` (`Receiving_recID` ASC),
INDEX `fk_jobOpen_jobCode1_idx` (`jobCode_jobCodeID` ASC),
CONSTRAINT `fk_jobOpen_meatReceiving1`
FOREIGN KEY (`Receiving_recID`)
REFERENCES `Production`.`Receiving` (`recID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_jobOpen_jobCode1`
FOREIGN KEY (`jobCode_jobCodeID`)
REFERENCES `Production`.`jobCode` (`jobCodeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
CREATE TABLE IF NOT EXISTS `Production`.`jobBins` (
`binID` INT NOT NULL AUTO_INCREMENT,
`binWeight` DECIMAL(10,0) NOT NULL,
PRIMARY KEY (`binID`),
UNIQUE INDEX `binID_UNIQUE` (`binID` ASC));
CREATE TABLE IF NOT EXISTS `Production`.`jobBins_has_jobOpen` (
`jobBins_binID` INT NOT NULL,
`jobOpen_jobNumber` VARCHAR(10) NOT NULL,
PRIMARY KEY (`jobBins_binID`, `jobOpen_jobNumber`),
INDEX `fk_jobBins_has_jobOpen_jobOpen1_idx` (`jobOpen_jobNumber` ASC),
INDEX `fk_jobBins_has_jobOpen_jobBins1_idx` (`jobBins_binID` ASC),
CONSTRAINT `fk_jobBins_has_jobOpen_jobBins1`
FOREIGN KEY (`jobBins_binID`)
REFERENCES `Production`.`jobBins` (`binID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_jobBins_has_jobOpen_jobOpen1`
FOREIGN KEY (`jobOpen_jobNumber`)
REFERENCES `Production`.`jobOpen` (`jobNumber`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
CREATE TABLE IF NOT EXISTS `Production`.`Receiving` (
`recID` INT NOT NULL,
`recDate` DATE NOT NULL,
`vendor` VARCHAR(45) NOT NULL,
`vendorEstNum` VARCHAR(45) NOT NULL,
`rawmatdesc_rawMatCode` VARCHAR(45) NOT NULL,
`rawMatCost` DECIMAL NOT NULL,
`recPO` VARCHAR(45) NOT NULL,
`recWeight` DECIMAL(10,2) NOT NULL,
PRIMARY KEY (`recID`),
INDEX `fk_meatReceiving_rawmatdesc1_idx` (`rawmatdesc_rawMatCode` ASC),
CONSTRAINT `fk_meatReceiving_rawmatdesc1`
FOREIGN KEY (`rawmatdesc_rawMatCode`)
REFERENCES `NatDeliProduction`.`rawmatdesc` (`rawMatCode`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
表单页面
<?php
//Include the database class
session_start();
require('../model/class.user.php');
$user = new USER();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859- 1">
<title>Open Job</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"> </script>
<link rel="stylesheet" href="../style.css" type="text/css" />
<script type="text/javascript">
var count = 0;
$(function(){
$('p#add_field').click(function(){
count += 1;
$('#container').append(
'<strong>Bin Weight' + count + '</strong><br />'
+ '<input id="field_' + count + '" name="fields[]' + '" type="number" /><br />' );
});
});
</script>
<body>
<?php
// if form submitted
if(isset($_POST['btnSubmit'])) {
$jobNumber = strip_tags($_POST['txt_jobNumber']);
$jobCreationDate = strip_tags($_POST['date_Date']);
$Receiving_recID = strip_tags($_POST['num_recID']);
$jobType = strip_tags($_POST['txt_jobType']);
$jobCode_jobCodeID = strip_tags($_POST['num_jobCode']);
$form_data = array(
'jobNumber' => $jobNumber,
'jobCreationDate' => $jobCreationDate,
'Receiving_recID' => $meatReceiving_recID,
'jobType' => $jobType,
'jobCode_jobCodeID' => $jobCode_jobCodeID,
);
if($jobNumber=="") {
$error[] = "provide job number !";
}
else if($jobCreationDate=="") {
$error[] = "provide the job creation date";
}
else if($Receiving_recID=="") {
$error[] = "provide receiving ID !";
}
else if($jobType=="") {
$error[] = "provide job type !";
}
else if($jobCode_jobCodeID=="") {
$error[] = "provide job code !";
}
else
{
try
{
$stmt = $user->runQuery("SELECT jobNumber FROM jobOpen WHERE jobNumber=:jobNumber");
$stmt->execute(array(':jobNumber'=>$jobNumber));
$row=$stmt->fetch(PDO::FETCH_ASSOC);
if($row['jobNumber']==$jobNumber) {
$error[] = "sorry product job number already exists !";
}
else
{
if($user->testInsert('jobOpen',$form_data)){
$user->redirect('jobOpen.php?jobAdded');
}
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
if ($_POST['fields']) {
//loop through added fields
foreach ($_POST['fields'] as $value) {
//Insert into job bins table
$postJobWeight = sprintf("INSERT INTO jobBins(binWeight) VALUES ('%s')",
mysql_real_escape_string($value));
$stmt = $user->runQuery($postJobWeight);
$insertedBinID = $user->last_insert_id();
//Insert in to link table
$postJobBinTable = sprintf("INSERT INTO jobBins_has_jobOpen (jobBins_binID,jobOpen_jobNumber) VALUES ('%s','%s')",
mysql_real_escape_string($insertedBinID),
mysql_real_escape_string($jobNumber));
$stmt = $user->runQuery($postJobBinTable);
}
} else {
}
echo "<h1> User Added, <strong>" . count($_POST['fields']) . "</strong> bin(s) for this job";
}
?>
<div class="user-form">
<div class="container">
<form method="post" class="form-signin">
<h2 class="form-signin-heading">Job Information</h2><hr />
<?php
if(isset($error))
{
foreach($error as $error)
{
?>
<div class="alert alert-danger">
<i class="glyphicon glyphicon-warning-sign"></i> <?php echo $error; ?>
</div>
<?php
}
}
else if(isset($_GET['jobAdded']))
{
?>
<div class="alert alert-info">
<i class="glyphicon glyphicon-log-in"></i> Job Opened Successfully
</div>
<?php
}
?>
<?php if (!isset($_POST['btnSubmit'])) { ?>
<label for="jobNumber">Job Number:</label>
<input type="text" name="txt_jobNumber" id="jobNumber" />
<div class="spacer"></div>
<label for="jobCreationDate">Job Date:</label>
<input type="date" name="date_Date" id="jobCreationDate" />
<div class="spacer"></div>
<label for="recID">Receiving ID:</label>
<input type="number" name="num_recID" id="recID" />
<div class="spacer"></div>
<label for="jobType">Job Type:</label>
<input type="text" name="txt_jobType" id="jobType" />
<div class="spacer"></div>
<label for="jobCode">Job Code:</label>
<input type="number" name="num_jobCode" id="jobType" />
<div class="spacer"></div>
<div id="container">
<p id="add_field"><a href="#"><span>» Add Bin Weight ...</span></a></p>
</div>
<div class="spacer"></div>
<input id="go" name="btnSubmit" type="submit" value="Open Job" class="btn" />
</form>
<?php } ?>
</body>
<?php include 'footer.php' ; ?>
class.user.php中的相关函数
public function testInsert($table_name, $form_data)
{
try
{
$fields = array_keys($form_data);
$values = array_values($form_data);
$stmt = $this->conn->prepare("INSERT INTO ".$table_name."(`".implode('`,`', $fields)."`)VALUES('".implode("','", $form_data)."')");
foreach ($form_data as $fields => $values){
$stmt->bindparam(':' . $fields,$values);
}
$stmt->execute();
return $stmt;
} catch (PDOException $e) {
echo $e->getMessage();
}
}
public function runQuery($sql)
{
$stmt = $this->conn->prepare($sql);
return $stmt;
}
public function last_insert_id() {
return mysql_insert_id();
}
静态输入可以很好地进入表jobOpen,但binWeights什么都不做。 jobBins表或jobBins_has_jobOpen表中没有填充任何内容。不会抛出任何错误,也不会创建或发布任何值。我确信我遗漏了一些非常简单的东西,但我对JQuery并不是很熟悉,而且我对PHP也不是很了不起......任何帮助都会非常感激。