1

I have my sign up forms in html all setup and working, it runs and inserts a row into mysql table the first time, but I can't insert any more rows.

My php is:-

include "config.php";

$gamerid = $_POST['gamerid_create'];
$email = $_POST['email_create'];
$password = md5($_POST['password_create']);
$country = $_POST ['country_create'];

echo "<pre>";
print_r($_POST);
echo "</pre>";

$insert = "insert into users (gamerid,email,password,country) values('$gamerid','$email','$password','$country')";
mysql_query($insert);

Now when I created my table in mysql I used the following code:-

CREATE TABLE users
(
id INT primary key NOT NULL AUTO_INCREMENT,
gamerid varchar (255) NOT NULL,
email varchar (255) NOT NULL,
password varchar (255) NOT NULL,
country varchar (255) NOT NULL
)

Why aren't more rows inserted?

My html is as follows:

<form id = "signup_form" method = "POST" action = "http://localhost:8080/quizicle/pages/register.php">
<table id = "signup_form_table" border = "0" class = "signup_table_body">
<tr class = "signup_form_tr_label">
<td class = "signup_form_td"><label for "gamerid" class = "signup_form_label">Create a Gamer ID:</label></td>
<td class = "signup_form_td"><label for "email" class = "signup_form_label">Your Email:</label></td>
<td class = "signup_form_td"><label for "password" class = "signup_form_label">Password:</label></td>
<td class = "signup_form_td"><label for "country" class = "signup_form_label">Country:</label></td>
<td class = "signup_form_td"></td>
</tr>

<tr class = "signup_form_tr">
<td class = "signup_form_td"><input type = "text" name = "gamerid_create" id = "gamerid_create" maxlength="50" class = "signup_form_input"/></td>
<td class = "signup_form_td"><input type = "text" name = "email_create" id = "email_create" maxlength="50" class = "signup_form_input"/></td>
<td class = "signup_form_td"><input type = "password" name = "password_create" id = "password_create" maxlength="50" class = "signup_form_input"/></td>
<td class = "signup_form_td">
<select name = "country_create">                                    <option>Please select a country</option> 
<option>United Kingdom</option> 
</select>
</td>
<td class = "signup_form_td"><input type = "submit" value = "Sign me up!" id = "btn_register" value = "register" class = "signup_form_submit_btn"/></td>
</tr>   
</table>
</form>
5
  • my insert statement didn't copy properly, it should be: $insert = "insert into users(gamerid,email,password,country) values('$gamerid','$email','$password','$country')"; Oct 19, 2012 at 18:22
  • Could you provide your html/javascript ?
    – Luc Morin
    Oct 19, 2012 at 18:23
  • 1
    First of all you are VERY VULNERABLE to SQL injection. Can you insert another record manually into the database?
    – wesside
    Oct 19, 2012 at 18:23
  • 1
    Please, don't use mysql_* functions for new code. They are no longer maintained and the community has begun the deprecation process. See the red box? Instead you should learn about prepared statements and use either PDO or MySQLi. If you can't decide, this article will help to choose. If you care to learn, here is good PDO tutorial.
    – John Conde
    Oct 19, 2012 at 18:25
  • @GBD He has not specified so it must be default 11
    – Mr. Alien
    Oct 19, 2012 at 18:33

2 Answers 2

0
    <?php

include "config.php";

// INPUT CLEANING FUNCTION
function clean($str)
{
    $cleaned = mysql_real_escape_string(strip_tags($str));
    return $cleaned;
}

$gamerid = clean($_POST['gamerid_create']);
$email = clean($_POST['email_create']);
$password = clean(md5($_POST['password_create']));
$country = clean($_POST ['country_create']);


$insert = "insert into users(gamerid,email,password,country) values('$gamerid' ,'$email',' $password', '$country')";                        
mysql_query($insert);
?>
  1. Check your id field if its autoincrement
  2. Check name of attributes of you input tags or textarea tags // Optional Suggestions
  3. Valid the email using filter_var function which is built in with php
  4. Validate password length and strength
  5. Also consider some naming conventions
  6. Use a framework CODEIGNITER or LARAVEL
1
  • Thanks Johndave, it's working now after pasting your code haha Oct 19, 2012 at 18:41
-1

It's probably in regards to your primary key id

Let's change up the sql and see if it works.

$insert = "INSERT INTO users (id, gamerid, email, password, country) 
  VALUES (NULL, '{$_POST['gamerid_create']}', '{$_POST['email_create']}', 
  '{$_POST['password_create']}', '{$_POST['country_create']}');";

Some things you should consider as you develop your skills with PHP / MySQL:

  • Validate and scrub your user input. Sql injection risks. See: mysql_real_escape_string(), for basic help here.

  • You should configure your tables make sure you set the field type and size to the minimum that you need, VARCHAR(255) will waste alot of space if you dont fill it all.

  • Always hash passwords with at least the md5 algorithm.

6
  • his primary key is set to auto increment
    – Mr. Alien
    Oct 19, 2012 at 18:32
  • No; not MD5. MD5 "should be considered cryptographically broken and unsuitable for further use" -- kb.cert.org/vuls/id/836068
    – Michael
    Oct 19, 2012 at 18:33
  • I would not advise the OP to continue using mysql_ functions.
    – Kermit
    Oct 19, 2012 at 18:33
  • You guys are silly. The code is sound and I was making improvement suggestions to his current questions without escaping the bounds of his question. It's clear the op does not use PDO, Doctrine or other Entities, why reinvent the wheel on him? Oct 19, 2012 at 18:38
  • Silly? mysql_ functions are being deprecated. Why continue to use it?
    – Kermit
    Oct 19, 2012 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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