1

my problem is that i have two codes one for guest table and the other is for employee table....they are literally the same but with changes on the name of columns....the guest code works like charm...but the employee code wont insert the row at all...it doesnt show me errors it prints out the TRUE message.

here is the employee code:

if ($emp = $con->prepare('SELECT emp_id, password FROM employee WHERE emp_name = ?')) {
    // Bind parameters (s = string, i = int, b = blob, etc), hash the password using the PHP password_hash function.
    $emp->bind_param('s', $_POST['emp_name']);
    $emp->execute();
    $emp->store_result();
    // Store the result so we can check if the account exists in the database.
    if ($emp->num_rows > 0) {
        // Useremp_name already exists
                    echo "<script>viewmessagebox('emp_name exists please choose another ....','empactivate.php')</script>";

    } else {
        // Insert new account
        // Useremp_name doesnt exists, insert new account

    
if ($emp = $con->prepare('INSERT INTO employee (gender, emp_type, designation, status ,emp_name, password, login_id, activation_code) VALUES (?, ?, ?, ?, ?, ?, ?, ?)')) {
    // We do not want to expose passwords in our database, so hash the password and use password_verify when a user logs in.
    $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
    $uniqid = uniqid();
    $stat = 'Active';

$emp->bind_param('ssssss', $_POST['gender'], $_POST['emp_type'], $_POST['designation'], $stat, $_POST['emp_name'], $password, $_POST['login_id'], $uniqid);

    $emp->execute();
$from    = 'noreply@yourdomain.com';
$subject = 'Account Activation Required';
$headers = 'From: ' . $from . "\r\n" . 'Reply-To: ' . $from . "\r\n" . 'X-Mailer: PHP/' . phpversion() . "\r\n" . 'MIME-Version: 1.0' . "\r\n" . 'Content-Type: text/html; charset=UTF-8' . "\r\n";
// Update the activation variable below
$hashed = 'you password' . $_POST['password'];
$activate_link = 'http://localhost/eHostel%20Source%20code/empactivate.php?login_id=' . $_POST['login_id'] . '&code=' . $uniqid;
$message = '<p>Please click the following link to activate your account: <a href="' . $activate_link . '">' . $activate_link . '</a>  <a href="' . $password . '">' . $hashed . '</a>   </p>';
mail($_POST['login_id'], $subject, $message, $headers);

echo "<script>viewmessagebox('Please check your login_id to activate your account!....','index.php')</script>";
}
 else {
    // Something is wrong with the sql statement, check to make sure accounts table exists with all 3 fields.
            echo "<script>viewmessagebox('UNKOWN ERROR TRY AGAIN ....','empactivate.php')</script>";

}
    }
    $emp->close();
} else {
    // Something is wrong with the sql statement, check to make sure accounts table exists with all 3 fields.
            echo "<script>viewmessagebox('UNKOWN ERROR TRY AGAIN ....','empactivate.php')</script>";

}
$con->close();

}
}

and here is the guest code:

if ($stmt = $con->prepare('SELECT guestid, password FROM guest WHERE name = ?')) {
    // Bind parameters (s = string, i = int, b = blob, etc), hash the password using the PHP password_hash function.
    $stmt->bind_param('s', $_POST['name']);
    $stmt->execute();
    $stmt->store_result();
    // Store the result so we can check if the account exists in the database.
    if ($stmt->num_rows > 0) {
        // Username already exists
                    echo "<script>viewmessagebox('name exists please choose another ....','guest - Copy.php')</script>";

    } else {
        // Insert new account
        // Username doesnt exists, insert new account

    
if ($stmt = $con->prepare('INSERT INTO guest (contactno, status ,name, password, emailid, activation_code) VALUES (?, ?, ?, ?, ?, ?)')) {
    // We do not want to expose passwords in our database, so hash the password and use password_verify when a user logs in.
    $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
    $uniqid = uniqid();
    $stat = 'Active';

$stmt->bind_param('ssssss', $_POST['contactno'], $stat, $_POST['name'], $password, $_POST['emailid'], $uniqid);

    $stmt->execute();
$from    = 'noreply@yourdomain.com';
$subject = 'Account Activation Required';
$headers = 'From: ' . $from . "\r\n" . 'Reply-To: ' . $from . "\r\n" . 'X-Mailer: PHP/' . phpversion() . "\r\n" . 'MIME-Version: 1.0' . "\r\n" . 'Content-Type: text/html; charset=UTF-8' . "\r\n";
// Update the activation variable below
$hashed = 'you password' . $_POST['password'];
$activate_link = 'http://localhost/eHostel%20Source%20code/activate.php?emailid=' . $_POST['emailid'] . '&code=' . $uniqid;
$message = '<p>Please click the following link to activate your account: <a href="' . $activate_link . '">' . $activate_link . '</a>  <a href="' . $password . '">' . $hashed . '</a>   </p>';
mail($_POST['emailid'], $subject, $message, $headers);

echo "<script>viewmessagebox('Please check your emailid to activate your account!....','index.php')</script>";
}
 else {
    // Something is wrong with the sql statement, check to make sure accounts table exists with all 3 fields.
            echo "<script>viewmessagebox('UNKOWN ERROR TRY AGAIN ....','guest - Copy.php')</script>";

}
    }
    $stmt->close();
} else {
    // Something is wrong with the sql statement, check to make sure accounts table exists with all 3 fields.
            echo "<script>viewmessagebox('UNKOWN ERROR TRY AGAIN ....','guest - Copy.php')</script>";

}
$con->close();

}
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • have you tried to use `error_reporting(E_ALL); ini_set('display_errors', '1');` at the beginning of php code? It should report MYSQL errors. Also wondering at which statement exactly it fails to execute. – rlf89 Apr 17 '21 at 07:58

1 Answers1

0

Okay, I found the issue. Here is a problem with line:

$emp->bind_param('ssssss', $_POST['gender'], $_POST['emp_type'], $_POST['designation'], $stat, $_POST['emp_name'], $password, $_POST['login_id'], $uniqid);

Here you are binding 8 variables, but on first argument here are only 6 types of data:

'ssssss'

Also it would be better to save data using proper types:

i - integer, d - double, s - string, b - BLOB

rlf89
  • 1,336
  • 1
  • 11
  • 17
  • 1
    Thank you so much!....just fixed it and it worked....it was puzzling me that it keep printing out that the row inserted without any error....but now everything is great :) – Abood Hamed Apr 17 '21 at 11:55