-1

I have created a database in phpMyAdmin with table for topics such as Math, Physics etc. Each topic has a topic_id as primary key in table tb_topic. Each topic can have multiple videos. Admin can add videos to each topic which requires topic_id as foreign key in table tb_video_management to store videos against a topic.

Backend: There is a button to add videos in form.php

form.php:

<?php
    $query = "SELECT topic_id, topic_name,aid FROM tb_topic";
    $result = $con->query($query);
    while($query_row = mysqli_fetch_assoc($result)) {?>
        <tr>
            <td><form action='edit.php' method='get'>
                <button type='submit' name='edit' id = 'edit' value='<?php echo $query_row['topic_id'];?>' class='btn btn-success'>EDIT</button><br />
            </form></td>
        </tr>
    <?php 
    } ?>

When this button is clicked, page is navigated to another page "edit.php" which has 2 buttons, one for add new video and second for viewing video gallery.

edit.php:

$id = $_GET['edit'];
echo 'topic id----> ' . $id;

<form action='add_video.php' method='get'>
    <button type='submit' name='add' id = 'add' value='<?php echo $id;?>' class='btn btn-success'>ADD NEW VIDEO</button>
</form>
<form action="video.php" method="get">
    <button type="submit" name="goback" id="goback" value="submit" class="btn btn-primary">VIEW GALLERY</button>
</form>

The class where videos are added has a form for topic name, details etc.

add_video.php

$id = $_GET['add'];
echo 'topic id----> ' . $id;

<form action="add_video.php" method="post">
        Video Title: <input type="text" name="video_name"><br />
        Video Detail: <input type="text" name="video_detail"><br />
        Video Question: <input type="text" name="video_question"><br />
        Video Link: <input type="text" name="video_link"><br />

        <input type="submit" name="submit" value = "Submit"><br />
</form>

When "submit" button is clicked, following code executes:

if(isset($_POST['submit'])) {

    if(!(empty($_POST['video_name']) || empty($_POST['video_detail']) || empty($_POST['video_question']) || empty($_POST['video_link']))) {
        $name = $_POST['video_name'];
        $detail = $_POST['video_detail'];
        $question = $_POST['video_question'];
        $link = $_POST['video_link'];
        $insert = "INSERT INTO tb_video_management(topic_id, video_name, video_detail, video_question,video_link) VALUES ('$id','$name','$detail', '$question','$link')";
        if(!mysqli_query($con,$insert))
            echo "error";
        header('location:edit.php');
    }
}

The problem I am facing is, when I click submit button, the value in $id is lost (may be because another button (submit) is pressed) and it fails to insert the record as there is no value for "topic_id" anymore. I cannot resolve this issue of keeping foreign key value even after pressing submit button. So far I have been creating extra table to hold value for topic_id which is definitely not the right approach.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
SMB
  • 25
  • 5
  • Would it help to include a hidden input in the last form, something like ``, and then retrieve it with `$topic_id = $_POST['topic_id'];`? – showdev Jul 02 '19 at 05:25
  • @showdev Are you talking about adding the hidden input in add_video.php? `$topic-id = $_POST['topic_id'];` should be added inside `if(isset($_POST['submit']))`? – SMB Jul 02 '19 at 05:29
  • Yes you may acces the value $topic_id = $_POST['topic_id']; under if(isset($_POST['submit'])) – Shivani Sonagara Jul 02 '19 at 05:32
  • Yes, the ID can be passed to your PHP script in the same way as the other values like "video_name". See [](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/hidden). – showdev Jul 02 '19 at 05:32
  • It has worked for inserting data to database. However, when The page is redirected back to edit.php, it displays error of undefined index for `$_GET['edit']` because there is no more a value for `$_GET['edit']` – SMB Jul 02 '19 at 05:41
  • You can pass it back to `edit.php`: `header('location:edit.php?edit=' . $topic_id);` – showdev Jul 02 '19 at 05:44
  • @showdev It works after adding video because header() is inside code which executes after pressing submit. But if I press go back button `
    ` then it does not work.
    – SMB Jul 02 '19 at 05:58
  • You can use a hidden input in that form, too. Or pass the ID in the button value as you did in the "ADD NEW VIDEO" form. The idea is that, if you're navigating to a new page, you'll need to pass the data to that page somehow, either in the URL or posted from a `
    `. (You could also use `$_SESSION` variables, but that might get a little more complicated.)
    – showdev Jul 02 '19 at 06:00
  • You might find this informative: [PHP Pass variable to next page](https://stackoverflow.com/a/872522/924299). – showdev Jul 02 '19 at 06:05

1 Answers1

2

To use the $id into another page, you must have to store that in such a way that it will be remain exist after submit.That can be done by using the hidden field.

You may set the value of $id like:

<input type="hidden" name="topic_id" value="<?php echo $id?>">

Once you submit and redirect to add_video.php, you may get the value of $id same as another field video_detail.

On submit look like:

if(isset($_POST['submit'])) {
    $topic_id = $_POST['topic_id'];
}
Shivani Sonagara
  • 1,299
  • 9
  • 21
  • Do I have to add this statement in both edit.php and add_video.php? – SMB Jul 02 '19 at 05:34
  • Yes, you have to add hidden field into edit.php file. So, you may get value of topic_id using the post method under the add_video.php. again same add that value as a hidden field under the add_video.php file and get value to submit button. – Shivani Sonagara Jul 02 '19 at 05:39
  • It has worked for inserting data to database. However, when the page is redirected back to edit.php, it displays error of undefined index for `$_GET['edit']` because there is no more a value for `$_GET['edit']`. Same error exists for `$_GET['add']`on add_video.php but it is not displayed due to redirection of page to edit.php. However, the error exists. – SMB Jul 02 '19 at 05:44
  • After submitting the new video, you will redirect to edit page? – Shivani Sonagara Jul 02 '19 at 05:47
  • Yes, it does redirect to edit page. I resolved error on edit page using `header('location:edit.php?edit=' . $topic_id);` as told by showdev. But if I stay at add_video.php page, then it displays error for undefined index 'add'. – SMB Jul 02 '19 at 05:54
  • You can do same for the add functionality. just pass the $topic_id as a query parameter. – Shivani Sonagara Jul 02 '19 at 05:57