我有一个包含两列的简单数据库--id和file_name。在file_name列中,我有mp3文件名,例如Summer of 69.mp3。现在我想用UPDATE语句重命名10个文件名。但我试过的只是更新最后一个文件。
$dbc = mysqli_connect ('host','user','pwd','database')
or die ('Error connecting to database');
$query = "SELECT * FROM files ORDER BY id DESC LIMIT 10";
$result = mysqli_query ($dbc,$query)
or die ('Error Came');
echo '<form action=rename.php method=post>';
while($row = mysqli_fetch_array($result)) {
$filename = $row['file_name'];
$fid = $row['id'];
echo '<input type="text" name="f[]" value="'.$row['file_name'].'" size="50"><br/><br/>';
}
echo '<input name="submit" type="submit" value="update"></form>';
if(isset($_POST['submit'])) {
foreach ($_POST['f'] as $f ){
$query = "UPDATE files SET file_name ='$f' WHERE id='$fid'";
mysqli_query ($dbc,$query)
or die ('Error Came');
}
}
mysqli_close($dbc);
有什么方法可以解决这个问题吗?
答案 0 :(得分:0)
if(isset($_POST['submit'])) {
$f=$_POST['f'];
foreach ($f as $key=>$value ){
$query = "UPDATE files SET file_name ='$value' WHERE id=$row[$key]";
mysqli_query ($dbc,$query)
or die ('Error Came');
}
}
这可能适合你
答案 1 :(得分:0)
只有最后一个文件正在更新,因为$ fid包含最后一个文件的ID。
$i=0;
while($row = mysqli_fetch_array($result)) {
if($i==0)
{$fid = $row['id'];$i++;}//now $fid contains the first file's id
$filename = $row['file_name'];
echo '<input type="text" name="f[]" value="'.$row['file_name'].'" size="50"><br/><br/>';
}
现在在另一部分
if(isset($_POST['submit'])) {
foreach ($_POST['f'] as $f ){
$query = "UPDATE files SET file_name ='$f' WHERE id='$fid'";
mysqli_query ($dbc,$query)
or die ('Error Came');
$fid++;
}
}