【问题标题】:Store content from an html table to a mysql database将内容从 html 表存储到 mysql 数据库
【发布时间】:2013-04-10 03:18:37
【问题描述】:

我要做的就是将用户输入的值存储到数据库中。有一个表,它有许多文本框,用户可以在其中输入他的输入。单击提交按钮后,值应该存储在 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)
*/

【问题讨论】:

  • 请发布您遇到的错误,以便我们帮助解决问题。
  • 我收到以下错误。学生分数输入结果 出现错误。未添加。”
  • 为什么在一个页面上使用 mysql 而在另一个页面上使用 mysqli?这是你的代码吗?
  • 第一个是我的..第二个是从教程中获取的..我对 php 完全陌生..抱歉这个错误

标签: php mysql


【解决方案1】:

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

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();
    ?>

【讨论】:

  • 谢谢您的帮助先生。我按照您的建议编辑了我的代码,但我只得到学生分数输入结果和空白页..我感觉我的 insert.php 代码有问题跨度>
  • PHP.ini 中是否启用了错误显示?如果不启用它。你能告诉我抛出错误的最后一条语句吗?
  • 我已经在 /etc/php5/apache2/php.ini 中的 php.ini 文件中打开了错误显示。我得到的唯一输出是直到这一行

    Student Marks条目结果

    下方不显示任何内容。
  • 发布 print_r($_POST); 的结果
  • 你正在使用 $db->query($query);在您的上下文中这是错误的,请使用 mysql_query($query);在您有一个单独的数据库类时,您可以创建它的一个对象并像这样调用它 $db->query($query);其他 $db-> 引用也是如此...
猜你喜欢
  • 1970-01-01
  • 2011-02-08
  • 1970-01-01
  • 2017-07-15
  • 2013-01-14
  • 1970-01-01
  • 2018-10-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多