Mysqli没有导入字母

时间:2014-09-01 18:37:14

标签: php mysqli

我正在尝试为我的网站注册PHP,并且我正在尝试将使用mysql的旧脚本转换为mysqli。我遇到一个问题,当我在任何文本字段中键入任何字母(abc)时,数据不会导入数据库。如果我在所有的盒子中使用数字(123)它可以工作并且导入正常。我已经尝试将其与用户名和密码的一些字母混合起来,以查看是否只有一个文本框导致了问题,但是任何带有字母的框都会导致脚本无效。

这是我的PHP脚本:

<?php

$mysqli = new mysqli("localhost","root","","users_db");

if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

/* Define username */
if(isset($_POST['username'])){
    $username = $_POST['username'];
}

/* Define email */
if(isset($_POST['email'])){
    $email = $_POST['email'];
}

/* Define password */
if(isset($_POST['password'])){
    $password = $_POST['password'];
}

/* Define cpassword */
if(isset($_POST['cpassword'])){
    $cpassword = $_POST['cpassword'];
}

if (trim($username) == ''){
    echo 'No username entered.';
    exit();
}

if (strlen($username) <= 5 || strlen($username) >= 30){
    echo 'Username needs to be between 5 and 30 characters';
    exit();
    }

if (trim($email) == ''){
    echo 'No email entered.';
    exit();
}

if (trim($password) == ''){
    echo 'Invalid password.';
    exit();
}

if ($password != $cpassword){
    echo 'Passwords do not match';
    exit();
}

$run = mysqli_query($mysqli, "SELECT * FROM users WHERE username='$username'");

if (mysqli_num_rows($run)>0){
    echo 'Username already exists';
    exit();
}

$import = "INSERT INTO users (username,email,password) VALUES ($username,$email,$password)";

if (mysqli_query($mysqli, $import)){
    echo 'Registration Successful';

    $result = mysqli_query($mysqli, "SELECT * FROM users WHERE username='$username'");
    $row = mysqli_fetch_array($result);
    $id = $row['id'];

    mkdir("../users/" . $id, 0777, true);
    fopen("../users/" . $id . "/" . "New User.txt", "w") or die("Unable to create file");
}else{
    echo 'Failed to import';
}

?>

我是PHP和mysqli的新手,所以如果我做一些愚蠢的事情,不要太苛刻 :)

1 个答案:

答案 0 :(得分:0)

感谢弗雷德-ii-给我的报价($ username,$ email,$ password)工作,现在一切正常。我还将解决上面提出的其他问题。