-1

This code looks horrible and I know. I don't know how to fix it though. When I try and update the table using the edit web page, only the first row in the first column updates but the subtitle is not updating in the second column, first row. Is there a way to change this? Sorry for the terrible explanation.

Update Page

 //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>";
    }

Edit Page

                <?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" />
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
monkey232
  • 57
  • 8
  • I don't see any form tags. And I suggest protecting your file against mysql injection (take a look here: http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Julian Dec 12 '16 at 18:07
  • You need to specify where condition while updating subtitle as well – Talha Abrar Dec 12 '16 at 18:08
  • You could have updated both the columns in the same query right ? – Nandan Bhat Dec 12 '16 at 18:08
  • doesn't $query1 also update a row with a specific ID? – Emmanuel N K Dec 12 '16 at 18:09
  • @Talha Thanks very much. Going in the right direction, but when i edit the data in the database now change to 1. So for example I tried to update the title to Title, it said successful, but changed to 1. – monkey232 Dec 12 '16 at 18:58

2 Answers2

0

You need to add 'where' condition while updating subtitle as well

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

on other hand, you can update both of them in single query, like this

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

wouldn't this be better? unless you have some specific reason

Talha Abrar
  • 880
  • 8
  • 22
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>

You did not have a 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>";
    }
}
}

You can change your php and do it all within one query

Adam Hull
  • 214
  • 1
  • 8
  • Thanks very much. Going in the right direction, but when i edit the data in the database now change to 1. So for example I tried to update the title to Title, it said successful, but changed to 1. – monkey232 Dec 12 '16 at 18:57
  • This worked! Thanks very much. I know you have did a lot already, but can you give a brief description of what the empty parts do? – monkey232 Dec 12 '16 at 19:20
  • This stops a empty value being past through POST meaning that you will not get a undefined variable error – Adam Hull Dec 12 '16 at 19:21
  • But if you need to enter a blank then it will have to be changed – Adam Hull Dec 12 '16 at 19:22
  • But also please bear in mind this script is not secure and will need securing, if learning php I advise you learn Mysqli Prepared statements http://php.net/manual/en/mysqli.quickstart.prepared-statements.php/ – Adam Hull Dec 12 '16 at 19:25