skip to Main Content

html webpage screenshotphp code shown on button clickmySql database tableI need to store user login data. i am using phpMyAdmin. When I click on submit button, data is not stored. Instead the php code is shown. Both code files are given below. What I am doing wrong. Help me. I
am unable to store user data using phpmyadmin in xampp.

my html code


    <html>
    <head>
    <title>Yahoo Signin And Signup Form</title>
    </head>
    <body>
     <h2 style="color: midnightblue">yahoo!</h2>
     <hr color="magenta">
    <form method="post" action="connect.php" >
    <fieldset style="background:#6495ED;">
    <legend style="padding:20px 0; font-size:20px;">Signup:</legend>
    <label for ="firstName">Enter First Name</label><br>
    <input type="text" placeholder="First name" id="firstName" name ="firstName"> 
    <br>
    <label for ="lastName">Enter Last Name</label><br>
    <input type="text" placeholder="Last name" id="lastName" name ="lastName"> 
    <br>
    <label for ="email">Enter Email</label><br>
    <input type="text" placeholder="Email" id="email" name ="email"><br>
    <label for ="password">Enter Password</label><br>
    <input type="password" placeholder="Password" id="password" name ="password"> 
    <br>
    <label for ="number">Enter Mobile Number</label><br>
    <input placeholder="03---" id="number" name ="number"><br>
    <label for ="date">Enter Date of Birth</label><br>
    <input type="text" placeholder="DD/MM/YY" id="date" name ="date"><br>
    <label for ="gender">Enter Gender</label><br>
    <input type="text" placeholder="Male/Female/Other" id="gender" name 
    ="gender"><br>
    <br><button style="background-color:orangered;border- 
    color:dodgerblue;color:lightyellow">Signup</button>
    </fielsdet>
    </form>
    </body>
    </html>

my connect.php

    <?php
    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $number = $_POST['number'];
    $date = $_POST['date'];
    $gender = $_POST['gender'];

    // Making Connection with database

    $con = new mysqli('localhost','root','','phpdata');
    if ($con -> connect_error) {
    die('Connection failed :'.$conn -> connect_error);
    }
    else{
    $stmt = $con->query("INSERT INTO signup(firstName, lastName, email, password, 
    number, date, gender)
        values(?,?,?,?,?,?,?)");
    $stmt->bind_param("ssssiss",$firstName, $lastName, $email, $password, 
    $number, $date, $gender);
    $stmt->execute();
    echo "Sign up successful";
    $stmt->close();
    $con->close();
     }?>

5

Answers


  1. Add type in your submit button.

    <button type='submit' style="background-color:orangered;border-color:dodgerblue;color:lightyellow">Signup</button>
    

    and also your question marks and params ara not matching. it should be match. otherwise data won’t store your db

    correct that line also

    Login or Signup to reply.
  2. Use prepare instead of query. All everything is ok.:

    $stmt = $con->prepare("INSERT INTO signup(firstName, lastName, email, password, number, date, gender)
            values(?,?,?,?,?,?,?)");
    

    And make button type as submit:

    <br><button type="submit" style="background-color:orangered;border-color:dodgerblue;color:lightyellow">Signup</button>
    
    Login or Signup to reply.
  3. The main problem is you are not loading code via apache server try to open http://localhost/signup.html instead of C:/xmapp/htdocs/connect.php

    It seems you want to user PDO but your connection string not correct

    <?php
        $firstName = trim($_POST['firstName']);
        $lastName = trim($_POST['lastName']);
        $email = trim($_POST['email']);
        $password = md5(trim($_POST['password']));
        $number = trim($_POST['number']);
        $date = trim($_POST['date']);
        $gender = trim($_POST['gender']);
    
        $con= new PDO("mysql:host=127.0.0.1;dbname=phpdata", 'root', 'root');
        $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sqli = "INSERT INTO signup(firstName, lastName, email, password, number, date, gender)
            values(?,?,?,?,?,?,?)";
    try {
        $stmt= $con->prepare($sqli);
        $stmt->bindParam(1,$firstName);
        $stmt->bindParam(2,$lastName);
        $stmt->bindParam(3,$email);
        $stmt->bindParam(4,$password);
        $stmt->bindParam(5,$number);
        $stmt->bindParam(6,$date);
        $stmt->bindParam(7,$gender);
        $status = $stmt->execute(); 
        echo "Sign up successful";
        $stmt->close();
        $con->close();
    } catch(PDOException $e) {
      echo "Error ".$e->getMessage();
    } 
    ?>
    

    another problem is with your html form button type is missing

    <button type="submit".... />
    
    Login or Signup to reply.
  4. here is the code, it works fine with me

    <?php
    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $number = $_POST['number'];
    $date = $_POST['date'];
    $gender = $_POST['gender'];
    
    // Making Connection with database
    
    $con = new mysqli('localhost','root','','phpdata');
    if ($con -> connect_error) {
    die('Connection failed :'.$conn -> connect_error);
    }
    else{
    
    $stmt = $con->query("INSERT INTO signup(firstName, lastName, email, password, number, date, gender)
        values("'.$firstName.'","'.$lastName.'","'.$email.'","'.$password.'","'.$number.'","'.$date.'","'.$gender.'")");
    if ($con->query($stmt) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
    
    $con->close();
    
    }//end of else of connection
    ?>
    
    Login or Signup to reply.
  5. Here is the complete code after analyzing it for a lot of time. in your $stmt variable there was no query, it was empty. This code works fine just copy and paste it.

    <?php
    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $number = $_POST['number'];
    $date = $_POST['date'];
    $gender = $_POST['gender'];
    
    // Making Connection with database
    
    $con = new mysqli('localhost','root','','abc');
    if ($con -> connect_error) {
    die('Connection failed :'.$conn -> connect_error);
    }
    else{
    
        $sql = "INSERT INTO signup(firstName, lastName, email, password, number, date, gender)
        values('$firstName','$lastName','$email','$password','$number','$date','$gender')";
    
    if ($con->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $con->error;
    }
    
    $con->close();
    
    
    
    }//end of else of connection
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search