一个叫做Job,另一个称为属性。 Attributes表取决于Job表,因为一个表可以有很多Attributes。作业表包含几个字段,即
Jobid(自动增量,PRIMARY KEY),JobName,Jobdescription
“属性”表包含以下字段
id(自动增量,PRIMARY KEY,AttribName,Score,Jobid(作业表中的外键)。
每个作业输入的属性数量各不相同,因此一个作业可以有10个属性,而另一个作业可能有2,3,4等属性。
以下是我遇到困难之前创建的代码。
Insert_job.php
<?php
include 'html/head.php';//connect to connect to the database and initialize all functions
include 'scripts/functions/init.php';
include 'html/page_title.php';
include 'html/top_menu.php';
include 'titles/Job_insert.php';
if(empty($_POST)=== false)
{
$R_fields = array('JobName','JobDesc','JobDuties','RecruitmentProcess','ContractType','SPackage');
foreach($_POST as $key=>$value)
{
if (empty($value) && in_array($key,$R_fields)=== true)
{
$errors[] = 'fields marked with (*) are required';
break 1;
}
}
if(empty($errors)=== true)
{
if($_POST['DirectorateName'] == '------ select ------')
{
$errors[] ='Please select Directorate Name';
}
if($_POST['Attributes'] == '-select-')
{
$errors[] ='Please Select the Number of Attributes';
}
}
}
include 'html/job_insert.php';
//Check if the form is not empty then submit details
if(empty($_POST) === false && empty($errors)=== true)
{
//store input into the session variables
$_SESSION['JobName'] = $_POST['JobName'];
$_SESSION['JobDesc'] = $_POST['JobDesc'];
//Store the number of attributes to get captured
$_SESSION['Attributes'] = $_POST['Attributes'];
//redirect
header('Location: Rank.php');
exit();
}
else if(empty($errors) === false)
{
//output errors if the errors array is not empty
echo output($errors);
}
?>
</div> <!-- div class entry ends here -->
</div> <!-- div post ends here -->
</div> <!-- div idcontents ends here -->
<!-- end #content -->
<?php
include 'html/top_side.php';
include 'html/side_other.php';
include 'html/side_bottom.php';
include 'html/footer.php';
?>
您会注意到我将所有输入存储到会话变量中并将其带到下一页,我将一次性插入到两个表中。
以下代码适用于Rank.php
<?php
include 'scripts/functions/init.php';
include 'html/head.php';
include 'html/page_title.php';
include 'html/top_menu.php';
include 'titles/Rank.php';
//declare the Array to store user input
$job_array = array();
//declare the Array to store attributes
$attributes = array();
//declare the Array to store attributes scores
$scores = array();
//Number of input fields selected by user on Job_insert.php page
$Number = $_SESSION['Attributes'];
//User Input from Job_insert.php page
$JobName = $_SESSION['JobName'];
$JobDesc = $_SESSION['JobDesc'];
$JobDuties = $_SESSION['JobDuties'];
$RProcess = $_SESSION['RecruitmentProcess'];
$SPackage = $_SESSION['SPackage'];
$DName = $_SESSION['DirectorateName'];
//Store user input Job details into an array
$job_array = array(
'JobName' =>$JobName,
'JobDesc'=>$JobDesc,
'JobDuties' =>$JobDuties,
'RecruitmentProcess'=>$RProcess,
'SPackage'=>$SPackage,
'DirectorateName'=>$DName);
//Check if the form is not empty then submit details
if(empty($_POST) === false && empty($errors)=== true)
{
//submit job details
job_insert($job_array);
//Store the current jobid into a variable
$jobid = mysql_insert_id();
for($i =0;$i<$Number;$count++)
{
//This is where I am getting stuck
}
//redirect
header('Location: Rank.php');
exit();
}
else if(empty($errors) === false)
{
//output errors if the errors array is not empty
echo output($errors);
}
//将表单输出到屏幕 包括'html / Rank.php';
以下是我如何将用户输入存储到数组中的示例
<form action = "" method ="POST" enctype="multipart/form-data">
<fieldset>
<?php
if($Number == 10)
{
echo '<table border="0">';
echo '<th>'.'Attribute'.'</th>';
echo '<th>'.'Score'.'</th>';
//Print the first row of the result set
echo '<tr>';
//First Form
echo '<td>'.'<input type="text" size="35" name="attributes[]">'.'</td>';
echo '<td>'.'<select id="select1" name ="Score[]">
<option>-select-</option>
<option>10</option>
<option>9</option>
<option>8</option>
<option>7</option>
<option>6</option>
<option>5</option>
<option>4</option>
<option>3</option>
<option>2</option>
<option>1</option>
</select>'.'</td>';
echo '</tr>';
//Second Form
echo '<tr>';
echo '<td>'.'<input type="text" size="35" name="attributes[]">'.'</td>';
echo '<td>'.'<select id="select2" name = "score[]"">
<option>-select-</option>
<option>10</option>
<option>9</option>
<option>8</option>
<option>7</option>
<option>6</option>
<option>5</option>
<option>4</option>
<option>3</option>
<option>2</option>
<option>1</option>
</select>'.'</td>';
echo '</tr>';
基本上我将用户输入存储到名为Attributes的数组和另一个Array Called Score中。将作业详细信息插入作业表工作正常,但现在我需要将包含在Attributes和Score数组中的信息插入到mysql数据库中。请协助
答案 0 :(得分:0)
不确定你的意思
//This is where I am getting stuck
但您基本上想要为已创建的作业插入属性。因此,您可以循环遍历属性列表并为其构建查询。
$query = 'INSERT INTO attributes
(name, score, job_id)
VALUES';
foreach ($_SESSION['Attributes'] as $attribute) {
$query .= "({$attributep['name']}, {$attribute['score']}, $job_id),";
}
执行该查询就像在job_insert中一样。