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条)

报告相同问题?

悬赏问题

  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)