我已经创建了一个网站,可以上传文章,但是我无法更新已创建的文章。我已经成功地填写了一个表格,其中填写了预先填写好的数据库中的信息,但是当我提交任何更改后再转到文章中时,它不会更新。无法更新PHP中的记录
$ var_value是从上一页传递的主键,用于确定要加载的文章&编辑。
这是我的表单来更新文章。
<?php
$var_value = $_POST['varname'];
$get_studies = "select * from news where `news`.`articleID` = $var_value";
$run_studies = mysqli_query($con,$get_studies);
while($row_studies = mysqli_fetch_array($run_studies)){
$newsTitle = $row_studies['title'];
$newsDate = $row_studies['date'];
$shortBio = $row_studies['shortBio'];
$longBio = $row_studies['longBio'];
$longBio2 = $row_studies['longBio2'];
$image = $row_studies['image'];
}
echo "
<div class='panelContent1' id='addNewsWraper'>
<h2>Dashboard</h2>
<h3>Update Article</h3>
<form method='post' enctype='multipart/form-data' onsubmit='alert('stop submit'); return false;' >
<div class='newsForm'>
<p>Article Title<br /><input type='text' value='$newsTitle' name='newsTitle' /></p>
<p>Short Description<br /><textarea name='newsShort' placeholder='Around a paragraph' />$shortBio</textarea>
<p>Image<br /><input type='file' name='newsImage' /></p>
</div>
<div class='newsForm'>
<p>Date<br /><input type='text' value='$newsDate' name='newsDate' placeholder='2017' /></p>
<p>Story<br /><textarea name='newsLong' placeholder='News article text' />$longBio</textarea>
<p>Story2<br /><textarea name='newsLong2' value='' placeholder='News article text' />$longBio2</textarea>
<button type='submit' name='updateNews'>
Update
</button>
</div>
</form>
</div>
";
?>
这是我如何尝试更新文章。我试图根据主键更新记录,这个变量正在传递给页面,因为它正在加载表单中的内容。
<?php
if(isset($_POST['updateNews'])){
$newsTitle = $_POST['newsTitle'];
$newsDate = $_POST['newsDate'];
$newsShort = $_POST['newsShort'];
$newsLong = $_POST['newsLong'];
$newsLong2 = $_POST['newsLong2'];
$newsImage = $_POST['newsImage'];
$insertNews = "UPDATE mods SET title='$newsTitle', date='$newsDate', shortBio='$newsShort', longBio='$newsLong', longBio2='$newsLong2', image='$newsImage' WHERE articleID='$var_value'";
$updateNews = mysqli_query($con,$insertNews);
if($updateNews){
echo "<script>alert('Article updated.');</script>";
}
}
?>
'$ newsImage = $ _POST ['newsImage'];'你正在使用错误的超全局。 –
,不知道你是否连接到你的数据库;你的代码的第二部分没有显示任何证据。 –
您好弗雷德,我已连接到数据库抱歉,我刚刚添加了我用来尝试更新文章的代码。我应该为此添加更多代码吗?数据库连接工作正常,但页面加载我想从数据库的形式编辑的文章,只是不会允许我保存更改。 – Allan