0

我正在用 PHP 编写一个脚本来将用户插入数据库中的表中。这是代码:

<?php
$email      = check_input($_POST['EmailAddress'], "Enter your email address");
$pass       = check_input($_POST['Password'], "Enter a password");
$first_name = check_input($_POST['FirstName'], "Enter your first name");
$last_name  = check_input($_POST['LastName'], "Enter your last name");
$gender     = check_input($_POST['Gender']);
$dobmonth   = check_input($_POST['dobmonth']);
$dobday     = check_input($_POST['dobday']);
$dobyear    = check_input($_POST['dobyear']);
$dob        = $dobmonth.'/'.$dobday.'/'.$dobyear;
$phone      = check_input($_POST['CellPhone']);
$fanmail    = check_input($_POST['FanMail']);

if (verify($email) === true) {

从这里编辑:

$link = new mysqli("localhost","263764","Corvette88", "256764 ");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
} else {
    echo 'connected to db table ';
}
echo $mysqli->host_info . "\n";

$query = "INSERT INTO 68_users VALUES ('$email','$pass','$first_name','$last_name','$dob','$gender','$phone','$fanmail')" or die(mysql_error());
if ( !mysqli_query($link, $query) ) {
echo 'error: '.mysqli_error($link);
exit();
}

echo "1 record added ";

mysqli_close($link);

到这里..

echo"connection closed";
} else {echo'unverifiable email address ';
    echo $email;
}


function check_input($data, $problem='') {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0) {
    die($problem);
}
return $data;
}

function conntodb() {

}

function verify($email){
$isValid = true;
$atIndex = strrpos($email,'@');
if (is_bool($atIndex) && !$atIndex) {
    return false;
} else {
    $domain = substr($email,$atIndex+1);
    $local = substr($email, $atIndex);
}
$localLen = strlen($local);
$domainLen = strlen($domain);
if($localLen < 1 || $localLen > 64) {
    return false;
}else if ($domainLen < 1 || $domainLen > 255) {
    return false;
}
if ($local[0] == '.' || $local[$localLen-1] == '.') {
    return false;
} elseif (preg_match('/\\.\\./', $local)) {
    return false;
}
if(!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) {
    return false;
} else if (preg_match('/\\.\\./', $domain)) {
    return false;
} else if (!(checkdnsrr($domain,"MX") || checkdnsrr($domain, "A"))) {
    return false;
}
return $isValid;
}

?>

我已经为代码设置了检查点,以显示它是否出错以及是否成功。

我的问题是:它告诉我它已成功地将行添加到数据库中,但是当我检查我的数据库时,它没有显示数据库以任何方式被填充。我不知道我哪里出错了,因为它正在通过我的检查点..有什么想法吗?

4

1 回答 1

3

您没有检查查询中的错误:

mysql_query ("INSERT INTO 68_users (email, pass, first_name, last_name, dob, gender,     phone, fanmail)
VALUES ('$email','$pass','$first_name','$last_name','$dob','$gender','$phone','$fanmail')");

改为这样做:

mysql_query ("INSERT INTO 68_users (email, pass, first_name, last_name, dob, gender,     phone, fanmail)
VALUES ('$email','$pass','$first_name','$last_name','$dob','$gender','$phone','$fanmail')") or die(mysql_error());

此外,您甚至不应该使用 mysql_*。请改用 PDO 或 MySQLi。看到这个

尝试改变

mysqli_query($link, $query);

至:

if ( !mysqli_query($link, $query) )
{
    echo 'error: '.mysqli_error($link);
    exit();
}
于 2013-01-02T19:58:33.170 回答