0

MySQL データベースを更新できる小さなスクリプトがあります。最初にテーブルのすべてのエントリを表示し、それをフォームに入れます。次に、すべての変更をデータベースに更新する必要があります。しかし、残念ながらスクリプトは機能しません。誰でも私を助けてもらえますか?

これはファイルです: updateform.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head> 
<style type="text/css">  

body { 
    margin:50px 0px; padding:0px; 
    text-align:center; 
        font:13px Tahoma,Geneva,sans-serif 
    } 

#content { 
    width:1000px; 
    margin:0px auto; 
    text-align:left; 
    padding:15px; 
    border:1px dashed #333; 
    background-color:#eee; 
    } 
</style> 

</head> 
<body> 
<div id='content'><h3><center>Update Information</center></h3> 
<?php 
// connect to the database 
mysql_connect(host,dbuser,passwd); 

// select the database 
mysql_select_db(db) or die("Unable to select database"); 

// run the query and put the results in an array variable called $result 
$result = mysql_query("SELECT * FROM program ORDER BY 'id'"); 
// find out how many records there are to update 
$size = count($_POST['id']); 

// start a counter in order to number the input fields for each record 
$i = 0; 
print "<table width='100%' border='0' cellspacing='1' cellpadding='0'><tr><td>"; 
// open a form 
print "<form name='id' method='post' action='update.php'> 
<table width='100%' border='0' cellspacing='1' cellpadding='1'><tr> 

<td align='center'><strong>ID</strong></td> 
<td align='center'><strong>Vorname</strong></td> 
<td align='center'><strong>Nachname</strong></td> 
<td align='center'><strong>Punkte</strong></td> 
</tr>\n"; 

// start a loop to print all of the courses with their book information 
// the mysql_fetch_array function puts each record into an array. each time it is called, it moves the array counter up until there are no more records left 
while ($Update = mysql_fetch_array($result)) { 
  print "<tr>\n"; 
// assuming you have three important columns (the index (id), the course name (course), and the book info (bookinfo)) 
  // start displaying the info; the most important part is to make the name an array (notice bookinfo[$i]) 
  print "<td align='center'><p>{$Update['id']}</p></td>\n"; 

  print "<td align='center'><input type='text' size='40' name='vorname[$i]' value='{$Update['vorname']}' /></td>\n"; 

  print "<td align='center'><input type='text' size='40' name='nachname[$i]' value='{$Update['nachname']}' /></td>\n"; 

  print "<td align='center'><input type='text' size='40' name='punkte[$i]' value='{$Update['punkte']}' /></td>\n"; 

  print "</tr>\n"; 

// add 1 to the count, close the loop, close the form, and the mysql connection 
++$i; 
} 
print "<tr> 
<td colspan='4' align='center'><input type='submit' value='submit' />"; 
print "</td> 
</tr> 
</table> 
</td> 
</tr> 
</form> 
</table>"; 
mysql_close(); 
?><br /><br /><div></body> 
</html>

これはファイルです: update.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head> 
<style type="text/css">  

body { 
    margin:50px 0px; padding:0px; 
    text-align:center; 
        font:13px Tahoma,Geneva,sans-serif 
    } 

#content { 
    width:1000px; 
    margin:0px auto; 
    text-align:left; 
    padding:15px; 
    border:1px dashed #333; 
    background-color:#eee; 
    } 
</style> 

</head> 
<body> 
<div id='content'><h3><center>Success! </center></h3> 
<table width='100%' border='0' cellspacing='1' cellpadding='0'><tr><td> 


<table width='100%' border='0' cellspacing='1' cellpadding='1'> 
<tr> 

<td align='center'><strong>Vorname</strong></td> 
<td align='center'><strong>Nachname</strong></td> 
<td align='center'><strong>Punkte</strong></td> 
<td align='center'><strong>ID</strong></td> 
</tr> 
<?php 
// connect to the database and select the correct database 
mysql_connect(host,dbuser,passwd); 
mysql_select_db(db) or die("Unable to select database"); 

// find out how many records there are to update 
$size = count($_POST['id']); 

// start a loop in order to update each record 
$i = 0; 
while ($i < $size) { 
// define each variable 
$vorname = $_POST['vorname'][$i]; 
$nachname = $_POST['nachname'][$i]; 
$punkte = $_POST['punkte'][$i]; 
$id = $_POST['id'][$i]; 

// do the update and print out some info just to provide some visual feedback 

$query = "UPDATE `program` SET `vorname` = '$vorname', `nachname` = '$nachname', `punkte` = '$punkte' WHERE `id` = '$id' LIMIT 1"; 
mysql_query($query) or die ("Error in query: $query"); 
print " 

</tr> 
<td align='left'><p>$vorname</p></td> 
<td align='left'>$nachname</td> 
<td align='left'>$punkte</td> 
<td align='left'>$id</td> 
</tr> 
"; 
++$i; 
} 
mysql_close(); 
?> 
<tr> 
<td colspan='4' align='center'> 
</td> 
</tr> 
</table> 
</td> 
</tr> 

</table></div></body></html>

誰でも私を助けることができますか?アレックスに挨拶

4

2 に答える 2

0

問題は、レコードの ID を適切に追跡していないことです。

ID に従ってフォームのテキスト入力に名前を付けてみてください。

例えば ​​:

print "<td align='center'><input type='text' size='40' name='vorname_".$Update['id']."' value='".$Update['vorname']."' /></td>\n";

その後、explode、strval、または正規表現関数を使用して id を取り除くことができます。

于 2013-05-30T23:11:50.387 に答える