2

I'm trying to write a registration form, firstly I tried to do it OO, but this didn't respond, it seemed to just clear the form and refresh the page, but didn't insert any data:

EDIT: Current code:

<?php
session_start();
include 'registrationform.php';
include 'connection.php';

if (isset($_POST['regsubmit'])) 
    {
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$user = $_POST['username'];    
$pass = $_POST['password'];
$query = "INSERT INTO users (firstname, lastname, username, password) VALUES(?, ?, ?, ?)";
$statement = $connection->prepare($query);
//bind parameters for markers, where (s = string, i = integer, d = double,  b = blob)
$statement->bind_param('ssss', $firstname, $lastname, $username, $password);

if($statement->execute()){
     print 'Success! ID of last inserted record is : ' .$statement->insert_id .'<br />'; 
     //$_SESSION['username'] = $_POST['username'];
}else{
     die('Error : ('. $mysqli->errno .') '. $mysqli->error);
}
$statement->close();
?>

Registration Form

<!DOCTYPE html>
<?php
include 'header.php';
?>
<center>

<html>
<link rel="stylesheet" type="text/css" href="web.css" />


</font>
    <head>


    </head>
    <body> 

    <div id="registrationform">

        Please enter your registration details<br /><br />

        <form method="post" action="registrationsubmit.php">
            First Name:
            <input type="text" name="firstname" />
            <br /><br>
            Last Name:
            <input type="text" name="lastname" />
            <br /><br>
            Username:
            <input type="text" name="username" />
            <br /><br>
            Password:
            <input type="text" name="password" />
            <br /><br>
            <input type="submit" name="regsubmit" value="Submit" />
        </form>

</div>



        </body>
</html>

</center>

OO Attempt

<?php
session_start();
include 'registrationform.php';
include 'connection.php';

if (isset($_POST['regsubmit'])) 
    {
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$user = $_POST['username'];    
$pass = $_POST['password'];
$stmt = $connection->prepare("INSERT INTO users VALUES (?, ?, ?, ?)");
$stmt->bind_param('ssss', $firstname, $lastname, $user, $pass);
$stmt->execute();
printf("%d Row inserted.\n", $stmt->affected_rows);  
$stmt->close();
}
?>

As this wasn't working I tried just doing it without OO, for a starting point. But now I'm given the error "Access denied for user ''@'10.246.64.24' (using password: NO)", yet my connection connects fine and I've also written a login that works perfectly so can't figure it out. Here is the current code that I have:

registrationsubmit.php

<?php
include "connection.php";
include "header.php";
if(isset($_POST['regsubmit']))
{
 mysql_select_db("c3438525_co_uk",$connection);
 $firstname = $_POST['$firstname'];
 $lastname = $_POST['$lastname'];
 $username = $_POST['username']; 
 $password = $_POST['password'];
 $query = "INSERT INTO users (FirstName, LastName, Username, Password) VALUES ('$firstname', '$lastname', '$username', '$password')"; 
 $data = mysql_query ($query)or die(mysql_error()); 
 if($data) { echo "Successfully Registered"; }

 else
 {
  ?>
        <script>alert('error while registering you...');</script>
        <?php
 }
}
?>

Connection.php

 <?php
ob_start();
$connection = mysqli_connect("***", "***", "BFUWGpn3", "***"); 
?>
user2326995
  • 163
  • 1
  • 11
  • @MarcB how is this a duplicate of [Can I mix MySQL API's in PHP?](http://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php)? – Small Legend Oct 22 '15 at 16:01
  • 1
    @SmallLegend: connection.php: `mysqli_connect()`. registration_submit.php: `mysql_query()`. mysql_*() will attempt to auto-connect with default username/pw upon first usage, and is obviously failing. – Marc B Oct 22 '15 at 16:03
  • @MarcB Yeah I understand that, but that isn't his question – Small Legend Oct 22 '15 at 16:04
  • yes, and? exactly how can it insert anything if OP's using the **WRONG** mysql function for the insert query? `mysql_query('INSERT ...')` is faling because OP used `mysqli` to connect in the first place. – Marc B Oct 22 '15 at 16:05
  • @MarcB so surely it shouldn't be marked as a duplicate, but you should refer him to this question? I'm not looking to argue, I'm still learning the ropes with SO – Small Legend Oct 22 '15 at 16:08
  • 1
    OP's problem is entirely due to mixing the libraries, so the question is a dupe. – Marc B Oct 22 '15 at 16:13
  • Thankyou @MarcB - so should I change my connection.php to connect through mysql instead of mysqli? – user2326995 Oct 22 '15 at 16:16
  • @user2326995 no, use mysqli_, mysql_ is deprecated – Small Legend Oct 22 '15 at 16:33
  • mysql is dead in the water, and will be removed from a future version of php. Use PDO or MySQLi only. – Marc B Oct 22 '15 at 16:46

1 Answers1

2

You're using a mysqli_ connection and your query is mysql_

For the registrationsubmit.php

Use something on the lines:

$query = "INSERT INTO users (firstname, lastname, username, password) VALUES(?, ?, ?, ?)";
$statement = $connection->prepare($query);
//bind parameters for markers, where (s = string, i = integer, d = double,  b = blob)
$statement->bind_param('ssss', $firstname, $lastname, $username, $password);

if($statement->execute()){
     print 'Success! ID of last inserted record is : ' .$statement->insert_id .'<br />'; 
     //$_SESSION['username'] = $_POST['username'];
}else{
     die('Error : ('. $mysqli->errno .') '. $mysqli->error);
}
$statement->close();

Refer to : http://php.net/manual/en/mysqli.insert-id.php

Small Legend
  • 733
  • 1
  • 6
  • 20