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

PHP MySQL基于用户复选框将行id保存到数据库,php,html,mysql,Php,Html,Mysql,我正在尝试获取用户使用复选框检查的sql行,并将id发布到脚本,该脚本将用户选择的行保存到db,以便他们可以在以后的数据收集中提取保存的行 下面是我的代码-问题是当我发布复选框值时,它显示为1,我不确定为什么会发生这种情况。所有复选框值均显示为1 require('./wp-blog-header.php'); $current_user = wp_get_current_user(); $school = $_POST['school']; $connection = mysql_con

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

下面是我的代码-问题是当我发布复选框值时,它显示为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 />";
        }
}
?>

TBH,这件事一团糟。问题的基础是a只有一个命名元素,正如另一个答案所指出的,b试图给它一个数组作为值。但即使在修复后,这也永远不会起作用

你的数据库结果在四个独立的循环中,我不知道你的想法是什么。同样,如果你向我展示这个网页,我可以很容易地通过单击删除你的整个数据库

这是工作5分钟后的样子。我仍然不认为这是一个合理的脚本,但希望它能给你一些可以学习的东西。你需要优先了解,第一种方法是停止使用5年。因为它已经内置到PHP中近十年了。它提供了方便的方法,可以轻松地将结果集转储到数组中

<html>
<head>
<link rel="stylesheet" type="text/css" href="results.css">
</head>
</html>

<?php
require('./wp-blog-header.php');
$current_user = wp_get_current_user();
$school = $_POST['school'];
$db = new PDO("mysql:host=198.71.225.63;dbname=msl_data", "newmslsuper", "");
$stmt = $db->prepare("INSERT INTO searches (ID, school, type) VALUES(?,?,?)";
$stmt->execute(array($current_user->ID, $school, 1));

$stmt = $db->prepare("SELECT * FROM `data` WHERE `school` LIKE ?");
$stmt->execute(array("%$school%"));
// put it in an array. presto!
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (count($result) === 0) { 
    echo "Sorry your search for '$school' returned no results. Please try again.";
}
else {
    $fields = array_keys($result[0]);

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

    // assume "id" field is first
    unset($fields[0]);
    // printing table headers
    foreach($fields as $field) {
        echo "<th>$key</th>";
    }
    echo "</tr>\n";

    // printing table rows
    // just one loop
    foreach($result as $row) {
        echo "<tr>";
        // assume the column is named "id"
        echo "<td><input type='checkbox' value='$row[id]' name='cell[]'></td>";
        unset($row["id"]);
        foreach($row as $cell) {
            echo "<td>$cell</td>";
        }
        echo "</tr>\n";
    }

    echo "<input type='submit' value='SAVE'>";
    echo "</form>";
}
?>

如果选中该复选框,您将始终得到值1。您需要做的是,根据DB id值命名复选框。您不应该这样做。谢谢你的帮助,看起来好多了。现在的问题是——从Echo发布数组时;虽然$row=mysql\u fetch\u array$search{foreach$row as$rowarray,但我希望从列ID中获取每个复选框的列值,这里是结果数组[0]=>[[1]=>[],请不要将[]添加到值中,然后重试。