【发布时间】:2018-10-15 01:29:25
【问题描述】:
我拥有的是一个基于 html 的网站,其中包含数据库中的行。
我想要什么: 我希望它用 1 个按钮更新字段,我现在遇到的问题是它不会更新正确的行。它将更新数据库中的第一个。
我希望它只更新已编辑的内容(例如 10 个中的 1 个或 2 个),其余的保持不变。
$PAGES = $app->get_pages();
foreach($PAGES as $Pages){
echo "
<tr>
<td>".$Pages['id']."</td>
<form action='' method='post'>
<td><input type='text' class='form-control profiel' name='PageName' value='".$Pages['naam']."'</td>
<td><input type='text' class='form-control profiel' name='PageUrl' value='".$Pages['url']."'</td>
<td><input type='text' class='form-control profiel' name='PageStatus' value='".$Pages['status']."'</td>
<td><input type='text' class='form-control profiel' name='PagePos' value='".$Pages['positie']."'</td>
<input type='hidden' name='pageID' value='".$Pages['id']."'>
<td><button type='submit' name='deletePage'><i class='fa fa-trash'></i></button></td>
</tr>
";
}
echo "
</tbody></table>
<button type='submit' class='btn btn-danger' name='updatePage'>Bewerk</button>
</form>
if(isset($_POST['updatePage'])){
unset($updateVAL);
$updateVAL['naam'] = $app->check_string($_POST['PageName']);
$updateVAL['url'] = $app->check_string($_POST['PageUrl']);
$updateVAL['status'] = $app->check_string($_POST['PageStatus']);
$updateVAL['positie'] = $app->check_string($_POST['PagePos']);
$app->update_tabel('pages', $updateVAL);
}
更新功能:
public function update_tabel($tabel, $updateVAL){
$q = '';
foreach ($updateVAL as $key => $value) {
$value = str_replace("'","'",$value);
$q .= "`".$key."` = '".$value."',";
}
if(isset($q) && ($q !='')) {
$q = substr($q,0,-1);
$this->database->query("UPDATE ".$tabel." SET ".$q." LIMIT 1");
$this->database->execute();
}
}
示例
例如,如果我编辑“支持”并将其设为“支持tt”,它将在数据库中编辑它但是它将“主页”设置为“支持”并且不会更改“支持” ' 到 'Suporttt' 所以它更新不正确的记录
【问题讨论】:
-
没有 where 条件,由于您将限制设置为 1,它会更新第一行
-
@coder 对于
where条件我必须给出什么条件? -
其中 id = 某个数字,例如 .如果你想更新第一行,WHERE id = 1
-
您发布的内容包含解析错误。看看语法高亮。
-
@coder 是硬编码的,我需要它每次都不同,具体取决于您自定义的行。实际上它必须更新整个数据库
标签: php html mysql sql sql-update