0

Im having trouble passing the value id in my code to edit.php.

In displaynews i print out articles from the database. It also creates a link that redirects me to edit.php and sending the $id value with it.

Link to displaynews function

http://snipt.org/zhla8

Here's where im having trouble

      <h3>EDIT NEWS ARTICLE</h3>
      <form id="EditNews" name="EditNews" method="POST" action="edit.php">
      <textarea name="editnewstext"><?php $news=Textarea(1);echo $news ?></textarea> <!--HERE i need to replace the 1 with id passing in displaynews -->
                        <input type="submit" name="Edit_News" id="Edit_News">

                        <?php
                       include 'db.php';
                       include'editnewsarticle.php';
                             if(isset($_POST['Edit_News']))
                          {

                                $content= $_POST['editnewstext'];
                  geteditnews(1,$content); //<!--HERE i need to replace the 1 with idpassing in displaynews -->
                                Header("location:Home.php");

                          }   

Link to Edit.php page

http://snipt.org/zhkj8

Link to GetnewsTextarea function

http://snipt.org/zhlb9

Link To editnewsarticle function

http://snipt.org/zhki2

Please dont comment on how the mysql extension is depreciated and that my code is open for sql injections. Thanks in advance

EDIT: Here's the solution

                       if(isset($_GET['id']))
                        {
                        $id = $_GET['id'];
                        $data = mysql_query("SELECT * FROM news WHERE id = '$id' "); 
                    }
                        ?>
2
  • Header("location:Home.php"); won't work if you already printed anything, which you have already done. Also merging HTML and PHP that way is not incorrect but it's REALLY hard to follow, I'd advice you to append the HTML code to a variable and echo it at the end. This also makes the header() function work since you haven't printed anything until the end. Commented Mar 20, 2013 at 12:38
  • @Naryl Thanks ill do that Commented Mar 20, 2013 at 12:45

2 Answers 2

1

Does these changes in edit.php help?

if (isset($_POST['id']))
    $id = $_POST['id'];

<?php $news=Textarea($id);echo $news ?>  

geteditnews($id, $content); 
Sign up to request clarification or add additional context in comments.

2 Comments

Where do you suggest i put the if (isset($_POST['id'])) $id = $_POST['id'];?
Thanks for your help, your answer pointed me in the right direction.
1

Add a hidden field for the id right after the form tag. As such:

<form id="EditNews" name="EditNews" method="POST" action="edit.php">
    <input type="hidden" name="id" value="<?php echo $id; ?>">

3 Comments

You had stated that you were having problems passing the id along. I couldn't see any field actually containing the id to pass it onto edit.php. Two different pages don't have access to each other's variables, and to get it to edit.php, you need to be able to pull it from the GET or the POST. By making a hidden field, you're passing it through the POST so that you can pull it out with $_POST['id']
Thanks for explaining. I ended up using it.
Ah, wonderful! Glad I could help! I should have explained in rather more detail, I'm afraid, the first time around.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.