0

I am trying to insert my registration form data into my table but for some reason nothing is being inserted and I am not getting an error.

could someone please show me where I am going wrong with this as I am really new to php and MySQL, thanks in advance.

my config.php file holds my connection:

<?php
$host="localhost";
$username="mark";
$password="password";
$db_name="hewden1";
$conn = mysql_connect("$host", "$username", "$password") or die("Could Not Connect to Server");
$db = mysql_select_db("$db_name")or die("Cannot Connect the Database"); 
?>

Here's my registration form html:

<form name="test" action="validation/signup_process.php" method="post">
<input type="text" name="compname" class="login_form" placeholder="Company or Trading Name">
<br>
<input type="text" name="contactname" class="login_form" placeholder="Contact Name"><br><br><br>
<input type="text" name="emailaddress" class="login_form" placeholder="Email Address">
<br>
 <input type="text" name="password1" class="login_form" placeholder="Password">
 <input type="text" name="password2" class="login_form" placeholder="Confirm Password"><br>

 <input type="submit" name="submit" value="Register" class="buttons">
</form> 

my php/MySQL code:

<?php 
session_start();
include("config.php");
//retrieve our data from POST
$compname = $_POST['compname'];
$contactname = $_POST['contactname'];
$username = $_POST['emailaddress'];
$password1 = $_POST['password1'];
$password2 = $_POST['password2'];

if($password1 != $password2) {
$_SESSION['message2'] = '<div id="message_box2"><div class="boxclose" id="boxclose" onclick="this.parentNode.parentNode.removeChild(this.parentNode);"></div><h23>Ooops!</h23><p>The Password&#39;s did not match.</p> </div>';
header("location:..\sign-up.php");

}else{
if(strlen($username) > 30) {
$_SESSION['message2'] = '<div id="message_box2"><div class="boxclose" id="boxclose" onclick="this.parentNode.parentNode.removeChild(this.parentNode);"></div><h23>Ooops!</h23><p>The Username you have selected is incorrect.</p> </div>';
header("location:..\sign-up.php");

}else{

$hash = hash('sha256', $password1);

function createSalt(){
$text = md5(uniqid(rand(), true));
return substr($text, 0, 3); }
$salt = createSalt();
$password = hash('sha256', $salt . $hash);

$username = mysql_real_escape_string($username);
$query = "INSERT INTO supplier_pre_sign (contactname, company_name, supplier_email, password, date, user_type) VALUES ('$contactname','$compname','$username', '$salt', now(), 'visitor');" or die(mysql_error());

echo "eveything ok";

} }?>
  • You're creating a variable with your query in it but never actually executing it. –  Dec 18 '14 at 09:53

5 Answers5

0

Use this

$host="localhost";
$username="mark";
$password="password";
$db_name="hewden1";
$conn = mysql_connect($host, $username, $password) or die("Could Not Connect to Server");
$db = mysql_select_db($db_name)or die("Cannot Connect the Database"); 

//Insert query

$query = "INSERT INTO supplier_pre_sign (contactname, company_name, supplier_email, password, date, user_type) VALUES ('$contactname','$compname','$username', '$salt', now(), 'visitor');" or die(mysql_error());
$res = mysql_query($query);
I'm Geeker
  • 4,601
  • 5
  • 22
  • 41
  • this didn't seem to make any difference, still nothing being inserted into my table – John Cleeves Dec 18 '14 at 10:07
  • @John Cleeves Try: if($res){echo "ok";} else{ mysql_error($res) } <---this maybe show the error log. Edit: uhm...you have two else{}......use if{}elseif{}else{} – Joci93 Dec 18 '14 at 10:15
  • @Joci93 here is my error --- > Warning: mysql_error() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\hewden\ssa\suppliers\validation\signup_process.php on line 36 eveything ok – John Cleeves Dec 18 '14 at 10:29
0

You forgot to execute your query. You need to execute the query in order to insert in to your database.

Try this :

    <?php 
    session_start();
    include("config.php");
    //retrieve our data from POST
    $compname = $_POST['compname'];
    $contactname = $_POST['contactname'];
    $username = $_POST['emailaddress'];
    $password1 = $_POST['password1'];
    $password2 = $_POST['password2'];

    if($password1 != $password2) {
    $_SESSION['message2'] = '<div id="message_box2"><div class="boxclose" id="boxclose" onclick="this.parentNode.parentNode.removeChild(this.parentNode);"></div><h23>Ooops!</h23><p>The Password&#39;s did not match.</p> </div>';
    header("location:..\sign-up.php");

    }else{
    if(strlen($username) > 30) {
    $_SESSION['message2'] = '<div id="message_box2"><div class="boxclose" id="boxclose" onclick="this.parentNode.parentNode.removeChild(this.parentNode);"></div><h23>Ooops!</h23><p>The Username you have selected is incorrect.</p> </div>';
    header("location:..\sign-up.php");

    }else{

    $hash = hash('sha256', $password1);

    function createSalt(){
    $text = md5(uniqid(rand(), true));
    return substr($text, 0, 3); }
    $salt = createSalt();
    $password = hash('sha256', $salt . $hash);

    $username = mysql_real_escape_string($username);
    $query = "INSERT INTO supplier_pre_sign (contactname, company_name, supplier_email, password, date, user_type) VALUES ('$contactname','$compname','$username', '$salt', now(), 'visitor')";
    mysql_query($query); //You forgot to add this line
    echo "eveything ok";

    } }?>
Rohil_PHPBeginner
  • 6,002
  • 2
  • 21
  • 32
0

Your insert query is in a function and I don't see it being called.
Then, your $query is also not being executed.
Add mysql_query($query); after the line where you create your query.

kRiZ
  • 2,320
  • 4
  • 28
  • 39
0

Hey Only Change the query like this

$query = "INSERT INTO supplier_pre_sign (contactname, company_name, supplier_email, password, date, user_type) VALUES ($contactname,$compname,$username, $salt, now(), 'visitor');" or die(mysql_error());

i think it will work fine

And checkout something similar answer

Inserting $variable or $_POST value into mysql table

Community
  • 1
  • 1
Amit Mishra
  • 291
  • 3
  • 12
0

Try this:

<?php 
session_start();
include("config.php");
//retrieve our data from POST
$compname = $_POST['compname'];
$contactname = $_POST['contactname'];
$username = $_POST['emailaddress'];
$password1 = $_POST['password1'];
$password2 = $_POST['password2'];

if($password1 != $password2) {
$_SESSION['message2'] = '<div id="message_box2"><div class="boxclose" id="boxclose" onclick="this.parentNode.parentNode.removeChild(this.parentNode);"></div><h23>Ooops!</h23><p>The Password&#39;s did not match.</p> </div>';
header("location:..\sign-up.php");

}elseif($username != "something") {  //Write a code in the (). 
if(strlen($username) > 30) {
$_SESSION['message2'] = '<div id="message_box2"><div class="boxclose" id="boxclose" onclick="this.parentNode.parentNode.removeChild(this.parentNode);"></div><h23>Ooops!</h23><p>The Username you have selected is incorrect.</p> </div>';
header("location:..\sign-up.php");

}else{

$hash = hash('sha256', $password1);

function createSalt(){
$text = md5(uniqid(rand(), true));
return substr($text, 0, 3); }
$salt = createSalt();
$password = hash('sha256', $salt . $hash);

$username = mysql_real_escape_string($username);
$query = "INSERT INTO supplier_pre_sign (contactname, company_name, supplier_email, password, date, user_type) VALUES ($contactname,$compname,$username, $salt, now(), 'visitor');" or die(mysql_error());

$res = mysql_query($query);

if($res){echo "inserted";} else{ mysql_error($res) }

    echo "eveything ok";

} }?>
Joci93
  • 803
  • 3
  • 10
  • 24