php - 将 html 表中的内容存储到 mysql 数据库中

标签 php mysql

我想做的就是将用户输入的值存储到数据库中。有一个表,它有许多文本框,用户可以在其中输入他的输入。单击提交按钮后,值应该存储在 mysql 表中。 我现在遇到了一些错误。我是 php 的新手,下面附上了代码和屏幕截图。

//workingcode.php
<?php
$con = mysql_connect("localhost","tom","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
//select Database

mysql_select_db("sms", $con);

$result = mysql_query("SELECT * FROM dept_info", $con) or die(mysql_error());

echo "<table border='1'>";
echo '<tr> <th>Sl No</th> <th>USN</th> <th>Name</th><th>code1</th><th>code 2</th> <th>code 3</th> <th>code 4</th> <th>code 5</th> 
                <th>code 6</th> <th>code 7</th> <th>code 8</th></tr>';

while($row = mysql_fetch_array( $result )) {
    // Print out the contents of each row into a table
    echo "<tr><td>";
    echo $row['id'];
    echo "</td><td>";
    echo $row['usn'];
    echo "</td><td>";
    echo $row['name'];
    echo "</td>";
    for ($i=1; $i <= 8; $i++) {
        echo "<td> <input type='text' name='sl[{$row['usn']}][$i]' size='2' /> </td>" ;
    }
    echo '</tr>';
}

echo '
<form action = "insert.php" method = "post">
<div align="bottom">
<input type = "submit" align = "BOTTOM" value = "Proceed">
</div>
</form>
';
?>

我已经将它链接到一个 insert.php 文件,如下所示。

  <html>
<head>
<title>Marks Entry Results</title>
</head>
<body>
<h1>Student Marks Entry Results</h1>

    <?php


        $insertData = array();
foreach ($_POST['sl'] as $usn => $codes) {
    foreach ($codes as $sl) {
        $insertData[] = sprintf("('%s', %d)", mysqli_real_escape_string($usn), intval($sl));
    }
}
$con = mysql_connect("localhost","tom","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
//select Database

mysql_select_db("sms", $con);

$query = "INSERT INTO mytable (usn, code1) VALUES\n" . join(",\n", $insertData);
$result = mysql_query($query);
if ($result) {
echo mysql_affected_rows()." Marks inserted into database.";
} else {
echo "An error has occurred. Not added.";
}
mysql_close();
?>
</body>
</html>
//The description of the table which i made for this is
/*mysql> desc mytable;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| usn   | varchar(50) | NO   | PRI | NULL    |       |
| code1 | int(2)      | YES  |     | NULL    |       |
| code2 | int(2)      | YES  |     | NULL    |       |
| code3 | int(2)      | YES  |     | NULL    |       |
| code4 | int(2)      | YES  |     | NULL    |       |
| code5 | int(2)      | YES  |     | NULL    |       |
| code6 | int(2)      | YES  |     | NULL    |       |
| code7 | int(2)      | YES  |     | NULL    |       |
| code8 | int(2)      | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
9 rows in set (0.01 sec)
*/

最佳答案

文本框不在任何表单标签内,因此当您点击提交时,文本框的值不会回发到服务器...请尝试以下操作。

echo '<form action = "insert.php" method = "post">';
while($row = mysql_fetch_array( $result )) {
    // Print out the contents of each row into a table
    echo "<tr><td>";
    echo $row['id'];
    echo "</td><td>";
    echo $row['usn'];
    echo "</td><td>";
    echo $row['name'];
    echo "</td>";
    for ($i=1; $i <= 8; $i++) {
        echo "<td> <input type='text' name='sl[{$row['usn']}][$i]' size='2' /> </td>" ;
    }
    echo '</tr>';
}

echo '
<div align="bottom">
<input type = "submit" align = "BOTTOM" value = "Proceed">
</div>
</form>';




  //insert.php

<?php




    $insertData = array();
    foreach ($_POST['sl'] as $usn => $codes) {
        foreach ($codes as $sl) {
            $insertData[] = sprintf("('%s', %d)", mysqli_real_escape_string($usn), intval($sl));
        }
    }

    $con = mysql_connect("localhost","tom","");
    if (!$con) {
       die('Could not connect: ' . mysql_error());
    }
    //select Database

    mysql_select_db("sms", $con);

    $query = "INSERT INTO mytable (usn, code1) VALUES\n" . join(",\n", $insertData);
    $result = mysql_query($query);
    if ($result) {
       echo mysql_affected_rows()." Marks inserted into database.";
    } else {
    echo "An error has occurred. Not added.";
    }
    mysql_close();
    ?>

关于php - 将 html 表中的内容存储到 mysql 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15916319/

相关文章:

c# - 尝试在 C# 中重现 PHP 的 pack ("H*") 函数

javascript - 发布依赖于返回 true 的 javascript 函数的表单

php - yelp 等网站如何转换位置输入(邮政编码、地区名称、县)并使用它进行搜索?

MySQL ORDER BY 多列 ASC 和 DESC

php - wordpress 帖子摘录问题

php - PHP数组输出仅显示一个元素

php - 使用友好的 URL 提交 CakePHP 表单

mysql - 当我们使用嵌套查询 for max(cgpa) 时,我们可以使用 select * 吗

mysql分区: double enum partitioning key

php - 如何在一个查询中更新两个表?