[英]Update table values to database
我所拥有的是一个基于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();
}
}
例
例如,如果我编辑“ Support”并使其成为“ Supporttt”,它将在数据库中对其进行编辑, 但是它将“ Home”设置为“ Suporttt”,并且不会将“ Support”更改为“ Suporttt”,因此它不会更新正确的记录
您的查询应类似于:
$this->database->query("UPDATE ".$tabel." SET ".$q." WHERE id='".$id."' LIMIT 1");
这是您需要做的。 您会看到设置了一个名为pageID的隐藏字段。将该隐藏字段用作更新查询的where子句条件。
如下更改代码
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']);
$id = $app->check_string($_POST['pageID']);
$app->update_tabel('pages', $updateVAL,$id);
}
如下更改更新功能
public function update_tabel($tabel, $updateVAL,$id){
$q = '';
foreach ($updateVAL as $key => $value) {
$value = str_replace("'","'",$value);
$q .= "`".$key."` = '".$value."',";
}
rtrim($q,',');
$q .= " WHERE id = $id";
if(isset($q) && ($q !='')) {
$q = substr($q,0,-1);
$this->database->query("UPDATE ".$tabel." SET ".$q." LIMIT 1");
$this->database->execute();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.