-1

I'm trying to make a basic registration form on WAMP, I've tried with tutorials and a book.

I seem to be having a problem after I submit the data on the webpage.

I have created the database and tables and can be seen here... http://oi57.tinypic.com/2j47hux.jpg

Here is the code I have on my html page:

    <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#536977">
<tr>
<form method="post" action="register.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#536977">
<tr>
<td colspan="3"><strong>Register: </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input type="text" id="username"></td>
</tr>

<tr>
<td width="78">First Name</td>
<td width="6">:</td>
<td width="294"><input type="text" id="firstname"></td>
</tr>
<tr>
<td width="78">Last Name</td>
<td width="6">:</td>
<td width="294"><input type="text" id="lastname"></td>
</tr>

<tr>
<td width="78">Age</td>
<td width="6">:</td>
<td width="294"><input type="text" id="age"></td>
</tr>

<tr>
<td>Email</td>
<td>:</td>
<td><input type="text" id="email"></td>
</tr>

<tr>
<td>Password</td>
<td>:</td>
<td><input type="text" id="password"></td>
</tr>


<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" value="Login"></td>
</tr>
</table>
</td
</form>
</tr>
</table>

The php I'm linking to is..

<?php
$con = mysqli_connect("localhost","root","","membersapp");
if (!$con)
  {
  die('Could not connect: ' . mysqli_error($con));
  }

$sql="INSERT INTO memberstable (username, firstname, lastname, age, email, password)
VALUES
('$_POST[username]','$_POST[firstname]','$_POST[lastname]','$_POST[age]','$_POST[email]','$_POST[password]' )";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "$_POST[firstname] $_POST[lastname] has been added";

mysqli_close($con);
?> 

I have ran a small php script to see if php connecting to database and it's connected.

Any ideas what is wrong?

Ok code edited..

    <?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$con = mysqli_connect("localhost","root","","membersapp");
if (!$con)
  {
  die('Could not connect: ' . mysqli_error($con));
  }

$sql="INSERT INTO memberstable (username, firstname, lastname, age, email, password)
VALUES
('$_POST[username]','$_POST[firstname]','$_POST[lastname]','$_POST[age]','$_POST[email]','$_POST[password]' )";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "$_POST[firstname] $_POST[lastname] has been added";

mysqli_close($con);
?> 

and

    <form method="post" action="register.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#536977">
<tr>
<td colspan="3"><strong>Register: </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input type="text" id="username" name="username"></td>
</tr>

<tr>
<td width="78">First Name</td>
<td width="6">:</td>
<td width="294"><input type="text" id="firstname" name="firstname"></td>
</tr>
<tr>
<td width="78">Last Name</td>
<td width="6">:</td>
<td width="294"><input type="text" id="lastname" name="lastname"></td>
</tr>

<tr>
<td width="78">Age</td>
<td width="6">:</td>
<td width="294"><input type="text" id="age" name="age"></td>
</tr>

<tr>
<td>Email</td>
<td>:</td>
<td><input type="text" id="email" name="email"></td>
</tr>

<tr>
<td>Password</td>
<td>:</td>
<td><input type="password" id="password" name="password"></td>
</tr>


<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" value="Login"></td>
</tr>
</table>
</td>
</form>
James_P
  • 41
  • 9
  • Make sure all your columns exist, that they are the correct type, and the lengths are big enough to accomodate the data going in them. Plus, choosing the correct table and database. – Funk Forty Niner Nov 06 '14 at 00:09
  • Also, your column is called `Age` and not `age`. – Funk Forty Niner Nov 06 '14 at 00:10
  • You also have 3 other columns with NULL set to NO. That could also be an issue. – Funk Forty Niner Nov 06 '14 at 00:23
  • Ok, Sorry Fred I'm new on here and to coding so tryign work this site also. Have I done it correct now.. (Just edit the original post and add in any edited code?) Also I fixed Age, and changed Null to YES to checkin case, Still the same. – James_P Nov 06 '14 at 00:35
  • I suggest that you start over with only the columns that you need. I suspect something in your table is acting strangely. You can try to repair it, but that may not fix it. Delete your present table, recreate a new one with only the columns that you need and not do anything special to them. – Funk Forty Niner Nov 06 '14 at 00:39
  • By the way, is there already data in your table? – Funk Forty Niner Nov 06 '14 at 00:41
  • I tested your code and it worked perfectly, this being along with my answer. However, I did not use UNIQUE for any of my columns. If you're trying to enter the same name and/or email, then a UNIQUE constraint won't let you. I don't know why it's not working for you. You could setup a test table with 1 or 2 fields and go from there. – Funk Forty Niner Nov 06 '14 at 01:01
  • I only had one data entered and when I tried it new it would not allow me. Even so I thought I'd at least get an error. yes I Created a new table with just 3 fields.. tried enter it in html and enter data and blank again... I could show you code I'm using and tables used.. I'm still getting blank though. – James_P Nov 06 '14 at 01:53
  • You sure all services are running? I have Wamp on one of my machines and it works fine. – Funk Forty Niner Nov 06 '14 at 01:54
  • Hi, done all code again properly like advised by yourselves. Ran it in a different browser and worked fine. Think there was a plugin causing blank page issue. Thanks for all the help with the code. :) – James_P Nov 06 '14 at 11:16

1 Answers1

5

There are a few things wrong with your code, so please read it to the end of my answer.


All of your inputs have no name attributes. You cannot rely on an id attribute alone.

Change

<input type="text" id="username">

to (minus all of the ^ characters of course) and to match $_POST[username]

<input type="text" id="username" name="username">
                                 ^^^^^^^^^^^^^^^
  • and follow the same convention for the others.

Edit:
Also, your column is called Age and not age, but I doubt that will make a difference. Try changing it to match in letter-case.

However, doing it this way leaves you open to SQL injection. Use prepared statements, or PDO with prepared statements.

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

which would have caught the errors.

Sidenote: Error reporting should only be done in staging, and never production.


Passwords

I also noticed that you may be storing passwords in plain text. This is not recommended.

Use one of the following:

Other links:


Sidenote:

Password inputs should use the password type instead of text

<input type="password" id="password" name="password">

otherwise, passwords will show up in a readable format while being typed in.


Something else is missing

  • You are also missing a > in </td which may affect your table, so change it to </td>
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Hi, thanks, I have sorted the TD problem, and have edited.. the input names. I also added in error reporting, and still get no error, just a blank page. Edited code is in OP. – James_P Nov 06 '14 at 00:37