首页 > 解决方案 > 注意:未定义的变量错误在堆栈溢出时尝试了所有方法

问题描述

这些是进来的错误:

注意:未定义变量:第 25 行 C:\xampp\phpMyAdmin\Book Shop\connect.php 中的名称

注意:未定义变量:第 25 行 C:\xampp\phpMyAdmin\Book Shop\connect.php 中的电子邮件

注意:未定义变量:第 25 行 C:\xampp\phpMyAdmin\Book Shop\connect.php 中的年龄

注意:未定义变量:第 25 行 C:\xampp\phpMyAdmin\Book Shop\connect.php 中的部门

注意:未定义变量:第 25 行 C:\xampp\phpMyAdmin\Book Shop\connect.php 中的年份

致命错误:未捕获的错误:调用 C:\xampp\phpMyAdmin\Book Shop\connect.php:26 中未定义的方法 mysqli::execute() 堆栈跟踪:#0 C:\xampp\phpMyAdmin\Book Shop\signUp.php (14): include() #1 {main} 在第 26 行的 C:\xampp\phpMyAdmin\Book Shop\connect.php 中抛出

代码 HTML:

<!DOCTYPE html>
<html>
 <head>
      <meta name="viewport" content="width=device-width", initial-scale=1, shrink-to-fit=no>
      <meta name="author" content="Harsh Kumar Sharma"> 
      <meta name="description" content="BookShop For Second Hand books For College Level">  
      <meta name="keywords" content="BookShop , Second Hand">
      <meta charset="UTF-8">
      <title>ICEM Book Shop</title>
      <link href="https://fonts.googleapis.com/css?family=Montserrat:300,500,600" rel="stylesheet">
      <link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500i" rel="stylesheet">
      <link type="text/css" rel="stylesheet" href="signUp.css">
 </head>
 <body>  <?php include 'connect.php'; ?>
         <div class="nav">
                <div class="logo">
                  <a href="#">
                      <img src="Images/logo_main.JPG" style="height: 40px ; width: 40px"></a>
                  <a href="signIn.html"><p style="margin:0px; font-size:35px ; color: #ffffff; display:inline-block; position: absolute">COLLEGE BOOKSTORE</p>
                  </a>
                </div>
                <div class="nav-menu">
                    <!-- Space for navigation menu -->
                </div>
            
         </div>
         <div class="sq_box">
            <center>
                <h1 style="color: white;">Welcome to THE COLLEGE BOOKSTORE</h1>
                <h1 style="color: white;">Sign Up</h1>
                <p>Register here to be a part of US.</p>
            </center> 
             <div style="padding:20px 0px 0px 140px; font-size: 20px;" id="form">
                <form method="POST" action="connect.php">
                 <label for="text">Name</label><br>
                 
                 <input type='text' placeholder="Full Name" id="name" name="name"><br>
                 
                 <label for="text">Age</label><br>
                 
                 <input type='text' placeholder="Age" id="age" name="age"><br>
             
                 <label for="email">Email</label><br>
             
                 <input type="email" placeholder="Email" id="email" name="email"><br>        
                 
                <label for="text">Department</label><br>
                    <input type="radio" id="computer" name="department">
                    <label for="computer">Computer Department</label><br>
                    <input type="radio" id="mechanical" name="department">
                    <label for="mechanical">Mechanical Department</label><br>
                    <input type="radio"  id="civil" name="department">
                    <label for="civil">Civil Department</label><br><br>
                 
                 <label for="text">Year</label><br>
                    <input type="radio" id="FE" name="year">
                    <label for="computer">FE</label>
                    <input type="radio" id="SE" name="year">
                    <label for="mechanical">SE</label>
                    <input type="radio"  id="TE" name="year">
                    <label for="civil">TE</label>
                    <input type="radio"  id="BE" name="year">
                    <label for="civil">BE</label><br><br>
                 
                 <label for="password">Password</label><br>
                 
                 <input type='password' placeholder="Password" id="password" name="password"><br>
                 
                 <label for="password">Confirm Password</label><br>
                 
                 <input type='password' placeholder="Confirm Password" id="con_password"><br>
                 <br><br>
                 <button type="submit" name="submit"><b>Submit</b></button><br><br>
                </form>
             </div>
         </div>  


         <script src="https://www.gstatic.com/firebasejs/7.19.1/firebase-app.js"></script>
         <script src="https://www.gstatic.com/firebasejs/7.19.1/firebase-analytics.js"></script>
         <script src="https://www.gstatic.com/firebasejs/7.19.1/firebase-auth.js"></script>
         <script src="https://www.gstatic.com/firebasejs/7.19.1/firebase-database.js"></script>
         <script src="login.js"></script>
 </body>
</html>

PHP:

<?php
$servername = "localhost";
$username = "root";
$password = "" ;
$database = "bookshop";
$conn = new mysqli($servername, $username, $password , $database);

if (mysqli_connect_errno()) 
              {
                  echo "Failed to connect to MySQL: " . mysqli_connect_error();
                  exit();
              }
  if(isset($_POST["submit"])){
  $name = $_POST["name"] ;
  $email = $_POST["email"];
  $age = $_POST["age"];
  $dept = $_POST["department"];
  $year = $_POST["year"];
  $password = $_POST["password"];
  }
 
  $sql = "INSERT INTO `userinfo`(`Name`, `Age`, `E-mail`, `Department`, `Year`, `Password`)VALUES (?,?,?,?,?,?) ";
  $stmtinsert = $conn->prepare($sql);
  $names = array($name,$email,$age,$dept,$year,$password);
  $result = $conn->execute($names);
  if($result)
  {
      echo "Successfully Saved ";
  }
   else
   {
    echo "error occured ";
   }
?>

标签: phphtml

解决方案


HTML 中的行<?php include 'connect.php'; ?>导致了问题。你有两个解决方案:

  • 第一个:只需删除该行。

  • 或者第二种:保留该行,然后在 中connect.php,而不是用 包裹所有内容if(isset($_POST["submit"])){ /* ... */ },然后在 HTML 表单中,将action属性修改为action=""(空!然后表单将提交到同一页面)

对于 msqli 错误,您必须调用(do: ,而不是)execute返回的对象上的方法$conn->prepare()$stmtinsert->execute()$conn->execute()


推荐阅读