-2

I want to do a simple registration form that stores data in the database but it doesn't seem to be working for me.

This is my html code:

<html>
<body>
    <form method="post" name="register" action="register.php">
    <table>
    <tr>
    <td>User Name:</td>
    <td><input type="text" name="name"></td>
    </tr>
    <tr>
    <td>User Email:</td>
    <td><input type="text" name="email"></td>
    </tr>
    <tr>
    <td>User Role:</td>
    <td><input type="text" name="role"></td>
    </tr>
    <tr>
    <td><input type="submit" name="SubmitForm" value="Save"></td>
    </tr>

</form>
</body>
</html>

And this is my PHP code:

<?php
// Make a MySQL Connection

$user="root";
$password="my password";
$database="playtime";
$host="localhost";
$table="ttuser";

mysql_connect($host, $user, $password) or die("Connection Failed");
mysql_select_db($database) or die("Connection Failed");

$name = $_POST['name'];
$email = $_POST['email'];
$role = $_POST['role'];

// Insert a row of information into the table "ttuser"

$query = "INSERT INTO ttuser (uemail, uname, urole) VALUES('$email', '$name', '$role')";
if(mysql_query($query)){
echo "inserted";}
else{
echo "fail";}

?>

it connects to the database fine, but when I click submit on the html page it goes to php but doesn't show anything. Can someone please help ?

17
  • 6
    SO, when people will stop copying sql injections from tutorials?
    – Peter
    Jan 16, 2013 at 22:27
  • Have you turned on error reporting? Jan 16, 2013 at 22:27
  • 3
    @PeterSzymkowski I'm sorry that an absolute new starter cannot write PHP code without copying bits from tutorials
    – elmify
    Jan 16, 2013 at 22:37
  • 2
    You are using an obsolete database API and should use a modern replacement. You are also vulnerable to SQL injection attacks that a modern API would make it easier to defend yourself from.
    – Quentin
    Jan 16, 2013 at 22:41
  • 1
    Try putting an or die(mysql_error()); at the end of the mysql_query call and see what it happens. I typically put that on every mysql_* function that I use when I first start debugging to help me find problems really quick. I'd always do something like this: $run = mysql_query($query) or die('Query failed. ' . mysql_error());
    – SISYN
    Jan 17, 2013 at 0:45

2 Answers 2

1

If the table ttuser has any fields that do not have default values and you are not providing any data to those fields (e.g., there is a field called "usalary" with no default value that was not included in your INSERT INTO statement), the MySQL statement will fail.

Heed the many sql injection warnings :)

1
  • thanks for your answer but the only fields I have in my table are: uname, uemail and urole so I don't think I have an issue with the INSERT INTO statement
    – elmify
    Jan 17, 2013 at 13:18
0

OK guys it was all my fault because I was not viewing the page via the server. thanks for all your help, it was a silly mistake

Not the answer you're looking for? Browse other questions tagged or ask your own question.