-1
我有2頁的表格,它允許用戶添加進程,然後將內容分配給每個進程。所以現在的問題是如何將數組進程保存到數據庫,(將每個進程插入單獨的行)。在我的數據庫中,我有兩個表格:'項目','流程'。因此,進程數組應該存儲在'進程'表中您可以在這裏查看和測試表單:http://headcount.esy.es/drag/我至少需要一些關於如何傳遞並將此值分解到數據庫的想法。 因此,這裏是SQL代碼應該插入數據的數據庫,但它不工作:將對象數組傳遞到數據庫中
require_once 'helper/helper.clean_data.php';
require_once('inc/config.php');
require 'model/model.project.php';
if(isset($_POST['submit']) || isset($_POST['save']))
{
global $array_object;
//getting values for the first table 'project'
if (isset($_POST["holdcode"])) {$holdcode = $_POST["holdcode"];}
if (isset($_POST["holdtitle"])) {$holdtitle = $_POST["holdtitle"];}
if (isset($_POST["leader"])) {$leader = $_POST["leader"];}
//getting values for the second table 'process'
if (isset($_POST["holdprocess"])) {$holdprocess = $_POST["holdprocess"];}
if (isset($_POST["holdnumber"])) {$holdnumber = $_POST["holdnumber"];}
if (isset($_POST["procleader"])) {$procleader = $_POST["procleader"];}
$counta = 1;
}
else
{
$counta = 0;
}
function insertRecord()
{
global $holdcode, $holdtitle, $leader, $holdprocess, $holdnumber, $procleader;
//* database connection
$host="mysql.hostinger.my"; // Host name
$username="u624343149_pp"; // Mysql username
$password="123987"; // Mysql password
$db_name="u624343149_pp"; // Database name
// Connect to server and select database.
$con = mysqli_connect("$host", "$username", "$password", "$db_name");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// To protect MySQL injection (more detail about MySQL injection)
$holdcode = cleanInputData($holdcode);
$holdtitle = cleanInputData($holdtitle);
$leader = cleanInputData($leader);
$holdprocess = cleanInputData($holdprocess);
$holdnumber = cleanInputData($holdnumber);
$procleader = cleanInputData($procleader);
$holdcode = mysqli_real_escape_string($con, $holdcode);
$holdtitle = mysqli_real_escape_string($con, $holdtitle);
$leader = mysqli_real_escape_string($con, $leader);
$holdprocess = mysqli_real_escape_string($con, $holdprocess);
$holdnumber = mysqli_real_escape_string($con, $holdnumber);
$procleader = mysqli_real_escape_string($con, $procleader);
$result = getLastReportNo();
if (!$result) {
die('Invalid querrrrry: ' . mysql_error());
}
//** createing project number using currnet date
if(mysqli_num_rows($result) == 0)
{
$year = date("y");
$month = date("m");
$number = sprintf("%04d", 1);
$yearStr = strval($year);
$monthStr = strval($month);
$numberStr = strval($number);
$projectNo = $yearStr . $monthStr . $numberStr;
}
else
{
if ($row_last = mysqli_fetch_array($result))
{
$row_last[0] = cleanOutputData($row_last[0]);
$projectNo = $row_last[0];
$extractedNumberStr = substr($projectNo, -4);
$extractedYearMonthStr = substr($projectNo, 0, 4);
$year = date("y");
$month = date("m");
$yearStr = strval($year);
$monthStr = strval($month);
$currentYearMonthStr = $yearStr . $monthStr;
if($extractedYearMonthStr == $currentYearMonthStr)
{
$extractedNumber = intval($extractedNumberStr);
$extractedNumber++;
$extractedNumber = sprintf("%04d", $extractedNumber);
$extractedNumberStr = strval($extractedNumber);
$projectNo = $currentYearMonthStr . $extractedNumberStr;
}
else
{
$number = sprintf("%04d", 1);
$projectNo = $currentYearMonthStr . $number;
}
}
}
mysqli_set_charset($con, 'utf8');
//query result inserting values into the first table 'project'
$result = insertNewProject($projectNo,$_SESSION['login_user'],$holdcode,$holdtitle, $leader);
// Check result
if (!$result)
{
die('Invalid queryyyyyyyyy: ' . mysql_error());
}
$result = getLastReportNo();
if (!$result)
{
die('Invalidddddd query: ' . mysql_error());
}
//splitting an array and inserting an array in a second table 'process'
if ($row_last = mysqli_fetch_array($result))
{
$row_last[0] = cleanOutputData($row_last[0]);
$projectNo = $row_last[0];
foreach($holdprocess as $process)
{
if(!empty($holdnumber))
{
if (is_array($holdnumber) || is_object($holdnumber))
{
foreach($holdnumber as $number)
{
if($process == $number)
{
$record = insertProcess($projectNo,$process,$procleader, $number);
break;
}
}
if (!$record) {
die('Invaliiiiiid query: ' . mysql_error());
}
}
}
}
}
}
function verifyFormFields()
{
global $holdcode, $holdtitle, $leader, $holdprocess, $holdnumber, $procleader;
date_default_timezone_set('Asia/Kuching');
$date1 = date("Y-m-d");
$date1=date_create($date1);
if(empty($holdcode)){
return "<p id ='err'>Please Enter Project Code</p>";
}
else if(empty($holdtitle)){
return "<p id ='err'>Please Enter Project Name</p>";
}
if(isset($_POST['save']))
{
//Get the existing report no from cookie
$projectNo = $_COOKIE["projectNo"];
}
}
感謝您的幫助!
這兩個方法在哪裏被調用,insertRecord()和verifyFormFields()?你正在顯示他們定義的位置,但他們在哪裏被調用? – Craig
@Craig在我的第二個表格中,create2.php。事情是我成功地能夠插入第一個表'項目'的數據,問題是隻有一個數組。 –