2015-03-02 110 views
0
<?php 

    $status=&$_POST['status']; 
    $from=&$_POST['date_from']; 
    $to=&$_POST['date_to']; 
    $conn=mysqli_connect('localhost','root','','punbus') or die("Database not connected".mysqli_error()); 

    if(isset($_POST['sub'])){ 
    $ins="insert into driver_status(driver_name,status,date_from,date_to)      
      select Driver_name,'$status','$from','$to' from driver_master";  
    if(mysqli_query($conn,$ins)){ 
     echo "added"; 
    } 
    else{ 
     echo "NOT".mysqli_error($conn); 
    } 
    } 

    $sel='select Driver_name from driver_master'; 
    $query=mysqli_query($conn,$sel); 

    echo "<form action='driver_status.php' method='post'>"; 
    echo "<table cellpadding=5>"; 
    echo "<tr>"; 
    echo "<th>Driver Name</th>"; 
    echo "<th>Status</th>"; 
    echo "<th>From</th>"; 
    echo "<th>To</th>"; 
    echo "</tr>"; 

    while($row=mysqli_fetch_assoc($query)){  
    echo "<tr>"; 
    echo "<td>".$row['Driver_name']."</td>"; 
    $sel1='select d_status from status'; 
    $query1=mysqli_query($conn,$sel1); 
?> 

    <td> 
    <select name="status"> 
     <?php 
     while($row1=mysqli_fetch_assoc($query1)){ 
      $st=$row1['d_status']; 
      echo "<option value='$st'>$st</option>"; 
     } 
     ?> 
    </select> 
    </td> 
    <?php 
    echo "</tr>"; 

    } 

    echo "</table>"; 
    echo '<input type="submit" name="sub" value="Update"/>'; 
    echo "</form>"; 
?> 

這是我的代碼。我想將從4下拉列表中選擇的選項保存到mysql。當我提交表單時,從最後一個框中選擇的值將被保存在mysql表的所有行中。現在,請告訴我該怎麼辦? 我正在從數據庫表中正確地下拉框值,所以問題是什麼?如何在php中將選定值從下拉列表中保存到mysql中

回答

0

這是你的代碼。

<?php 
$status = $_POST['status']; 
$driver_name= $_POST['driver_name']; 
$from = $_POST['date_from']; 
$to = $_POST['date_to']; 
$conn = mysqli_connect('localhost', 'root', '', 'punbus') or 
     die("Database not connected" . mysqli_error()); 
if(isset($_POST['sub'])) { 
    foreach($status as $k=>$s){ 
     $ins = "insert into driver_status(driver_name,status,date_from,date_to) VALUES     
      ('".$driver_name[$k]."','$s','$from','$to')"; 
     if (mysqli_query($conn, $ins)) { 
      echo "added"; 
     } else { 
      echo "NOT" . mysqli_error($conn); 
     } 
    } 
} 

$sel = 'select Driver_name from driver_master'; 
$query = mysqli_query($conn, $sel); 

echo "<form action='driver_status.php' method='post'>"; 
echo "<table cellpadding=5>"; 
echo "<tr>"; 
echo "<th>Driver Name</th>"; 
echo "<th>Status</th>"; 
echo "<th>From</th>"; 
echo "<th>To</th>"; 
echo "</tr>"; 
while($row=mysqli_fetch_assoc($query)){  
    echo "<tr><td>".$row['Driver_name'] 
      ."<input type=\"hidden\" name=\"driver_name[]\" value=\"".$row['Driver_name']."\"/></td>"; 
    $sel1='select d_status from status'; 
    $query1=mysqli_query($conn,$sel1); 
    echo "<td><select name=\"status[]\">"; 
    while($row1=mysqli_fetch_assoc($query1)){ 
      echo "<option value=\"".$row1['d_status']."\">".$row1['d_status']."</option>"; 
    } 
    echo "</select></td></tr>"; 
} 
echo "</table>"; 
echo '<input type="submit" name="sub" value="Update"/>'; 
echo "</form>"; 
?> 
+0

它也不起作用。 status_ $麻木是正確的選擇標籤? – 2015-03-02 07:22:30

+0

'status_ $ numb'不是標記,它是選擇名稱屬性的值..它將在您的選擇中給出名稱'select_1,select_2,select_3,select_4'。並在你的PHP中,你可以通過'$ _POST ['select_1'] ..etc'來訪問這些值。所以你的4選擇不具有相同的名稱,但名稱不同。 – check 2015-03-02 07:31:00

+0

我已經試過這個,但它不起作用 – 2015-03-02 08:27:50

0

這與其他文本框相似 $ op =「select * from client where active = 0」; $ op1 = mysql_query($ op);

    echo '<select name="c" id="c" style="width:160px;" required>'; 

         while ($row = mysql_fetch_array($op1)) 
         { 
         echo $s=$row["c_name"]; 
          if($company!=$s) 
          { 
          echo '<option value="'.$row["c_name"].'">'.$row["c_name"].'</option>'; 
          } 

         } 

      $c=$_REQUEST['c']; 
      $sql="insert into project c_name) values('$c')"; 
    $sql1=mysql_query($sql); 
+0

我已經使用$狀態。 – 2015-03-02 06:34:40

+0

你的表單中有四個選擇框嗎? – user3386779 2015-03-02 06:36:12

+0

是的,有四個盒子 – 2015-03-02 06:37:13

相關問題