简体   繁体   English

PHP URL参数错误不采取

[英]PHP URL parameter error is not taking

I'm getting an error " Undefined index: tableName in C:\\xampp\\htdocs\\online test\\study_question1.php on line 8" 我收到一个错误“第8行的C:\\ xampp \\ htdocs \\ online test \\ study_question1.php中的未定义索引:tableName”

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

This is PHP code for below html code 这是下面的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 code from where I,m taking table_name 我从中获取table_name的HTML代码

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

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

 ?>

This should help. 这应该有所帮助。 GET and POST come from different places. GET和POST来自不同的地方。 GET is from the URL and POST is from the form. GET来自URL,而POST来自表单。 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 HTTP://localhost/online%20test/study_question1.php ?? TABLE_NAME = fred的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM