0

I'm getting an error " Undefined index: tableName in C:\xampp\htdocs\online test\study_question1.php on line 8"

My URL is :"http://localhost/online%20test/study_question1.php?tableName=computer_science"

This is PHP code for below html code

    <?php
        $connection = mysqli_connect('localhost','root','','userquestion') or die(mysqli_error($connection));

        if(isset($_POST['next'])){
            $table=$_POST['table_name'];
            $query= "CREATE TABLE `userquestion`.`$table` ( `id` INT(15) NOT NULL AUTO_INCREMENT , `question` TEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL , `option1` TEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL , `option2` TEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL , `option3` TEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL , `option4` TEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL , `true_ans` TEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;";
           $data = mysqli_query($connection,$query);
           header('Location: study_question1.php?table_name=' . $table);

        }


?>

HTML code from where I,m taking table_name

<html> 

<link rel="stylesheet" type="text/css" href="style.css">
<head> 
<title>Online</title> 
</head> 
<body id="body-color"> 


<div id="Sign-Up"> 
<fieldset style="width:30%">
<legend>Online</legend> 
<table border="0"> 
<tr> 
<form method="POST" action="online.php">
 <tr> 
 <td>table</td>
 <td> <input type="text" name="table_name"></td> 
 </td>

 </tr> 
 <tr> 
 <td><input id="button" type="submit" name="next" value="next"></td> 
 </tr> 

 </form> 
 </table> 
 </fieldset> 
 </div>

 </body> 

My PHP code is

 <?php
$connection = mysqli_connect('localhost','root','','userquestion') or die(mysqli_error($connection));
 if(isset($_POST['next'])) 
{
$table = $_GET['table_name'];
 $question = $_POST["question"];
 $option1 = $_POST['option1'];
 $option2 = $_POST['option2'];
 $option3 = $_POST['option3']; 
  $option4 = $_POST['option4'];
   $true_ans = $_POST['true_ans'];
 $query = "INSERT INTO `$table` (question,option1,option2,option3,option4,true_ans) VALUES ('$question','$option1','$option2','$option3','$option4','$true_ans')"; 
 $data = mysqli_query($connection,$query);

 if($data) 
 {
 echo "ab dusra dal..."; 
 }
 } 

 ?>
Shubham Gosewade
  • 65
  • 1
  • 1
  • 7

1 Answers1

0

This should help. GET and POST come from different places. GET is from the URL and POST is from the form. The code below has all unnecessary code removed so the answer is more transparent.

<?php
if (isset($_POST['next'])) {
    $table = $_POST['table_name'];
    echo "You picked: " . $table . " in the form";
} else if (isset($_GET['table_name'])) {
    $table = $_GET['table_name'];
    echo "You picked: $table in the url";
} else {
    echo "first call to page";  // you can delete this line after testing    
}
?>
<html> 
    <head> 
        <title>Online</title> 
    </head> 
    <body id="body-color"> 
        <div id="Sign-Up"> 
            <fieldset style="width:30%">
                <legend>Online</legend> 
                <form method="POST" action="online.php">
                    <table border="0"> 
                        <tr> 
                        <tr> 
                            <td>table</td>
                            <td> <input type="text" name="table_name"></td> 

                        </tr> 
                        <tr> 
                            <td>&nbsp;</td>
                            <td><input id="button" type="submit" name="next" value="next"></td> 
                        </tr> 

                    </table> 
                </form> 
            </fieldset> 
        </div>
    </body>
</html>

This is the URL for your question above.

http://localhost/online%20test/study_question1.php??table_name=fred

geeves
  • 652
  • 7
  • 24