php - SHOW COLUMNS, Dynamic variables, INSERT INTO.. implode? -
this form getting columns names table pracownicy
. it's dynamic because these names of applications new employee going use on computer when start working our company.
i've got form adding columns in table pracownicy
because numbers of applications employees using increasing.
this form supervisors of every department announcing new employee coming work. have problem inserting data mysql.
how can post , put insert into
data form ? numbers of variables (applications "show columns") changing every time i'm adding new application database can't use static variables.
show columns, $query = values (implode)
???
echo '<form action="formularz.php" method="post"> <table border=0 class=\"odd gradex\"> <tr bgcolor=#ffdddd> <td>imię nazwisko:</td> <td><input type="text" name="imieinazwisko"></td> </tr> <tr bgcolor=#ddddff> <td>dział:</td> <td align=center><select name="dzial"> <option value = "lcl">lcl <option value = "nvocc">nvocc <option value = "za">za <option value = "zam">zam <option value = "zlr">zlr <option value = "zr">zr <option value = "zt">zt </select></td> </tr> <tr bgcolor=#ffdddd> <td>telefon:</td> <td align=center><select name="telefon"> <option value = "stacjonarny">stacjonarny <option value = "blackberry">blackbery <option value = "blackberry + stacjonarny">blackbery + stacjonarny </select></td> </tr> <tr bgcolor=#ddddff> <td>komputer:</td> <td align=center><select name="komputer"> <option value = "laptop">laptop <option value = "laptop + iplus">laptop + iplus <option value = "stacjonarny">stacjonarny </select></td> </tr> <tr bgcolor=#ffdddd> <td> <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" /> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script> <link rel="stylesheet" href="/resources/demos/style.css" /> <script> $(function() { $( "#datepicker" ).datepicker(); }); </script> </head> <body> <p>data rozpoczęcia pracy: </td><td><input type="text" name ="data" id="datepicker"/></p></td> </tr> <tr bgcolor=#ddddff> <td>oprogramowanie:</td><td></td></tr> <tr bgcolor=#ddddff>'; $result = mysql_query("show columns pracownicy") or die(mysql_error()); while ($row = mysql_fetch_array($result)) { if($row[0] == 'id' || $row[0] == 'imieinazwisko' || $row[0] == 'dzial' || $row[0] == 'telefon' || $row[0] == 'komputer' || $row[0] == oprogramowanie' || $row[0] == 'data') continue; echo '<td bgcolor=#ddddff>'.$row[0].'<br />'; if (stripos($row[0], "uprawnienia") !== false) { echo '<td bgcolor=#ddddff><p><a class=podpowiedz href=#> <input type="text" name="'.$row[0].'"> <span>uprawnienia typu "stanowisko" lub "jak ktoś"</span></a></p> </td></tr>'; } else { echo '<td bgcolor=#ddddff align=center><select name="'.$row[0].'"> <option value = "nie">nie <option value = "tak">tak </td> </tr>'; } } //echo '</select></form>'; echo ' <tr> <td><input type="submit" name="zapisz" value="zapisz"></td> </tr> </form> </table> </form></center>';` if(isset($_post['zapisz'])) { $imieinazwisko = trim($_post['imieinazwisko']); $dzial = trim($_post['dzial']); $telefon = trim($_post['telefon']); $komputer = trim($_post['komputer']); $data = trim($_post['data']); ??? $rowsrray = trim($_post[$row[0]]); ---???? ??? $query = "insert `pracownicy` values (null , '$imieinazwisko' , '$dzial', '$telefon' , '$komputer' , '$data', ".implode(', ', $_post[$row['0']]).")"; ---???
you should think changing database structure. not idea dynamically add columns table in running database applcation.
your request not unusual one, in cases people solve problem having 1 column specifying actual application , few more put in information regarding chosen application (in form of char, integer odr date fields). has advantage of keeping table structure small , manageable being flexible @ same time. tables many (mostly unused) columns considered ugly or messy because slow down sql server unnecessarily.
so, maybe following might you:
create table applications (apid int auto_increment primary key, apname varchar(64), apdescription nvarchar(1024)) -- "pracownicy" --> users? create table users (usid int auto_increment primary key, usname varchar(64), usfirstname varchar(64), ustel varchar(64) null, uscomputer varchar(64) null) -- , linking element: app2usr create table app2usr (auaid int, auuid int, auinfo varchar(64) null, auactive int default 1, primary key(auaid,auuid))
now can have many (or few) applications per user without ever having change table layout. thing have update when new application pops applications table contains 1 line per application.
a typical query getting application information 1 user might be
select usname,usfirstname,apname,auinfo users inner join app2usr on auuid=usid inner join applications on apid=auaid wher auactive>0
when comes updating or inserting new information create (or delete) records in app2usr
accordingly. know rough description on "the right path" ...
edit:
for collecting application input fields form need find out @ first applications assigned user. maybe have used checkboxes or multiple select elements purpose?
- in first case can scan $_post array valid application names (against list should server first,
select apid,apname applications
). - in second case select element have put array of ids $_post array correspond selected applications - if have set select box in way in first place.
<select name="apps" multiple="multiple"><option value="1">application1</option>...</select>
, values ,application1
name applications table, should trick.
Comments
Post a Comment