2013-05-30 110 views
0

我有一个小脚本,它允许我更新我的MySQL数据库。 它首先显示我的表格中的所有条目,并将其放入表单中。 然后它应该将所有更改更新回数据库。 但不幸的是脚本不起作用。 任何人都可以帮我吗?用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> 

请谁能帮助我? 电贺亚历

+2

什么似乎出错?你有错误吗? – showdev

+1

查看使用mysqli_ *函数,因为mysql_ *函数已被弃用。 – wazy

+0

哦,对不起,忘了那个..:D 不,一切顺利。我可以看到数据库中的所有数据。但是当我“提交”时,我只能看到“成功”作为标题和以下,有数据库的标题头条。 (Vorname,Nachname,Punkte,ID)如果我检查数据库,没有任何改变。 – linx

回答

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

试过了。 发生错误.. 解析错误:语法错误,在/../../../../updateform.php意想不到T_STRING上线56 这是此行: linx

+0

对不起,我忘记了在\之前加\,看看我编辑的答案 – lexmihaylov

+0

好像看起来这样错误是固定的,但它创建了一个新的.. 解析错误:语法错误,意外的T_PRINT在/../../../../updateform.php上线60 这是这行: print“ \ n“; – linx

0

的问题是,你是不是正确跟踪你的记录的ID。

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

例如:

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

然后才能剥离的ID与爆炸,strval或一个正则表达式的功能。

+0

谢谢。 我已经知道了! – linx