我使用以下代码将数据输入到我的数据库中,但即使数据未插入数据库也不会产生错误。请帮帮我
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpassword = "root";
$conn = mysqli_connect($dbhost, $dbuser, $dbpassword);
if(! $conn )
{
die('Could not connect: ' . mysqli_error());
}
$post_enroll = $_POST['enroll'];
$post_mobile = $_POST['mobile'];
$post_email = $_POST['email'];
$post_password = $_POST['password'];
$sql = "INSERT INTO login (enroll, mobile, email, password) VALUES ('".$post_enroll."','".$post_mobile."','".$post_email."','".$post_password."')";
mysqli_select_db('rgpv');
$retval = mysqli_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysqli_error());
}
mysqli_close($conn);
?>
答案 0 :(得分:0)
更改
mysqli_select_db('rgpv');
$retval = mysqli_query( $sql, $conn );
要
mysqli_select_db($conn , "rgpv");
$retval = mysqli_query( $conn , $sql);
而不是使用:
if(! $retval )
使用:
if (mysqli_errno($conn) != 0){
答案 1 :(得分:0)
SO的许多专家强烈建议不要使用mysql / i函数。他们的建议是接受PDO。不过,我会接受他们的建议。 在这种情况下;我建议如下:
<?php
//DATABASE CONNECTION CONFIGURATION:
defined("HOST") or define("HOST", "localhost"); //REPLACE WITH YOUR DB-HOST
defined("DBASE") or define("DBASE", "rgpv"); //REPLACE WITH YOUR DB NAME
defined("USER") or define("USER", "root"); //REPLACE WITH YOUR DB-USER
defined("PASS") or define("PASS", "root"); //REPLACE WITH YOUR DB-PASS
$post_enroll = isset($_POST['enroll']) ? htmlspecialchars(trim($_POST['enroll'])) : null; //PROTECT AGAINST ATTACKS
$post_mobile = isset($_POST['mobile']) ? htmlspecialchars(trim($_POST['mobile'])) : null; //PROTECT AGAINST ATTACKS
$post_email = isset($_POST['email']) ? htmlspecialchars(trim($_POST['email'])) : null; //PROTECT AGAINST ATTACKS
$post_password = isset($_POST['password']) ? htmlspecialchars(trim($_POST['password'])) : null; //PROTECT AGAINST ATTACKS
try {
$dbh = new PDO('mysql:host='.HOST.';dbname='. DBASE,USER,PASS);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare("INSERT INTO login (`enroll`, `mobile`, `email`, `password`) VALUES (:enroll, :mobile, :email, :password)");
$stmt->bindParam(':enroll', $post_enroll);
$stmt->bindParam(':mobile', $post_mobile);
$stmt->bindParam(':email', $post_email);
$stmt->bindParam(':password', $post_password);
$status = $stmt->execute();
if(! $status ) {
die('Something went wrong...');
}
//GARBAGE COLLECTION
$dbh = null;
}catch(PDOException $e){
echo $e->getMessage();
}
?>
我希望这有点帮助...