【问题标题】:php wont insert data into MySQL DB if i change the table name [duplicate]如果我更改表名 [重复],php 不会将数据插入 MySQL DB
【发布时间】:2021-04-17 03:24:25
【问题描述】:

我的问题是我有两个代码,一个用于来宾表,另一个用于员工表....它们实际上是相同的,但列名有所变化....来宾代码就像魅力一样。 ..但是员工代码根本不会插入该行...它不会显示错误它会打印出 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();

}
}

【问题讨论】:

  • 你试过在php代码开头使用error_reporting(E_ALL); ini_set('display_errors', '1');吗?它应该报告 MYSQL 错误。还想知道它到底是在哪个语句执行失败。

标签: php mysql mysqli


【解决方案1】:

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

$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

【讨论】:

  • 非常感谢!....刚刚修复它并且它工作了....让我感到困惑的是它不断打印出插入的行没有任何错误....但现在一切很棒:)
猜你喜欢
  • 1970-01-01
  • 2016-02-16
  • 1970-01-01
  • 2018-05-16
  • 1970-01-01
  • 2016-11-02
  • 1970-01-01
  • 1970-01-01
  • 2015-08-25
相关资源
最近更新 更多