如果我更改 table 名称,php 不会将数据插入 MySQL 数据库

php wont insert data into MySQL DB if i change the table name

我的问题是我有两个代码,一个用于访客 table,另一个用于员工 table...它们实际上是相同的,但列名称有所变化。 ...来宾代码就像魅力...但员工代码根本不会插入该行...它没有向我显示错误,而是打印出 TRUE 消息。

这里是员工代码:

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();

}
}

这里是访客代码:

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();

}
}

好的,我找到问题了。这是行的问题:

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

这里绑定了 8 个变量,但是这里的第一个参数只有 6 种类型的数据:

'ssssss'

另外最好使用适当的类型保存数据:

i - 整数, d - 双, s - 字符串, b - BLOB