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


PHP MySQL save a row id to a database based on user checkbox

我正试图获取用户用复选框检查的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 />";
        }
}
?>

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

你把数据库结果放在四个独立的循环中,我不知道当时的想法是什么。同样,如果你给我这个网页,我可以很容易地删除你的整个数据库,只需点击一下。

这是工作5分钟后的样子。我仍然不认为这是一个合理的剧本,但希望它能给你一些借鉴。您需要优先学习如何防止SQL注入,而做到这一点的第一种方法是停止使用5年来一直不受支持的数据库引擎。PDO是最简单的替代方案,因为它已经在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>";
}
?>