每个数组对象插入一行

时间:2013-02-07 15:38:21

标签: php mysql arrays post

我正在创建一个文档关联工具,用户可以将文档与预先存在的文档列表中的票证相关联。

感谢Alberto Ponte的帮助,我能够从这个问题构建最初的无线电盒系统(原来是复选框,由于显而易见的原因而改变):Separate out array based on array data

现在我正确地显示单选按钮我试图将数据插入到MySQL中,每个文档都有一行。接下来我希望让它查找每个对象的表,看看是否存在现有的条目和更新,而不是插入重复的条目。

提前为即将推出的折旧代码道歉,我将更正未来的代码,但这比我现在有时间要大得多。

如果代码被认为是凌乱的话,也会道歉。我在PHP方面并不出色,而且在最佳实践方面也不是很热门。任何快速指示都是受欢迎的。

显示代码:

<?php

include "../includes/auth.php";
include "../includes/header.php";

## Security ########################################################

if ($company_id != 1 OR $gid < 7) {

echo'          <div class="pageheader">
                  <div class="holder">
                     <br /><h1>Access Denied <small> Your attempt has been logged</small></h1>
            </div>
         </div>
';
exit();
}

// GET Values ###################################################
$jat_id = intval($_GET['id']);
$div_id = intval($_GET['div_id']);




## Page Start ########################################################



echo '
               <div class="pageheader">
                  <div class="holder">
                     <br /><h1>Clovemead Job Management Platform<small> Welcome...</small></h1>
            </div>
         </div>

<div class="well5" style="width:1000px !important;">
<h3>Create Document Association</h3>

<table border=1>
  <tr>
    <td align=center><p>Here you can associate documents to Jobs and Tasks to.</p> </td>
  </tr>
</table>';

            $order2 = "SELECT * FROM documents";
                $result2 = mysql_query($order2);
                while ($row2 = mysql_fetch_array($result2)) {

                $nice_name = $row2['nice_name'];
                $doc_id = $row2['id'];

}


echo '


<h3>Documents</h3>

<table border=1>
  <tr><th>Document Name</th><th>Document Type</th><th>Required</th><th>Optional</th><th>Not Required</th></tr>
';



            $order2 = "SELECT * FROM documents ORDER BY type_id";
                $result2 = mysql_query($order2);
                while ($row2 = mysql_fetch_array($result2)) {

                $nice_name = $row2['nice_name'];
                $doc_id = $row2['id'];
                $doc_type_id = $row2['type_id'];

echo '

  <tr>
    <td>'.$nice_name.'</td>';

            $order3 = "SELECT * FROM document_types WHERE id = '$doc_type_id'";
                $result3 = mysql_query($order3);
                while ($row3 = mysql_fetch_array($result3)) {

                $type_name = $row3['type_name'];

echo '
    <td>'.$type_name.'</td>
    <form method="post" action="create-doc-assoc-exec.php">
        <input type="hidden" value="'.$doc_id.'" name="doc_id">
        <input type="hidden" value="'.$jat_id.'" name="jat_id">
        <input type="hidden" value="'.$div_id.'" name="div_id">';

                $order4 = "SELECT * FROM document_assoc WHERE doc_id = '$doc_id'";
                $result400 = mysql_query($order4);
                $_results = mysql_fetch_array($result400);
                if (!$_results) {
    echo '
                    <td><input type="radio" name="req0" value="2"></td>
                        <td><input type="radio" name="req0" value="1"></td>
                        <td><input type="radio" name="req0" value="0" checked ></td>';
                    } else {
                            foreach ($_results as $result400) {
                            $requirement = $_results['requirement'];
                                        }
                    $name = "req".$doc_id;

    echo '
            <input type="hidden" value ="'.$name.'" name="name">
                <td><input type="radio" name="'.$name.'" value="2" '; if ($requirement == 2) { echo ' checked '; } echo '></td>
                <td><input type="radio" name="'.$name.'" value="1" '; if ($requirement == 1) { echo ' checked '; } echo '></td>
                <td><input type="radio" name="'.$name.'" value="0" '; if ($requirement < 1) { echo ' checked '; } echo '></td>';


    }

    }

echo '
  </tr>';
}
echo '
</table>

<input type="submit" name="submit value" value="Create Document Association" class="btn success Large"></form>';


            $order2 = "SELECT * FROM divisions WHERE id = '$div_id'";
                $result2 = mysql_query($order2);
                while ($row2 = mysql_fetch_array($result2)) {

                $dbname = $row2['division_dbname'];

            $order3 = "SELECT * FROM $dbname WHERE id = '$jat_id'";
                $result3 = mysql_query($order3);
                while ($row3 = mysql_fetch_array($result3)) {

                $type = $row3['type'];
                $type2 = strtolower($type);

echo '

        <input type="button" value="Back" onclick="window.location=&#39;./view-'.$type2.'.php?id='.$jat_id.'&#39;" class="btn primary Large">



';

}}
echo '
</div>';

include "../includes/footer.php";
?>

执行代码:

<?php
include "../includes/dbconnect.php";
$name = $_POST['name']; 
$name = stripslashes($name);
$name = mysql_real_escape_string($name);


$doc_id = intval($_POST['doc_id']); 
$jat_id = intval($_POST['jat_id']); 
$div_id = intval($_POST['div_id']); 
$req = intval($_POST[$name]); 
$req_blank = intval($_POST['req0']); 


if ($req_blank == 0) {
$requirement = 0;
} elseif ($req_blank == 1) {
$requirement = 1;
} elseif ($req_blank == 2) {
$requirement = 2;
} elseif ($req == 0) {
$requirement = 0;
} elseif ($req == 1) {
$requirement = 1;
} elseif ($req == 2) {
$requirement = 2;
}


foreach ($doc_id as $doc_id2) {


$order = "INSERT INTO document_assoc (jat_id, dept_id, doc_id, requirement) VALUES ('$jat_id', '$div_id', '$doc_id2', '$requirement')";
$result = mysql_query($order);

            $order1 = "SELECT * FROM divisions WHERE id = '$div_id'";
                $result1 = mysql_query($order1);
                while ($row1 = mysql_fetch_array($result1)) {

                    $dbname = $row1['division_dbname'];



            $order2 = "SELECT * FROM $dbname WHERE id = '$jat_id'";
                $result2 = mysql_query($order2);
                while ($row2 = mysql_fetch_array($result2)) {

                    $type = $row2['type'];
            $type2 = strtolower($type);

if($result){
header("location:view-$type2.php?id=$jat_id&creation=success");
} else {
header("location:view-$type2.php?id=$jat_id&creation=failed");
}
}}

}
?>

一些参考点:

$doc_id is the ID of each document which is to be passed over as an array for use in a (i believe) foreach insert
$jat_id is the ticket id documents are being associated to
$div_id is the department the ticket is associated
And the radio boxes are to decider the requirement of each document.  To be passed over inline with $doc_id

我尝试过:

经过一段时间的搜索 - 找不到任何东西 - 重新检查我的术语并再次搜索我找到了在阵列上使用base64_encode(serialize())的建议,但我无法使其工作。

0 个答案:

没有答案