将内容从html表存储到mysql数据库 [英] Store content from an html table to a mysql database

查看:76
本文介绍了将内容从html表存储到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();
    ?>

All i want to do is to store the user entered values to a database.There is a table and it has many text boxes where the user enters his input.Upon clicking submit button the values should be stored in the mysql table. I'm getting some errors right now.I'm completely new to php,Below I've attached the code and screenshots.

//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>
';
?>

I've linked this to an insert.php file as shown below..

  <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)
*/

解决方案

The textboxes are not inside any form tag so when you hit submit the value of textboxes doesn't get post back to server...Try following.

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

这篇关于将内容从html表存储到mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆