douhu8851 2017-02-28 17:08
浏览 84

PHP URL参数错误没有采取

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

 ?>
  • 写回答

1条回答 默认 最新

  • douyi1779 2017-02-28 19:15
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据