noobcoder
noobcoder

Reputation: 152

Inserting ID through PHP and MySQL

I am trying to pass an ID from one page to another. I am getting that ID in the other page and I am trying to insert that ID in a different table. Consider that I have one table 'question' and I am taking the question ID from this table and passing it to another page. I am trying to store this ID in another table 'answer' in the next page. Here are the codes-

The First page-

<?php

                require('connect_db.php');
                $strSQL = "SELECT * FROM question";
                $rs = mysqli_query($conn,$strSQL);
                while($row = mysqli_fetch_array($rs,MYSQLI_ASSOC)) {

                   echo "<a href='AddAnswer.php?IsEdit=1&ID=" .$row['qu_title']. "'><h3 style='font-family: Georgia;margin-left: 3.8%'>".$row['qu_title']."</h3></a>";
                   echo "<h5 style='font-family: Georgia;margin-left: 3.8%'>".$row['qu_text']."</h5>";
                   echo "<hr>";
                  }

                mysqli_close($conn);

            ?>

The Second Page-

<?php
   session_start();
   require('connect_db.php');
   if (isset($_SESSION['email']) and $_SESSION['loggedin'] == "true") {

  global $id;
$id = ($_GET['ID']);
//echo $id;
if (isset($_POST['answer'])) 
{
    $answer=$_POST['answer'];
    mysqli_query($conn,"INSERT INTO answer(qu_id,ans_text)VALUES 
    ('$id','$answer')");
    header("location:Main.php?msg=success");
    mysqli_close($conn);
   }
 } 

 else {
     header("location:Login.php?msg=try");
  }
?>

But there seems to be problem in inserting this ID. Can anyone figure out this problem?

Upvotes: 0

Views: 80

Answers (3)

Eborbob
Eborbob

Reputation: 1975

Your first page has this

echo "<a href='AddAnswer.php?IsEdit=1&ID=" .$row['qu_title']. "'><h3 style='font-family: Georgia;margin-left: 3.8%'>".$row['qu_title']."</h3></a>";

Which will result in a link that looks like this:

AddAnswer.php?IsEdit=1&ID=<qu_title>

If the user clicks on that link to bring up your second page, there will be no POST variables available (nothing was POSTed), and the values IsEdit and ID available for GET.

In your second page though you are trying to get a POST variable called answer:

if (isset($_POST['answer'])) 

But this has never been set.

I suggest you read up about POST and GET, and then probably change the references to $_POST in your second page to $_GET, and make sure you set the value for answer in the HREF of the link in the first page, e.g.:

echo "<a href='AddAnswer.php?IsEdit=1&ID=" .$row['qu_title']. "&answer=$answer'><h3 style='font-family: Georgia;margin-left: 3.8%'>".$row['qu_title']."</h3></a>";

Upvotes: 0

You cannot have POST and GET in same code block. Assuming that the form/php script is also returning values in the GET format in which case, either you should get the ID first and post the answer and ID to second page. or pass the answer and ID through get method. However, it is possible to use both GET and POST if the form/php script is returning POST data and the URL has additional GET data encoded.

Upvotes: 0

Sivabalan
Sivabalan

Reputation: 1131

You are passing the value from first page to second page as get method. But in second page you're using that $_POST['ID'] to insert the value. You can use $_GET['ID'] or $_REQUEST['ID']

Upvotes: 1

Related Questions