使用php更新数据库

时间:2016-12-12 18:04:59

标签: php mysql database sql-update

这段代码看起来很糟糕,我知道。我不知道如何修复它。当我尝试使用编辑网页更新表时,只有第一列中的第一行更新,但第二行(第一行)中的副标题未更新。有没有办法改变这个?抱歉可怕的解释。

更新页面

 //Home Title
    $homeTitleUpdate = $_POST["homeTitleChange"];
    $editRow = $_POST["rowID"];
    $query = " UPDATE Home SET title = '$homeTitleUpdate' WHERE homeID = '$editRow'  ";
    $result = mysqli_query($conn, $query) or die(mysqli_error($conn));

    if ($result) {
        echo "<p> - Title updated succesfully to $homeTitleUpdate.</p>";
    }   else {
        echo "<p> - Title did not update. Something went wrong</p>";
    }


   //Home Subtitle
   $homeSubtitleUpdate = $_POST["homeSubtitleChange"]; 
   $query1 = " UPDATE Home SET subtitle = '$homeSubtitleUpdate' ";
   $result1 = mysqli_query($conn, $query) or die(mysqli_error($conn));

   if ($result) {
        echo "<p> - Subtitle updated successfully to $homeSubtitleUpdate.</p>";
    }   else {
        echo "<p> - Subtitle did not update. Something went wrong</p>";
    }

编辑页面         

                <?php

                echo  "<h2 style='color:black'>";
                echo  "<input type'text' name='homeTitleChange' value=$homeTitle>";
                echo  "<input type='hidden' name='rowID' value=$getID>";
                echo  "</h2>";

                echo "<h4 style='color:black'>";
                echo  "<input type'text' name='homeSubtitleChange' value=$homeSubtitle>";
                echo  "<input type='hidden' name='rowID' value=$getID>";
                echo "</h4>";

                ?>



                <input type="submit" value="save" />

2 个答案:

答案 0 :(得分:0)

您需要在更新字幕时添加“where”条件

$query1 = " UPDATE Home SET subtitle = '$homeSubtitleUpdate' WHERE homeID = '$editRow'  ";

另一方面,您可以在单个查询中更新它们,如此

$query = " UPDATE Home 
SET title = '$homeTitleUpdate', subtitle = '$homeSubtitleUpdate'   
WHERE homeID = '$editRow'  ";

这不会更好吗?除非你有某些特定原因

答案 1 :(得分:0)

   <?php

            echo  "<h2 style='color:black'>";
echo "<form action="change to your file" method="post">
            echo  "<input type'text' name='homeTitleChange' value=$homeTitle>";
            echo  "<input type='hidden' name='rowID' value=$getID>";
            echo  "</h2>";

            echo "<h4 style='color:black'>";
            echo  "<input type'text' name='homeSubtitleChange' value=$homeSubtitle>";
            echo  "<input type='hidden' name='rowID' value=$getID>";
            echo "</h4>";

            ?>



            <input type="submit"  name="submit" value="save" />
</form>

你没有表格

 //Home Title
if(isset($_POST['submit'])){
if
(
!empty($_POST["homeTitleChange"])
&& 
!empty($_POST["homeSubtitleChange"]) &&  
!empty($_POST["rowID"])
)
{
$homeTitleUpdate = $_POST["homeTitleChange"];
  $homeSubtitleUpdate = $_POST["homeSubtitleChange"]; 
    $editRow = $_POST["rowID"];
    $query = "UPDATE Home SET title = '$homeTitleUpdate', subtitle ='$homeSubtitleUpdate' WHERE homeID = '$editRow' ";
    $result = mysqli_query($conn, $query) or die(mysqli_error($conn));

    if ($result) {
        echo "<p> - Title/Subtitle updated succesfully to $homeTitleUpdate.</p>";
    }   else {
        echo "<p> - Title/Subtitle did not update. Something went wrong</p>";
    }
}
}

您可以在一个查询中更改您的PHP并完成所有操作