radecut — 2013-03-24T10:50:09-04:00 — #1
So I started an PHP login system, and the login works, but when I try to make a registration form, it just won't work. I have a database table named members, and it has three columns, named id, username, and password. The HTML is this.
<form name="form2" method="POST" action="checkregister.php" id="registerform">
<input type="text" name="newusername" id="newusername" class="textinput" /><br/>
<input type="text" name="newpassword" id="newpassword" class="textinput" /><br/>
<input type="submit" name="Submit" value="Register" />
And then my PHP script goes like this. My database info is also correct.
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Define $newusername and $newpassword
mysql_query("INSERT INTO members(id, username, password) VALUES(' ', $newusername', '$newpassword')");
Print "Registration Completed";
Any help would be appreciated, thank you!
spikez — 2013-03-24T11:35:21-04:00 — #2
hi, you are missing a ' before $newusername in your query string.
Echo out a mysql error message to give you a clue as to what is happening.
mysql_query("INSERT INTO members(id, username, password) VALUES(' ', $newusername', '$newpassword')") or die(mysql_error());
Also as a piece of advice, please look at SQL Injection as your query is using USER SUBMITTED input directly into your database query.
This could easily lead you into difficulties in the future as a hacker would have direct access to manipulate your database.
Specifically look at mysql_real_escape_string().
If youare just starting out with mysql, look at using PDO instead: http://php.net/pdo
radecut — 2013-03-24T12:04:37-04:00 — #3
Ah, thank you so much! I am just starting out with MySQL, so thanks for the tips, it works now.