douxing6434 2015-12-21 22:12
浏览 76
已采纳

PHP MySQL基于用户复选框将行ID保存到数据库

I am attempting to get the sql row that a user checks with a checkbox and post the id to a script that will save the users selected rows to a db so they can pull "saved" rows at a later data.

Below is my code -- the issue is when I post the checkbox value it is appearing as "1" and I am not sure why this is happening. All checkbox values are appearing as "1".

require('./wp-blog-header.php');

$current_user = wp_get_current_user();

$school = $_POST['school'];

$connection = mysql_connect('198.71.225.63:3306', 'newmslsuper', ''); 
mysql_select_db('msl_data');

$query = "INSERT INTO searches (ID, school, type) VALUES('$current_user->ID', '$school', '1')";

mysql_query($query);

$search = mysql_query("SELECT * FROM `data` WHERE `school` LIKE '%$school%'");

$count=mysql_num_rows($search);
if ($count==0) { 
    echo 'Sorry your search for'; echo " $school "; echo 'returned no results. Please try again.'; 
}
else {
    $fields_num1 = mysql_num_fields($search);

    echo "<form action='save.php' method='post'>";
    echo "<p>Check the box next to a Scholarship you would like to save and hit the SAVE button.<p/><table><tr><th>Save Search</th>";

    // printing table headers
    for($i=0; $i<$fields_num1; $i++)
    {
        $field1 = mysql_fetch_field($search);
        echo "<th>{$field1->name}</th>";
    }
    echo "</tr>
";

    // printing table rows

    while($row = mysql_fetch_array($search)){
        foreach($row as $rowarray)
            while($row1 = mysql_fetch_row($search)){
                echo "<tr>";
                echo "<td><input type='checkbox' value='$rowarray' name='cell'></td>";
                // $row is array... foreach( .. ) puts every element
                // of $row1 to $cell1 variable
                foreach($row1 as $cell1)
                    echo "<td>$cell1</td>";
                echo "</tr>
";
            }
    }
}

echo "<input type='submit' value='SAVE'>";

mysql_close(); //Make sure to close out the database connection
  • 写回答

2条回答 默认 最新

  • dongxiequ3724 2015-12-21 22:17
    关注

    Your checkboxes should be as array as they are multiple. The reason why you get them all as 1 as they override each other.

    <form method='post' id='form' action='page.php'> 
    
        <input type='checkbox' name='checkboxvar[]' value='Option One'>1
        <input type='checkbox' name='checkboxvar[]' value='Option Two'>2
        <input type='checkbox' name='checkboxvar[]' value='Option Three'>3
        <input type='submit'> 
    </form>
    
    
        <?php
     if(isset($_POST['submit']){
       $v = $_POST['checkboxvar'];
    
       foreach ($v as $key=>$value) {
                 echo "Checkbox: ".$value."<br />";
            }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器