如何在一个链接表中插入多个ID?

时间:2013-04-25 09:41:39

标签: php jquery mysql

我想在Knowledgedetail中插入de EmployeeID和KnowledgeID。他创建了员工,但在Knowledgedetail中没有做任何事情。我现在没有代码,我尝试了很多东西,但我不知道 首先在Addprofile.php中创建配置文件,至少你选择了你的知识。 我的问题是,如果制作个人资料并选择知识如何获得我在知识中的ID。

表1 员工:员工ID,姓名,编制,电子邮件,电话号码,照片,说明
表2 知识:KnowledgeID,Knowledge
 表3 Knowledgedetail :KnowledgedetailID,EmployeeID KnowledgeID

EmployeeID out员工与EmployeeID有关系,知识权限和知识 KnowledgeID out Knowledge与KnowledgeID outledegedetail

有关系

Addprofile.php

<?php
include("connection.php");
?>
<!DOCTYPE html>
<html>
<head>
    <title>Information System</title> 
    <link rel="stylesheet" type="text/css" href="css/test.css">
    <meta charset ="utf-8">
    <link rel='stylesheet' href='http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/smoothness/jquery-ui.css' type='text/css' media='screen' />  
    <link rel='stylesheet' href='css/ui.multiselect.css' type='text/css' media='screen' />
    <script src="../Informatiesysteem/js/jquery.min.js"></script>
    <script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'></script>
    <script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js'></script>
    <script type='text/javascript' src='../Informatiesysteem/js/ui.multiselect.js'></script>
    <script type='text/javascript'>
            jQuery(document).ready(function() {
                jQuery("#selectitems").multiselect();
            });
    </script>
</head>

<body>
    <div id="container">
        <div id="logo"></div>
            <div id="header">
            <h1>Add Profile</h1>
            </div>
                    <div id="menu">
                    </div>
                        <div id="content">
                <?php
                $result = mysql_query("select knowledgeid, knowledge from knowledge");
                $items = array(); 
                $selected = array();
                while ($row = mysql_fetch_array($result)){  
                $id [] = $row [ 'knowlegdeid' ];
                $items[] = $row[ 'knowledge' ];  
                }
                //form processing
                if (isset($_POST['selectitems'])) {
                $selected = $_POST['selectitems'];
                }       
                if (!empty($selected)) : print_r($selected); endif;
                ?>
                <form enctype="multipart/form-data" id="my form" method="post" action="Addedprofile.php">
                Name:            <input type="text" name="name" /></br>
                Establishment:   <input type="text" name="establishment"/></br>
                E-Mail:          <input type="email" name="email"/></br>            
                Phonenumber:     <input type="tel" name="phonenumber"/></br>    
                Photo:           <input type="file" name="photo"/></br>
                Description:     <textarea rows="4" cols="50" name="description"></textarea></br>
                Knowledge: <select name="selectitems[]" id="selectitems" multiple="multiple" style="width: 450px; height: 180px;">
                <?php //first we add the list of selected items if any
                foreach ($selected as $sel) { ?>
                <option value="<?php echo $sel; ?>" selected="selected"><?php echo $id[$sel]; $items[$sel];?></option>
                <?php } ?>
                <?php foreach ($items as $i => $v) { //then insert all items, skipping those who were added above
                if (in_array($d, $i, $selected)) : continue; endif; //skip ?>
                <option value="<?php echo $d, $i; ?>"><?php echo $v; ?></option>
                <?php } ?>
                </select>                                    
                </br></br></br></br>
                <input type="submit" name="add_profile" value="Add profile" />                       
                </form>
                </div>
</body>
</html> 

Addedprofile.php

<!DOCTYPE html>
<html>
<meta http-equiv="refresh" content=";URL=Addprofile.php" />
</html>
<?php
include ("connection.php");
$Name = $_POST['name'];
$Establishment = $_POST['establishment'];
$Email = $_POST['email'];
$Phonenumber = $_POST['phonenumber'];
$Photo = $_POST['photo'];
$Description = $_POST['description'];


$sql = "INSERT INTO employees  
        (
         name, 
         establishment,
         email,
         phonenumber,
         photo,
         description
         )
         VALUES ('". $Name ."', '". $Establishment ."', '". $Email ."', '". $Phonenumber ."', '". $Photo ."', '". $Description ."')";

$sqldetail = "INSERT INTO knowledgedetail 
        (
        employeeid,
        knowledgeid     
        )
        VALUES ......................."; 

                $add = mysql_query($sql);           



            if ($add === false){
                   echo 'Profile is not created';
             }
            else {               
                 echo "Profile created";
                 }

            echo '</br>';



$knowledge = mysql_query($sqldetail);                                       

        if ($add === false){
               echo 'Knowledge is not added';
         }
        else {               
             echo "Knowledge added";
             }

            echo '</br>';


?>

1 个答案:

答案 0 :(得分:0)

这是你的代码有一个问题:

$knowledge = mysql_query($sqldetail);                                       

if ($add === false){
    echo 'Knowledge is not added';
}
else {               
    echo "Knowledge added";
}

if声明中,您应该比较$knowledge而不是$add。所以,它应该是:

$knowledge = mysql_query($sqldetail);                                       

if ($knowledge === false){
    echo 'Knowledge is not added';
}
else {               
    echo "Knowledge added";
}

此外,每次mysql_error()失败时都会向mysql_query()添加电话:

echo "MySQL ERROR: SQL = $sql -- Error=".mysql_error()";