-3

These are the errors coming in:

Notice: Undefined variable: name in C:\xampp\phpMyAdmin\Book Shop\connect.php on line 25

Notice: Undefined variable: email in C:\xampp\phpMyAdmin\Book Shop\connect.php on line 25

Notice: Undefined variable: age in C:\xampp\phpMyAdmin\Book Shop\connect.php on line 25

Notice: Undefined variable: dept in C:\xampp\phpMyAdmin\Book Shop\connect.php on line 25

Notice: Undefined variable: year in C:\xampp\phpMyAdmin\Book Shop\connect.php on line 25

Fatal error: Uncaught Error: Call to undefined method mysqli::execute() in C:\xampp\phpMyAdmin\Book Shop\connect.php:26 Stack trace: #0 C:\xampp\phpMyAdmin\Book Shop\signUp.php(14): include() #1 {main} thrown in C:\xampp\phpMyAdmin\Book Shop\connect.php on line 26

CODE 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 ";
   }
?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Why are you including the `connect.php` file in the HTML ( ``). You do not need to include it. – Prince Dorcis Sep 13 '20 at 11:42
  • @PrinceDorcis Thank you. i removed it but the problem is still there. – Harsh Kumar Sharma Sep 13 '20 at 11:47
  • You can instead keep the include there, but you need to wrap the content of the connect.php file with `if(isset($_POST["submit"])){ /* ... */ }` instead. Kindly check my answer below. – Prince Dorcis Sep 13 '20 at 11:57
  • @PrinceDorcis What you told i got it. And 5 errors are fixed but the last error of execute() is not resolving. Pls if you can tell it would be helpful – Harsh Kumar Sharma Sep 13 '20 at 12:03
  • I've included it in the answer. Kindly check. – Prince Dorcis Sep 13 '20 at 12:06
  • **Never store passwords in clear text or using MD5/SHA1!** Only store password hashes created using PHP's [`password_hash()`](https://php.net/manual/en/function.password-hash.php), which you can then verify using [`password_verify()`](https://php.net/manual/en/function.password-verify.php). Take a look at this post: [How to use password_hash](https://stackoverflow.com/q/30279321/1839439) and learn more about [bcrypt & password hashing in PHP](https://stackoverflow.com/a/6337021/1839439) – Dharman Sep 13 '20 at 17:50

1 Answers1

-2

The line <?php include 'connect.php'; ?> in the HTML is causing the problem. You have two solutions:

  • The first: simply remove that line.

  • Or the second: Keep the line there, then in the connect.php, rather wrap all the content with if(isset($_POST["submit"])){ /* ... */ }, then in the HTML form, modify the action attribute to action="" (empty! Then the form will be submitted to the same page)

For the msqli error, you must call the execute method on the object returned by $conn->prepare() (do: $stmtinsert->execute(), instead of $conn->execute())

Prince Dorcis
  • 955
  • 7
  • 7
  • I did this and the errors on line 25 were gone. But still it is showingFatal error: Uncaught Error: Call to undefined method mysqli::execute() in C:\xampp\phpMyAdmin\Book Shop\connect.php:26 Stack trace: #0 {main} thrown in C:\xampp\phpMyAdmin\Book Shop\connect.php on line 26 – Harsh Kumar Sharma Sep 13 '20 at 11:56
  • I've included it in the answer. Kindly check. – Prince Dorcis Sep 13 '20 at 12:04