PHP使用带表单的POST未定义索引

时间:2016-09-22 10:02:04

标签: php html sql

我有一个不想在浏览器中工作的php文件。

 Style= FONTE: 8pt; TEXT-DECORATION: none; FONT-FAMILY:PADDING-BOTTOM 
 onclick=$find('ReportViewer').exportReport('EXCEL'); 
 href javascript:void(0) /shape ="" _events="[object object ]" _selected ="true">Excel</a> 

尝试打开<!DOCTYPE html> <html lang="en"> <head> <title>Insert A New Skateboard</title> <meta charset="utf-8"> <meta name= "keywords" content = "skateboard"> <!--[if lt IE 9]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"> </script> <![endif]--> </head> <body> <div id="page"> <form name="variable_key" action="insertskateboard.php" method="post"> <!--bring back the form if there is error in the user entry --> <p>Skateboard ID: <input type="text" name="skateboardid" size="15" maxlength="15" /></p> <p>Deck: <input type="text" name="deck" size="20" maxlength="40" /> </p> <p>Trucks: <input type="text" name="trucks" size="10" maxlength="20" /> </p> <p>Wheels: <input type="text" name="wheels" size="10" maxlength="20" /> </p> <p>Image: <input type="text" name="image" size="60" maxlength="50" /> </p> <p><input type="submit" name="submit" value="submit" /></p> </form> <?php include('function.php'); $skateboardid = trim($_POST['skateboardid']); $deck = trim($_POST['deck']); $trucks = trim($_POST['trucks']); $wheels = trim($_POST['wheels']); $image = trim($_POST['image']); /*$query_age = (isset($_GET['query_age']) ? $_GET['query_age'] : null);*/ if (!empty($skateboardid) || !empty($deck) ||!empty($trucks) || !empty($wheels) || !empty($image)) { // Create the insert query $query = "insert into skateboard values('$skateboardid', '$deck', '$trucks', '$wheels', '$image')"; // Execute the query and capture the number of rows altered $result_set = mysql_query($query, RMITconnect()); print "<h4>You have successfully added a skateboard to the database </h4>"; print "<h4>To see the skateboard you just added, click on the <a href= 'products.php'> Products </a></h4> "; } else { print "<h4>You have not entered the form information </h4>"; } ?> </div> </body> </html> 时,收到错误消息:

  

注意:第25行/home/sh5/s3520755/public_html/insertskateboard.php中的未定义索引:skateboardid

3 个答案:

答案 0 :(得分:0)

使用post方法时,请执行以下操作:

    if(isset($_POST))
    {
       $skateboardid = trim($_POST['skateboardid']);
       $deck = trim($_POST['deck']);
       $trucks = trim($_POST['trucks']);
       $wheels = trim($_POST['wheels']);
       $image = trim($_POST['image']); 

    /*$query_age = (isset($_GET['query_age']) ? $_GET['query_age'] : null);*/

    if (!empty($skateboardid) || !empty($deck) ||!empty($trucks) || !empty($wheels) || !empty($image))
    {

        // Create the insert query
        $query = "insert into skateboard values('$skateboardid', '$deck', '$trucks', '$wheels', '$image')";
        // Execute the query and capture the number of rows altered
        $result_set = mysql_query($query, RMITconnect());
        print "<h4>You have successfully added a skateboard to the database </h4>";
        print "<h4>To see the skateboard you just added, click on the <a href= 'products.php'> Products </a></h4> ";        
    }
    else 
    {
        print "<h4>You have not entered the form information </h4>";
    }


}

答案 1 :(得分:0)

您需要检查您的indizes是否已设置。如果您只是调用脚本而$_POSTempty,那么这些目标就不可用了。试试这个:

if (isset($_POST['skateboardid']) && isset($_POST['deck']) ...) {
    $skateboardid = trim($_POST['skateboardid']);
    ...
}

答案 2 :(得分:0)

  
    

&#34;当试图打开insertskateboard.php时,我收到错误:&#34;

  

好。如果你的意思是在浏览器中打开它。那么这是正常的,因为请求来自get方法而不是post

将表单处理代码包装在if($_SERVER['REQUEST_METHOD'] == "POST")中,如下所示

if($_SERVER['REQUEST_METHOD'] == "POST"){
   $skateboardid = trim($_POST['skateboardid']);
   $deck = trim($_POST['deck']);
   $trucks = trim($_POST['trucks']);
   $wheels = trim($_POST['wheels']);
   $image = trim($_POST['image']);
}