Php POST is returning an empty array

I have designed this page for form submission but the php POST is returning an empty array, kindly suggest alternatives, thankyou :slight_smile:

This is my file connect.php

{
$name = ($_POST['name']);
$id = ($_POST['username']);
$email = ($_POST['email']);
$mobile = ($_POST['contact']);
$pass = ($_POST['password']);*/

    if (isset($_POST['username'])) {
        $name = $_POST['username'];
    }
   // if (isset($_POST['userid'])) {
       // $username = $_POST['userid'];
   // }
    if (isset($_POST['useremail'])) {
        $email = $_POST['useremail'];
    }
    if (isset($_POST['usercontact'])) {
        $contact = $_POST['usercontact'];
    }
    if (isset($_POST['userpass'])) {
        $pass = $_POST['userpass'];
    }

    print_r($_POST);
    $sql = "INSERT INTO user_detail(user_name, user_email, user_phone, user_pwd) VALUES ('$name', '$email', '$contact', '$pass')";
    $exec = mysqli_query($conn, $sql);

    if (!$exec) {
        die('Could not enter data: ' . mysqli_error($conn));
    }

    echo "Entered data successfully\n";

    mysqli_close($conn);

    ?>

and this is the page userlogin.php

<!DOCTYPE html>
<html >
  <head>
    <meta charset="UTF-8">
      <link rel="shortcut icon" href="MediSeen.ico">
    <title>Sign-Up/Login Form</title>
    <link href='http://fonts.googleapis.com/css?family=Titillium+Web:400,300,600' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" href="css/normalize.css">
      <link rel="stylesheet" href="css/style.css">

  </head>
   <body>

    <div class="form">

      <ul class="tab-group">
        <li class="tab active"><a href="#signup">Sign Up</a></li>
        <li class="tab"><a href="#login">Log In</a></li>
      </ul>
      <div class="tab-content">
        <div id="signup">
          <h1>Sign Up</h1>

          <form action="connect.php" method="POST">

            <div class="top-row">
              <div class="field-wrap">
                <input name="username" type="text" placeholder="Name*" required />
              </div>

              <div class="field-wrap">
                <input name="userid" type="text" placeholder="UserName*" required />
              </div>
            </div>

            <div class="field-wrap">
              <input name="useremail" type="email" placeholder="Email ID*" required />
            </div>
            <div class="field-wrap">
              <input name="usercontact" type="text" maxlength="10" placeholder="Contact*" required />
            </div>
            <div class="field-wrap">
              <input name="userpass" type="password" placeholder="Password*" required />
            </div>

            <div class="field-wrap">
              <input type="password" placeholder="Confirm Password*" required />
            </div>
            
            <button name="submit" type="submit" class="button button-block">Get Started!</button>


          </form>

        </div>



        <div id="login">
          <h1>Welcome Back!</h1>

          <form action="/" method="post">

            <div class="field-wrap">
            <label>
              Email Address<span class="req">*</span>
            </label>
            <input name="mail" type="email" required autocomplete="off"/>
          </div>

          <div class="field-wrap">
            <label>
              Password<span class="req">*</span>
            </label>
            <input name="pass" type="password" required autocomplete="off"/>
          </div>

          <p class="forgot"><a href="#">Forgot Password?</a></p>

          <button class="button button-block">Log In</button>

          </form>

        </div>

      </div><!-- tab-content -->

</div> <!-- /form -->
    <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>

        <script src="js/index.js"></script>




  </body>
</html>

This is the error msg
Connected to MySQL
Connected to mediseenArray ( ) Could not enter data: Duplicate entry ā€˜ā€™ for key ā€˜user_emailā€™

because email is UNIQUE and it already has a null value saved in databaseā€¦

It may be a bit off-topic, but why are you setting

$id = ($_POST['username']);

before checking whether $_POST['username'] is set?

He is setting it twice; once at the top and then using an if later. Also there is no user input validation.

1 Like

@gandalf458
I am sorry for the mistake, that section is in ā€œcommentsā€
and the section above that code isā€¦

<?php
$usrname = 'root';
$password = '';
$hostname = 'localhost:3306';
$name = "";
$username = "";
$email = "";
$contact = "";
$pass = "";
//connection to the database
$conn = mysqli_connect($hostname, $usrname, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";

//select a database to work with
$selected = mysqli_select_db($conn, "mediseen")
or die("Could not select mediseen");
echo("Connected to mediseen");

I dont know how this code got removed.
mediseen is the name of my database.

@Rubble
She*
and no i am not setting it twiceā€¦
I thought of having user input validation only after I get the correct values in the table atleast.

Sorry

I would go back to basics and just have one simple from to a printr() only. When that is working expand on it.

Chicken and Egg.
To ensure that the email input is not null (if it is a required field which the form says it is), you need to validate it!
The lack of validation is causing the error.

Oh Okay, Thankyou so much :slight_smile:

I removed the required attribute to check. Itā€™s still using null values.

The required attribute will not change anything in the processing script, that just tells me you intend email to be mandatory.
If email is optional, you still need to check if it was filled in and only insert it if it has been filled.

At the top of the code it says

$email = ($_POST['email']);

but the input field is called ā€œuseremailā€. But later it checks whether thereā€™s anything in it and accesses the correct field, so thatā€™s probably not relevant.

that is the comment section sirā€¦

Youā€™re a sitting duck for SQL injection attacks, when sending any data to a database you should ALWAYS use prepared statements. ALL user submitted data should ALWAYS be validated (no matter how well you trust the user) as you donā€™t wonā€™t to be saving any garbage to the database

1 Like

Any code like that is called a massive security hole. The only reason for copying the value is if you have validated or sanitized it first. If not then you are better off referencing the $_POST field that you know hasnā€™t been validated rather than copying it to another field name so as to make it impossible to tell whether any field contains a valid value or not.

That aside, still interesting that the $_POST array is empty when the user submits the form. I can only assume itā€™s something to do with a button tag instead of an input type=submit - just because I personally havenā€™t used the former. Unless the javascript at the bottom is affecting something.

Could be.

Iā€™m thinking itā€™s something before the

{
$name = ($_POST['name']);
$id = ($_POST['username']);
$email = ($_POST['email']);
$mobile = ($_POST['contact']);
$pass = ($_POST['password']);*/

And Iā€™m curious as to why the variables are in parentheses

@knk,

Try to debug it like this. just comment the rest of your codes.

connect.php

if(isset($_POST['sumbit']) && $_POST['submit'] === 'submit')){
   print_r($_POST);
}else{
  echo "no data submitted";

}

Itā€™s not clear, but that section is commented out. OP mentioned it in #4, but would have been clearer to not list it.

1 Like

A bit of a red herring posting mysterious code that is commented out, especially when itā€™s not clear it is commented.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.