2017-09-08 21 views
1

我在这里是新来的,不太方便与PHP的MySql。虽然我设法做了一些工作(在一些朋友的帮助下---),但它可能有点混乱),但我现在卡住了。如何将ID添加到另一个表中?

这是情况; 我有三张桌子; 参与者,项目和面试。

参与者表在面试开始时填写了我面试的人的详细信息。

项目表是由我在面试过程中使用的一组主题组成的。

在面试过程中面试表将填入数据。

什么工作是我有系统显示在一个复选框,一个文本框和一个选择框旁边的弹出页面中的项目。 输入到表单域中的数据被添加到采访表中。

我还需要的是将参与者的ID和itemID以及itemID添加到采访表中。有人可以帮我吗?

这是弹出页面的调用代码;

<div class="communication"> 
     <a href="popups/DeveloperCommunication.php" onclick="window.open('popups/DeveloperCommunication.php','communication', 'width=800,height=350,scrollbars=no,toolbar=no,location=no'); return false">   
     <?php 
     // get the records from the database 
     if ($result = $conn->query('SELECT Categories FROM categories WHERE ID="2"')) 
     { 
     // display records if there are records to display 
     if ($result->num_rows > 0) 
     { 

     while ($row = $result->fetch_object()) 
     { 
     // set up a row for each record 
     echo $row->Categories; 
     } 
     } 
     // if there are no records in the database, display an alert message 
     else 
     { 
     echo "No results to display!"; 
     } 
     } 
     // show an error if there is an issue with the database query 
     else 
     { 
     echo "Error: " . $conn->error; 
     } 
     ?> 
     </a></a> 
    </div> 

这是弹出页面的代码;

<?php 
    include '../../include/dbh.inc.php'; 
    date_default_timezone_set('Europe/Amsterdam'); 
?> 

<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>Developer Communication</title> 
<link href="../../css/chapterDeveloper.css" rel="stylesheet" type="text/css" media="screen"> 
</head> 

<body> 
<div class="popupTitle"> 
    <p>Developer Communication</p> 
</div> 

<?php 

    $comment = ""; 

// connect to the database 
$result = $conn->query('SELECT participant.ID, items.items, participant.yes, Participant.no, Participant.question, Participant.comment 
FROM participant 
RIGHT JOIN items ON participant.itemID=items.ID 
WHERE items.role="DE" AND items.categorie="Communication" 
ORDER BY items.items') 

?> 

<form method="post" action="../../include/func.participant.inc.php" onsubmit="refreshAndClose()"> 

<table> 
    <tr> 
     <!--<th></th>--><th>Items</th><th>Y</th><th>N</th><th>?</th><th>Comment</th><th>levels</th> 
     <?php 
     while ($row = mysqli_fetch_assoc($result)) {?> 
    </tr> 
    <tr> 
     <td> 
       <?php echo $row['items'] ?> 
     </td> 
     <td> 
       <input name="items[<?php echo $row['ID']; ?>][yes]" type="checkbox" value="yes" <?php if ($row['yes'] == "yes") echo "checked"; ?>/> 
     </td> 
     <td> 
       <input name="items[<?php echo $row['ID']; ?>][no]" type="checkbox" value="no" <?php if ($row['no'] == "yes") echo "checked"; ?>/> 
     </td> 
     <td> 
       <input name="items[<?php echo $row['ID']; ?>][question]" type="checkbox" value="question" <?php if ($row['question'] == "yes") echo "checked"; ?>/> 
     </td> 
     <td> 
       <textarea name="items[<?php echo $row['ID']; ?>][comment]" rows="1" cols="25" placeholder="comment"><?php echo $comment;?></textarea> 
     </td>  
     <td> 
      <select name="items[<?php echo $row['ID']; ?>][level]"> 
       <option value="">Select...</option> 
       <option value="1">1. Starter</option> 
       <option value="2">2. Junior</option> 
       <option value="3">3. Intermediate</option> 
       <option value="4">4. Senior</option> 
       <option value="5">5. Expert</option> 
       <option value="6">6. Un Known</option> 
       <option value="7">7. Future</option> 
       <option value="8">8. Not relefant</option> 
      </select> 
     </td> 
    </tr> 
     <?php } ?> 
</table> 
<input type="submit" name="submit" value="Submit"> 

</form> 

<?php 
function refreshAndClose() { 
      window.opener.location.reload(true); 
      window.close(); 
} ?> 
</body> 
</html> 

而这是在弹出页面中使用的函数;

<?php include 'dbh.inc.php'; ?> 

<?php 

var_dump($_POST); //met $_POST wordt de array met de verschillende items getoond 

/* 
* Je lust nu door deze array met items, en ieder item sla je op in de database obv de id 
*/ 
foreach($_POST['items'] as $item) { 
    $query = "INSERT INTO interview (participantID, itemID, item, role, yes, no, question, comment, level) 
       VALUES ('DE', '11', '12', '13', '".mysqli_real_escape_string($conn, $item['yes'])."', 
         '".mysqli_real_escape_string($conn, $item['no'])."', 
         '".mysqli_real_escape_string($conn, $item['question'])."', 
         '".mysqli_real_escape_string($conn, $item['comment'])."', 
         '".mysqli_real_escape_string($conn, $item['level'])."', 
      )"; 
    mysqli_query($conn, $query); 

    echo "<br>Record toegevoegd! ($query)<hr>"; 
} 
# 

位置11需要是参与者ID,位置12需要是itemID,位置13需要是它自己的项目。

我希望有人能帮助我。

Greetz。

回答

0

FWIW,我觉得这样更容易阅读和理解,但是查看这些表的CREATE和INSERT语句以及所需的结果会很有用。

SELECT p.ID 
    , i.items 
    , p.yes 
    , p.no 
    , p.question 
    , p.comment 
    FROM items i 
    LEFT 
    JOIN participant p 
    ON p.itemID = i.ID 
WHERE i.role = "DE" 
    AND i.categorie = "Communication" 
ORDER 
    BY i.items 

顺便说一句,既具有肯定的和无柱是奇数

0

谢谢你帮我出去!

这确实读取更加容易,但我不这样用别名喷...

创建我使用了三个表的语句;

CREATE TABLE `participants` (
    `ID` int(11) NOT NULL, 
    `first_name` text NOT NULL, 
    `insertion` text NOT NULL, 
    `last_name` text NOT NULL, 
    `position` text NOT NULL, 
    `email` text NOT NULL, 
    `datetime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, 
    `role` varchar(2) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

CREATE TABLE `items` (
    `ID` int(3) NOT NULL, 
    `items` varchar(75) DEFAULT NULL, 
    `categorie` varchar(16) DEFAULT NULL, 
    `role` varchar(2) DEFAULT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

CREATE TABLE `interview` (
    `ID` int(11) NOT NULL, 
    `participantID` int(11) NOT NULL, 
    `ItemID` int(11) NOT NULL, 
    `item` varchar(255) NOT NULL, 
    `Categorie` varchar(255) NOT NULL, 
    `role` varchar(2) NOT NULL, 
    `yes` varchar(1) NOT NULL, 
    `no` varchar(1) NOT NULL, 
    `question` varchar(1) NOT NULL, 
    `comment` text NOT NULL, 
    `level` int(11) NOT NULL, 
    `datetime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

插入语句已在所提供的函数中准备好了。

参与者的插入语句是;

<?php include 'dbh.inc.php'; ?> 

<?php 

// create a variable 
$first_name=$_POST['first_name']; 
$insertion=$_POST['insertion']; 
$last_name=$_POST['last_name']; 
$position=$_POST['position']; 
$role=$_POST['role']; 
$email=$_POST['email']; 

//Execute the query 


mysqli_query($conn,"INSERT INTO participant (first_name, insertion, last_name,position,email,role) VALUES ('$first_name','$insertion','$last_name','$position','$email','$role')"); 

    if(mysqli_affected_rows($conn) > 0){ 
    echo "<p>Participant Added</p>"; 
    echo "<a href='../indexnl.php'>Select role to scan</a>"; 
} else { 
    echo "Participant NOT Added<br />"; 
    echo mysqli_error ($conn); 
} 
相关问题