繁体   English   中英

用PHP更新MySQL表

[英]Update MySQL table with PHP

我有一个小脚本,可以用来更新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>

有人可以帮我吗? 问候亚历克斯

更改

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";

问题是您没有正确跟踪记录的ID。

尝试根据ID命名表单文本输入。

例如 :

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

之后,您可以使用explode,strval或regex函数删除id。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM