1

I am trying to update or edit the data from the database but I don't want the edit page to be in another page, I just want it to be in the same page where I can view the different news that the user has added. But the form won't show up when I click the edit link .

Help me find what's wrong or missing?

here's my edit_news.php

<?php 
date_default_timezone_set('Asia/Manila');
include_once('db.php');
if($isset($_GET['id']))
{
    $id=$_GET['id'];
    if(isset($_POST['edit'])) {

    $title = $_POST['title'];
    $body = $_POST['body'];
    $date = date('Y-m-d H:i:s');

    $title = mysql_real_escape_string($title);
    $body = mysql_real_escape_string($body);

    $servername = "localhost";
    $username="root";
    $password = "";
    $database = "zchs_alumni";
    $connection = new mysqli($servername, $username, $password, $database);
    if ($connection->connect_error) {
        die("Connection failed: " . $connection->connect_error);
    }
        $sql = ("UPDATE news SET title = '$title', body = '$body', name = '$name', date = '$date' WHERE id='$id'")or die();
        mysql_query($sql);
        echo "<script type='text/javascript'>alert('Changes saved!'); window.location.assign('/zchs-alumni/news.php');</script>";
    }
}?>

<?php
if($isset($_GET['id']))
{
    $id=$_GET['id'];
    $query=mysql_query("SELECT * FROM news WHERE id='$id'");
    while($row = mysql_fetch_array($query)) {
        $title=$row['title'];
        $body=$row['body'];
?>

<form action="" method="post">
<p>
    <label for="title" id="title">Title</label>
    <input type="text" name="title" value="<?php echo $row['title']; ?>"/>
</p><br/>
<p>
    <label for="body" id="body">Body</label>
    <input type="text" name="body" value="<?php echo $row['body']; ?>"/>
</p><br/>
<p>
    <input type="submit" name="update" value="Save Changes" style="float: right"/>
</p>
</form>
<?php   
    }   }?>

And this is my news.php where the news show up and where I want the editing of the data to take place.

<?php
 mysql_connect("localhost", "root", "") or die(mysql_error());
 mysql_select_db("zchs_alumni") or die(mysql_error());
$query = mysql_query("SELECT * FROM news ORDER BY date DESC LIMIT $start, $limit"); 


 while($row = mysql_fetch_array($query)) {

?>
<p> <span><h3><?php echo $row['title']; ?></h3></span></p>
<p> <span><?php
    $img = $row['photo'];
        if($img != ""){
            $image = 'news/'.$img;
            echo '<center><img src="'.$image.'" width="750" height="350" alt=""></center>';
        }
    ?></span></p>
<br/>
<p> <span><?php echo $row['body']; ?></span></p>
<br/>
<p> <span><h6>Posted at
    <?php
        $row_date = strtotime($row['date']);
        echo date("F j, Y, g:i a", $row_date);
    ?></h6></span></p>
<br/>
 <p><span><a href="edit_news.php?id=<?php echo $row['id']; ?>"><span class="edit" title="Edit">EDIT</span></a></p>
<?php
}
 ?>
Louie
  • 219
  • 4
  • 13
  • 1
    Please dont use the `mysql_` database extension, it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the `PDO` or `mysqli_` database extensions, [and here is some help to decide which to use](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – RiggsFolly Feb 06 '16 at 12:47
  • _I am trying to update or edit the data from the database but I don't want the edit page to be in another page_ Then you are going to have to learn how to us AJAX. This code does not attempt to use AJAX, so you are quite a way from your goal. Can I just remind you that SO is **not a free coding or tutorial service** You have to show that you have made some effort to solve your own problem. – RiggsFolly Feb 06 '16 at 12:51
  • I'm stll just learning php that's why I'm asking if I missed something because I don't think I did because there's no error on the code. I tried solving it for hours but since I couldn't I asked some help on here. I do not know AJAX so I'm trying to do it in php, and since it's not possible, then I'll try to use AJAX then. Thank u! @RiggsFolly – Louie Feb 06 '16 at 13:00

1 Answers1

-1

I have seen these guys do that. Simple check if the post variable is defined if(isset($_POST)) and if so then it is an update submission, if not then display the form!

milton cedeno
  • 58
  • 1
  • 4