将一组对象传递到数据库中

时间:2016-09-15 04:25:05

标签: php mysql arrays forms

我有2页表单,允许用户添加进程,然后为每个进程分配内容。所以现在的问题是如何将数组进程保存到数据库中(将每个进程插入到单独的行中)。在我的数据库中,我有两个表:'project','process'。所以进程数组应该存储在'process'表中你可以在这里查看和测试表单: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"];
    }
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您可以在插入之前对数组进行json_encode,并在检索时对其进行json_decode。