0

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

感谢 Alberto Ponte 的帮助,我能够从这个问题中构建初始的单选框系统(最初是复选框,由于明显的原因而更改):根据数组数据分离数组

现在我已经正确显示了单选按钮,我正在尝试将数据插入到 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())在数组上使用的建议,但我无法让它工作。

4

0 回答 0