PHP URL参数错误未执行

时间:2017-02-28 17:08:21

标签: php

我收到错误"未定义的索引:第8行和第34行的C:\ xampp \ htdocs \ online test \ study_question1.php中的tableName;

我的网址是:" http://localhost/online%20test/study_question1.php?tableName=computer_science"

这是以下html代码的PHP代码

    <?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代码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> 

我的PHP代码是

 <?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..."; 
 }
 } 

 ?>

1 个答案:

答案 0 :(得分:0)

这应该有所帮助。 GET和POST来自不同的地方。 GET来自URL,POST来自表单。下面的代码删除了所有不必要的代码,因此答案更加透明。

<?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>

这是您上述问题的网址。

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