2013-04-10 29 views
2

我想要做的就是將用戶輸入的值存儲到數據庫中。有一個表格,它有很多文本框,用戶輸入他的輸入。點擊提交按鈕後值應該存儲在mysql表中。 我現在正在收到一些錯誤。我對PHP完全陌生,下面我附上了代碼和截圖。將內容從一個html表格存儲到一個mysql數據庫

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

請發佈您收到的錯誤,以便我們可以幫助解決問題。 – 2013-04-10 03:20:27

+0

我收到以下錯誤。學生標記輸入結果發生錯誤。 「 – 2013-04-10 03:43:49

+0

爲什麼在一個頁面上使用mysql和在另一個頁面上使用mysql?這是你的代碼嗎? – 2013-04-10 03:58:18

回答

0

的文本框都沒有任何形式的標記,以便裏面,當你點擊提交文本框的值不獲取回發給服務器...嘗試以下。

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

感謝您的幫助先生..我按照您的建議編輯了我的代碼,但我只獲得了學生標記輸入結果和空白頁..我的insert.php代碼錯誤 – 2013-04-10 04:16:44

+0

您的錯誤顯示在PHP中啓用。 ini?如果不啓用它也可以告訴我錯誤拋出後的最後一條語句嗎? – 2013-04-10 04:20:14

+0

我已經在/etc/php5/apache2/php.ini中的php.ini文件中顯示錯誤。 。我得到的唯一輸出是直到這一行

學生標記輸入結果

低於此值不會顯示 – 2013-04-10 04:27:14

相關問題