php - How to insert multiple ID's in one linked table? -
i want insert de employeeid , knowledgeid in knowledgedetail. creates employee nothing in knowledgedetail. i'm there no code, have tried many things have no idea.
first in addprofile.php make profile , @ least choose yoour knowledge. question if make profile , choose knowledge how de id's in knowledgedetail.
table 1
employees: employeeid, name, establishment, e-mail, phonenumber, photo, description
table 2
knowledge: knowledgeid, knowledge
table 3
knowledgedetail: knowledgedetailid, employeeid knowledgeid
employeeid out employees have relation employeeid out knowledgedetail and
knowledgeid out knowledge have relation knowledgeid out knowledegedetail
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 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 add list of selected items if foreach ($selected $sel) { ?> <option value="<?php echo $sel; ?>" selected="selected"><?php echo $id[$sel]; $items[$sel];?></option> <?php } ?> <?php foreach ($items $i => $v) { //then insert items, skipping 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 employees ( name, establishment, email, phonenumber, photo, description ) values ('". $name ."', '". $establishment ."', '". $email ."', '". $phonenumber ."', '". $photo ."', '". $description ."')"; $sqldetail = "insert knowledgedetail ( employeeid, knowledgeid ) values ......................."; $add = mysql_query($sql); if ($add === false){ echo 'profile not created'; } else { echo "profile created"; } echo '</br>'; $knowledge = mysql_query($sqldetail); if ($add === false){ echo 'knowledge not added'; } else { echo "knowledge added"; } echo '</br>'; ?>
here's 1 thing that's wrong code:
$knowledge = mysql_query($sqldetail); if ($add === false){ echo 'knowledge not added'; } else { echo "knowledge added"; }
in if
statement, should compare $knowledge
, not $add
. so, should be:
$knowledge = mysql_query($sqldetail); if ($knowledge === false){ echo 'knowledge not added'; } else { echo "knowledge added"; }
also, add call mysql_error()
every time mysql_query()
fails:
echo "mysql error: sql = $sql -- error=".mysql_error()";