【发布时间】:2013-03-13 11:30:35
【问题描述】:
一个叫做 Job,另一个叫做 Attributes。 Attributes 表依赖于 Job 表,因为一张表可以有很多 Attributes。作业表包含几个字段,即
Jobid(auto increment, PRIMARY KEY), JobName, Jobdescription
属性表包含以下字段
id(auto increment, PRIMARY KEY, AttribName, Score, Jobid(Job Table 中的外键)。
每个作业要输入的属性数量不同,因此一个作业可以有 10 个属性,而另一个作业可能有 2、3、4 等属性。
以下是我卡住之前创建的代码。
插入_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 的数组和另一个名为 Score 的数组中。将作业详细信息插入作业表工作正常,但现在我需要将 Attributes 和 Score 数组中包含的信息插入 mysql 数据库。请帮忙
【问题讨论】:
-
这段代码有很多问题,很难提供帮助。首先:在将数据插入数据库之前清理输入。不要在脚本中间包含代码。准备你的库并包含它们。尝试将 html 输出与 php 逻辑分开。
-
我有一个用于清理输入的函数,但我在不同的脚本中执行此操作,然后在此脚本上调用该函数。
标签: php mysql arrays textfield