注意:未定义的索引PHP 1

时间:2014-03-27 18:37:32

标签: php html

或一些奇怪的原因代码开始工作没有任何理由我觉得服务器很累。我不知道。

您好我已经阅读了所有类似的答案,我无法找到解决问题的方法。如果有人可以指出这将是非常有帮助的。好吧,因为这个问题已被标记为已经回答,所以我将详细说明。我已经阅读了其他解决方案以及他们所提到的内容,我至少尝试了所有这些解决方案。我尝试过使用isset($ _ POST ['submit'])我在使用它之前声明了所有变量,但仍然无法使用此代码。

这是我的HTML。

<!DOCTYPE html>
<html>
    <head>
        <title>php-form.html</title>
        <meta charset = "utf-8">

    </head>
    <body>

<?PHP

$first = $last = $email = $telephone = "";
$first_error = $last_error = $tele_error = ""; 

if(isset($_POST['submit']))
{
    if(isset($_POST["first"]) && $_POST["first"] == "")
    {
        $first_error = "Please enter first name";
    }
    else{

            $first = test_form_variable($_POST["first"]);
    }
   echo "2";

    if(isset($_POST["last"]) && $_POST["last"] == "")
    {

        $last_error = "Please fill your last name.";
    }
    else{

            $last = test_form_variable($_POST["last"]);
    }

    if(isset($_POST["telephone"]) && $_POST["telephone"] == "")
    {
        $tele_error = "telephone is required";

    }
    else{

            $telephone = test_form_variable($_POST["telephone"]);
    }


  require "db.php";


}


function test_form_variable($data)
{

   $data = trim($data);
   $data = stripcslashes($data);
   $data = htmlspecialchars($data);
   return $data;

}

 ?>

        <form method = "post"  action="<?php echo $_SERVER['PHP_SELF'] ?>">
            First Name: <input type="text" name = "first">
            <span class = "error">*<?php echo $first_error;?></span><br><br>
            Last Name: <input type="text" name = "last">
            <span>*<?php echo $last_error; ?></span><br><br>
               Email: <input type="text" name = "email"><br><br>
            Telephone: <input type="number" name = "telephone">
            <span>*<?php echo $tele_error; ?></span><br>

            <br>

            Gender:
            <input type="radio" name = "gender" value = "female">Female
            <input type="radio" name = "gender" value = "male">Male
            <br>

            Submit: <input type="submit" value ="Submit" name="submit">


        </form>
        <form action="table.php" method="post" >

            <input type="submit" name = "sh" value = "Show all records" >

        </form>


    </body>

</html>

这是另一个在按下show all records按钮时调用的php文件。我尝试使用isset(post [])并且没有发布但没有工作的代码。如果没有错误,则显示空白页面上没有任何内容。

<?php

if(isset($_POST['sh']))
{



$connection = mysqli_connect('localhost', 'root', ''); //The Blank string is the password
if(!$connection)
{
    echo "Connection Unsuccessfull". mysqli_error();
}
mysqli_select_db($connection, "db");

$query = "SELECT * FROM person"; //You don't need a ; like you do in SQL
$result = mysqli_query($connection, $query);
if($result === false)
{
  die(mysqli_error($connection));
}

$row = "";

while($row = mysqli_fetch_array($result, MYSQLI_NUM))
{   //Creates a loop to loop through results
    printf($row[0]);
    printf($row[1]);
    printf($row[2]);
    printf($row[3]);
    printf($row[4]);

}



mysqli_close($connection); //Make sure to close out the database connection
}
?>

1 个答案:

答案 0 :(得分:1)

你的问题是那个

<form action="table.php" method="post" >
    <input type="submit" name = "sh" value = "Show all records" >
</form>

没有你想要的任何$ _POST语句......

if(isset($_POST['sh']))
{
    $first = $_POST['First'];
    $last = $_POST['Last'];
    $email = $_POST['Email'];
    $telephone = $_POST['Telephone'];
                // ^ these values are not defined anywhere, in the form above.

此外,您没有使用上面的变量......在它下面的任何地方......