Twistedwheelbarrow Twistedwheelbarrow - 1 month ago 11
MySQL Question

Registering users through php, but having issues

it's mainly another set of eyes here as I can't visibly see any errors and it isn't posting any. I am attempting to create a user registration form using PHP/SQL for part of an assignment, but when I hit register, it doesn't come up with anything, just a blank page. Before I fixed the connection, it would show the generic "Cannot connect to database".

Here is my HTML and PHP.

PHP

<?php

define('DB_HOST', 'localhost');
define('DB_NAME', 'users');
define('DB_USER', 'twisted');
define('DB_PASSWORD', '********');

$con = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die ("Failed to connect to MySQL: " . mysql_error());
$db = mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " .mysql_error());


function CreateUser() {
$fname = $_POST['fname'];
$sname = $_POST['sname'];
$username = $_POST['uname'];
$email = $_POST['email'];
$password = $_POST['passwd'];

$query = "INSERT INTO users (fname,sname,username,email,passwd) VALUES ('$fname','$sname','$username','$email','$passwd')";
$data = mysql_query ($query)or die(mysql_error());
if($data)
{
echo "Your registration was successful";
}
}

function SignUp() {
if(!empty($_POST['username'])) {
$query = mysql_query("SELECT * FROM users WHERE username = '$_POST[username]' AND pass = '$_POST[passwd]'") or die(mysql_error());

if(!$row = mysql_fetch_array($query) or die (mysql_error()))
{
CreateUser();
} else {
echo "You are already a registered user";
}
}
}

if(isset($_POST['submit'])) {
SignUp();
}

?>


Registration Form

<form id="register-form" action="/includes/connect.php" method="post" role="form" style="display: none;">
<div class="form-group">
<input type="text" name="fname"tabindex="1" class="form-control" placeholder="First Name" value="">
</div>
<div class="form-group">
<input type="text" name="sname""abindex="1" class="form-control" placeholder="Last Name" value="">
</div>
<div class="form-group">
<input type="text" name="uname" tabindex="1" class="form-control" placeholder="Username" value="">
</div>
<div class="form-group">
<input type="text" name="email" tabindex="1" class="form-control" placeholder="Email" value="">
</div>
<div class="form-group">
<input type="password" name="passwd" tabindex="2" class="form-control" placeholder="Password">
</div>
<div class="form-group text-center">
<input type="checkbox" tabindex="3" class="" name="remember" id="remember">
<label for="remember"> Remember Me</label>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<input type="submit" name="submit" id="login-submit" tabindex="4" class="form-control btn btn-login" value="Log In">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<input type="submit" name="register-submit" id="register-submit" tabindex="4" class="form-control btn btn-register" value="Register Now">
</div>
</div>
</div>
</form>


Edit: The database exists, and these are the fields.

https://gyazo.com/d20200fdc5bd3648484f27448aaccc96

Answer

You need to give your submit a name or it won't be available using $_POST['submit']:

<p><input type="submit" value="Submit" name="submit" /></p>