无法更新PHP中的记录

时间:2017-08-25 17:24:29

标签: php mysql

我创建了一个网站,我可以上传文章但是我在更新已创建的文章方面遇到了麻烦。我已设法填写一个表格,其中包含预先填写的数据库中的信息,但是当我提交任何更改然后到文章然后它不会更新。

$ var_value是从上一页传递的主键,用于确定要加载哪个文章&编辑。

这是我更新文章的表单。

username/paswword@hostname

以下是我尝试更新文章的方法。我试图根据主键更新记录,这个变量正在传递给页面,因为它正在加载表单中的内容。

<?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>

                ";

            ?>

2 个答案:

答案 0 :(得分:0)

你说$var_value正在传递给你的php更新脚本,但我看不出它是,也不是被POST选中并转移到本地变量。将其作为<input type="hidden"传递,然后选择使用POST

在第一个php脚本中:

<p>Article Title<br /><input type='text' value='$newsTitle' name='newsTitle' /> 
<input type="hidden" value='$var_value' name='var_value' />
</p>

在第二个PHP脚本中:

 $var_value = $_POST['var_value'];

通过使用参数化查询来保护脚本免受sql注入也很好。

答案 1 :(得分:0)

您的查询存在问题

  

insertNews = "UPDATE mods SET title='$newsTitle' date='$newsDate' shortBio='$newsShort' longBio='$newsLong' longBio2='$newsLong2' image='$newsImage' WHERE articleID='$var_value'";

将此替换为您当前的查询,您将很高兴。

相关问题