dsjxgu4759 2015-03-02 06:26
浏览 38
已采纳

如何将选定的值从下拉列表保存到php中的mysql

<?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>";
?>

That is my code. I want to save option selected from 4 drop down list to mysql. When I submit the form, the value selected from last box are getting saved in all rows of mysql table. Now, please tell me what should I do? I am getting drop down box values from database table properly so what is the problem?

  • 写回答

2条回答 默认 最新

  • dowaw80220 2015-03-02 07:12
    关注

    this is your code.

    <?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>";
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?