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

1

改变

print "<td align='center'><p>{$Update['id']}</p></td>\n";

print "<td align='center'>
   <p>{$Update['id']}</p>
   <input type=\"hidden\" name=\"id[$i]\" value=\"{$Update['id']}\" />
</td>\n";
于 2013-05-30T22:58:22.683 回答
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 回答