dougu8742 2015-07-30 02:31
浏览 50
已采纳

PHP / MySQL问题插入数组

I am trying to insert data into a MySQL table and one of the columns is inserting 'Array' instead of the array values. What am I missing, please? Thanks.

 id         studentname     title   academicdiscipline  priority 
 012345678  TEST, NAME      Array   Civil                  3
 012345678  TEST, NAME      Array   Civil                  1
 012345678  TEST, NAME      Array   Civil                  4
 012345678  TEST, NAME      Array   Civil                  5
 012345678  TEST, NAME      Array   Civil                  2 

Here is how I insert the data into the database - which works just fine apart from the 'title' column:

if(isset($_POST['submit'])) { 

$id = $_POST["id"]; 
$studentname = $_POST["studentname"]; 
$title = $_POST["title"]; 
$academicdiscipline = $_POST["academicdiscipline"];
$priority = $_POST["priority"];  

$array = array(); 
foreach ($priority as $priority)  {
    if ($priority >=1) {
        $array[] = "('$id', '$studentname', '$title', '$academicdiscipline', '$priority')"; } 

        $query = "INSERT INTO flux_project_selection (id, studentname, title, academicdiscipline, priority) VALUES" .implode(',', $array); }  
        $retval = mysql_query($query) or die(mysql_error()); } 

EDIT: Here is the HTML for the form:

        <table width="890" cellspacing="0"> 
        <thead><tr>
            <th></th>

            <th width="250"> Title </th>
            <th width="550"> Description </th>
            <th width"90"> Field </th>
            <th width="50"> Select </th>
           </tr></thead>   
    <?php
            $color1 = "#E9E9E9";    
            $color2 = "#FFFFFF"; 
            $row_count = 0; 

            echo "<tr>
            <td valign = \"top\" bgcolor=\"$row_color\">
                <input type=\"hidden\" name=\"id[]\" value=\"$id\">
                <input type=\"hidden\" name=\"studentname[]\" value=\"$studentname\"></td> 

            <td valign = \"top\" bgcolor=\"$row_color\">
                <input type=\"hidden\" name=\"title[]\" value=\"$title\"> $title </td>  

            <td valign = \"top\" bgcolor=\"$row_color\">
                <input type=\"hidden\" name=\"description[]\" value=\"$description\"> $description </td> 

            <td valign = \"top\" bgcolor=\"$row_color\"> 
                <input type=\"hidden\" name=\"academicdiscipline[]\" value=\"$academicdiscipline\"> $academicdiscipline </td>

            <td valign = \"top\" bgcolor=\"$row_color\"> 
            <select name=\"priority[]\" class=\"priority\">
                          <option>  </option>
                          <option value=\"1\"> 1 </option>
                          <option value=\"2\"> 2 </option>
                          <option value=\"3\"> 3 </option> 
                          <option value=\"4\"> 4 </option>
                          <option value=\"5\"> 5 </option>
                          <option value=\"6\"> 6 </option>
                          <option value=\"7\"> 7 </option>
                          <option value=\"8\"> 8 </option>
                          <option value=\"9\"> 9 </option>
                          <option value=\"10\"> 10 </option>
                        </select>   

         </td></tr>
         <tr bgcolor=\"#ffffff\"> </tr>
         <tr bgcolor=\"#902C2E\"> </tr>"; 

      $row_count++;  

 echo"<tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td>&nbsp;</td>
  <td ><input name=\"reset\" type=\"reset\" value=\"Clear selection\"/> 
       <input name=\"submit\" type=\"submit\" value=\"Submit selection\"/>  
</tr></table>";  
?>
  • 写回答

3条回答 默认 最新

  • doubi7306 2015-08-07 03:22
    关注

    Finally got this:

    if(isset($_POST['submit']) && !empty($_POST['submit'])) {
    $ids = $_POST['id']; 
    $names = $_POST['studentname']; 
    $titles = $_POST['title'];  
    $disciplines = $_POST['academicdiscipline']; 
    $priorities = $_POST['priority']; 
    
                    foreach($priorities as $key => $priority) { 
                        if ($priority > 0) { 
    
                        $query = "INSERT INTO flux_project_selection (id, studentname, title, academicdiscipline, priority) 
                                    VALUES ( " . mysql_real_escape_string($ids[$key]) . ",
                                            '" . mysql_real_escape_string($names[$key]) . "',
                                            '" . mysql_real_escape_string($titles[$key]) . "',
                                            '" . mysql_real_escape_string($disciplines[$key]) . "',
                                            " . mysql_real_escape_string($priority) . "  )";
    
                        $retval = mysql_query($query) or die(mysql_error()); 
                            } 
                        } 
    
                        echo "Worked. <br />"; 
            } 
    

    Thanks everyone.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题