2011-09-26 41 views
0

我有一個html頁面,它從'hs_hr_employee'表中獲取數據並在網頁上的表中放置信息。然後我有另一個表「權利」,它從'hs_hr_employee'表的4列中獲取信息並將它們存儲在列中。除了這4個之外,'權限'表還有一個額外的'權限'列。讓ComboBox變量存儲在MySQL中

現在,我有一個有4個選項的組合框。當我點擊「保存」按鈕時,我想將選擇的值存儲在組合框中,並將其保存在與用戶相關的「權限」表中。

(每個用戶在它旁邊都有一個組合框)。

更新代碼:

 <?php 

$connection = mysql_connect('localhost','admin','root'); 
if(isset($_POST['submit'])) 
{ 
    if(isset($_POST['cb_permissions']) && is_array($_POST['cb_permissions'])) 
    { 
     foreach($_POST['cb_permissions'] as $emp_number => $permission) 
     { 
      $sql = "UPDATE `your_permission_table` SET permission='".mysql_real_escape_string($permission)."' WHERE emp_number='".mysql_real_escape_string($emp_number)."'"; 
      echo __LINE__.": sql: {$sql}\n"; 
      mysql_query($sql); 
     } 
    } 
} 
?> 
<p style="text-align: center;"> 
    <span style="font-size:36px;"><strong><span style="font-family: trebuchet ms,helvetica,sans-serif;"><span style="color: rgb(0, 128, 128);">File Database - Administration Panel</span></span></strong></span></p> 
<p style="text-align: center;"> 
    &nbsp;</p> 

<head> 
<style type="text/css"> 
table, td, th 
{ 
border:1px solid #666; 
font-style:Calibri; 
} 
th 
{ 
background-color:#666; 
color:white; 
font-style:Calibri; 
} 
</style> 
</head> 

    <form method="post" action="admin.php"> 

    <?php 


     if (!$connection) 
      { 
      die('Could not connect: ' . mysql_error()); 
      } 

     mysql_select_db('users', $connection); 

     $result = mysql_query("SELECT emp_number, employee_id, emp_lastname, emp_firstname FROM hs_hr_employee"); 

     echo "<center>"; 

     echo "<table > 
     <tr> 
     <th>Employee Number</th> 
     <th>Employee ID</th> 
     <th>Surname</th> 
     <th>Name</th> 
     <th>Permissions</th> 
     </tr>"; 

     while($row = mysql_fetch_array($result)) 
      { 
      echo "<tr>"; 
      echo "<td>" . $row['emp_number'] . "</td>"; 
      echo "<td>" . $row['employee_id'] . "</td>"; 
      echo "<td>" . $row['emp_lastname'] . "</td>"; 
      echo "<td>" . $row['emp_firstname'] . "</td>"; 
      echo "<td> <select name='cb_permissions['".$row['emp_number']."'><option value='all'>All</option> <option value='remote'>Remote Gaming</option> <option value='landbased'>Landbased Gaming</option> <option value='general'>General Gaming</option> </select> </td>"; 
      echo "</tr>" ; 

      } 

     echo "</table>"; 

     echo "</center>"; 

     echo mysql_query('INSERT into rights(Emp_num, ID, Name, Surname) SELECT emp_number, employee_id, emp_firstname, emp_lastname FROM hs_hr_employee'); 

     $_POST['cb_permissions']; 

     mysql_close($connection); 

    ?> 

<p style="text-align: center;"> 
    &nbsp;</p> 
<p style="text-align: center;"> 
    &nbsp;</p> 

<p style="text-align: right;"> 
    <input name="Save_Btn" type="button" value="Save" /> 


    </p> 

</form> 

我如何能做到這一點任何幫助嗎?

截圖得到什麼,我正在做一個基本思想: enter image description here

回答

0

首先,你應該在你的文檔的最頂端移動連接代碼:

$connection = mysql_connect('localhost','admin','root'); 
if (!$connection) 
{ 
    die('Could not connect: ' . mysql_error()); 
} 

mysql_select_db('users', $connection); 

接下來,必須包裝內標籤的表:

<form method="post" action="target_url.php> 
    <table> 
    ... 
    </table> 
    <input type="submit" name="submit" value="Save"/> 
</form> 

之後,你將存儲employee_idemp_number(這取決於表密鑰將用於安裝許可)表單上的某處:

while($row = mysql_fetch_array($result)) 
{ 
?> 
<tr> 
    <td><?php echo $row['emp_number']; ?></td> 
    <td><?php echo $row['employee_id']; ?></td> 
    <td><?php echo $row['emp_lastname']; ?></td> 
    <td><?php echo $row['emp_firstname']; ?></td> 
    <td><select name="cb_permissions['<?php echo $row['emp_number']; ?>']"> 
     <option value='all'>All</option> 
     <option value='remote'>Remote Gaming</option> 
     <option value='landbased'>Landbased Gaming</option> 
     <option value='general'>General Gaming</option> 
    </select></td> 
</tr> 
<?php 
} 

然後,在你的target_url.php,你將不得不做的事:

If target_url.php is the same as your form, then code below should be placed at the very top of your document.

<?php 
if(isset($_POST['submit'])) 
{ 
    if(isset($_POST['cb_permissions']) && is_array($_POST['cb_permissions'])) 
    { 
     foreach($_POST['cb_permissions'] as $emp_number => $permission) 
     { 
      $sql = "UPDATE `your_permission_table` SET permission='".mysql_real_escape_string($permission)."' WHERE emp_number='".mysql_real_escape_string($emp_number)."'"; 
      echo __LINE__.": sql: {$sql}\n"; 
      mysql_query($sql); 
     } 
    } 
} 
?> 

這就是它。

+0

我在第68行得到這個錯誤:'echo'​​「;' – Brian

+0

錯誤:'解析錯誤:語法錯誤,意外的T_ENCAPSED_AND_WHITESPACE,期望T_STRING或T_VARIABLE或T_NUM_STRING在第68行的C:\ xampp \ htdocs \ admin.php上。 – Brian

+0

應該是: 「​​」; – ariefbayu

0

我建議:

  1. 使用require_once( 「db_connect.php」)< - 在這個文件使連接
  2. 使用Smarty的& html_options顯示此下拉。

1次你會學習這個,下次使用。代碼將開始組織... 也許複雜的初學者。但這是正確的方式。