4

Im trying to POST data entered by a user into my databse however my if statement to make sure this only happens once the submit button has been pressed is not working. Please help!

code below..

<form>
    <form method="post" action="register.php" >
  <table align="center">
    <tr>
      <td align="right">First Name:</td>
      <td align="left"><input type="text" name="FirstName"></td>
    </tr>
    <tr>
      <td align="right">Last Name:</td>
      <td align="left"><input type="text" name="LastName"></td>
    </tr>
    <tr>
      <td align="right">Email:</td>
      <td align="left"><input type="text" name="Email" /></td>
    </tr>
    <tr>
      <td align="right">Password:</td>
      <td align="left"><input type="password" name="Password"></td>
    </tr>
    <tr>
      <td align="right">Gender:</td>
      <td align="left"><input type="text" name="Gender"></td>
    </tr>
    <tr>
      <td align="right">Age:</td>
      <td align="left"><input type="text" name="Age"></td>
    </tr>
  </table>
<input type="submit" value="Submit" name="Submit">
</form>

<?php include ("registersql.php"); ?>

my regstersql.php

<?php
//include connection
 include ("connection.php");
//has form been submitted?
if(isset($_POST['Submit'])){
    $FirstName=$_POST['FirstName'];
    $LastName=$_POST['LastName'];
    $Email=$_POST['Email'];
    $Password=$_POST['Password'];
    $Gender=$_POST['Gender'];
    $Age=$_POST['Age'];
//Insert data
$query = "INSERT INTO Customers
(CustomerID, FirstName, LastName, Email, Password, Gender,Age)
VALUES
('$FirstName','$LastName','$Email','$Password','$Gender','$Age')";
mysqli_query($connection,$query);
} else
{
    echo "Error";
}

?>
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
TheKaiser4
  • 149
  • 1
  • 1
  • 11
  • 1
    More info and debugging needed. Also, `regstersql.php != regster.php` – AbraCadaver Jan 07 '15 at 18:18
  • 3
    not working **HOW**? Other than the gaping wide-open [sql injection attack](http://bobby-tables.com) vulnerablity, th eonly thing "wrong" with this code is that you'd spit out "Error" even if the form was never submitted in the first place. – Marc B Jan 07 '15 at 18:18
  • 2
    6 columns, 5 values. Do the math ;-) – Funk Forty Niner Jan 07 '15 at 18:19

5 Answers5

3

isset() tells you whether the variable has been defined, not whether it's been populated.

Try something like this:

if(isset($_POST['submit']) && !empty($_POST['submit'])) {
    // there is something in the field, do stuff
} else {
    // trigger error
}
chakeda
  • 1,551
  • 1
  • 18
  • 40
  • Also, as @AbraCadaver says, your form action (register.php) is not the same as your form processing file (regstersql.php). – chakeda Jan 07 '15 at 18:30
  • 1
    you need to add ) if(isset($_POST['submit']) && !empty($_POST['submit'])) { // there is something in the field, do stuff } else { // trigger error } – Peternak Kode Channel Aug 06 '16 at 00:40
  • Suppose I have a sign up form on my website that has field with `name="username"`. Would it be appropriate to use `if(!isset($_POST['username']))`, since there's no way that the user tried accessing the form processing PHP directly and the `username` field got some arbitrary value ?? – progyammer Aug 15 '17 at 17:11
  • @progyammer : I assume it would be appropriate but I can't tell for sure without seeing your code. Perhaps you should make another question. – chakeda Aug 15 '17 at 19:50
3

You need to change from

<form method="post" action="register.php" >

to

<form method="post" action="" >  //Sends data on same page. There is no register.php exist.
NeiL
  • 791
  • 8
  • 35
0

You did two things incorrectly :

1) you used tag twice:

<form method="post" action="register.php" >

with one closing tag

2) don't specify the file location you can use require or require_once ('yourfile.php') and into this file you can handle your POST submission with if(isset($_POST['submit'])) then have what you want.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

You have used twice the form Tag

Remove top one and form will work.

I know this is a very old post but i guess it may help some.

-3

Problem in customerid value passing.

/Insert data
$query = "INSERT INTO Customers
(CustomerID, FirstName, LastName, Email, Password, Gender,Age)
VALUES
('$FirstName','$LastName','$Email','$Password','$Gender','$Age')";
mysqli_query($connection,$query);
} else
{
    echo "Error";
}

?>

see above code if customerid auto mention empty ' ', but your not mentioned anything so it will not insert, if you mention user side success message will display but not insert data.

Use like:

values('','$FirstName','$LastName','$Email','$Password','$Gender','$Age')";
Pang
  • 9,564
  • 146
  • 81
  • 122