PHP MySQL根据用户复选框将行id保存到数据库

标签 php html mysql

我正在尝试获取用户使用复选框检查的 sql 行,并将 id 发布到脚本,该脚本将用户选定的行保存到数据库,以便他们可以在以后的数据中提取“保存的”行。

下面是我的代码——问题是当我发布复选框值时它显示为“1”,我不确定为什么会发生这种情况。所有复选框值均显示为“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>\n";

    // 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>\n";
            }
    }
}

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

mysql_close(); //Make sure to close out the database connection

最佳答案

您的复选框应该是数组,因为它们有多个。为什么你把它们全部设为 1,因为它们互相覆盖。

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

关于PHP MySQL根据用户复选框将行id保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34405202/

相关文章:

php - 当用户使用 Google Oauth2 和 people.me 进行身份验证时如何获取电子邮件地址

c# - 在 C# 中从 Ajax 获取值(value)

php - MySQL更新或插入具有多个查询的整数值以同时更改它

HTML 中的 JavaScript : Getting undefined input value?

html - How to use AngularJS to get JSON from external URL and display in page - Resource interpreted as Script 但使用 MIME 类型 text/html 传输

mysql - 如何防止 selectfound_rows 在竞争条件下中断?

php - API 使用限制

PHP查看字符串中的特殊字符

PHP分页计算

php - 如何从传递和接收参数的 PHP 内部运行 Ruby/Python 脚本?