0

登録データをデータベースに挿入する登録ページがあります。これは次のようになります。

if ($_POST['password'] == $_POST['conf_pass']){
        $user = $_POST['username'];
        $pass = $_POST['password'];

        $hash = md5(rand(0,1000));

        $accres = mysqli_query($connection, "INSERT INTO accounts (street1, suburb, city, postcode, username) VALUES($address, $suburb, $city, $postcode, $username)");
        $account_id = mysqli_insert_id($accres);
        $peopleres = mysqli_query($connection, "INSERT INTO people (lastname, firstname, accounts_id, birthdate, phonenumber, username, password, email, hash) VALUES($lastname, $firstname, $account_id, $birthdate, $phonenumber, $username, $password, $email, $hash)");

        $peoplerows=mysqli_fetch_assoc($peopleres);
        $person_id=$peoplerows[0];

        mysqli_query("INSERT INTO PeopleToRole (role_id) VALUES(1)");
        $email = $_SESSION['email'];
        $p->addContent("User Registered");
}

私は当初、postgres を使用してこれらすべてをプログラムしましたが (Apache サーバーでローカルにホストされていました)、ホスト Web サイトが既に mysqli で動作していたため、mysqli に変更する必要がありました。

したがって、このコードはページに登録されたユーザーを返すため、if ステートメントは機能しています。しかし、何らかの理由で、insert ステートメントはデータベースに何も挿入しません。

ある種のフォーマットエラーがありますか? または私が逃した小さな何か?ありとあらゆる助けをいただければ幸いです。ありがとう

4

2 に答える 2

3

クエリの引用符を忘れました。たとえば、次のように変更する必要があります。

"INSERT INTO accounts (street1, suburb, city, postcode, username) VALUES($address, $suburb, $city, $postcode, $username)"

に:

"INSERT INTO accounts (street1, suburb, city, postcode, username) VALUES('$address', '$suburb', '$city', '$postcode', '$username')"

つまり、このように動作すると、コードが sql インジェクションに対して脆弱になります (上記のコメントで cfreak が言及したように)。

コードをより安全にするために使用できる方法を示すマニュアルの小さな例を次に示します。bind_param()

$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$stmt = $mysqli->prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)");
$stmt->bind_param('sssd', $code, $language, $official, $percent);

$code = 'DEU';
$language = 'Bavarian';
$official = "F";
$percent = 11.2;

/* execute prepared statement */
$stmt->execute();

printf("%d Row inserted.\n", $stmt->affected_rows);

/* close statement and connection */
$stmt->close();
于 2013-10-01T01:35:34.687 に答える
1

よくある問題: クエリのエラーをわざわざチェックしていないため、何か問題が発生した場合、何が起こったのかわかりません。

mysqli_query()forの戻り値を確認しFALSE、見つかった場合mysqli_error($connection)はエラー メッセージを確認します。

例えば:

$accres = mysqli_query($connection, "INSERT INTO accounts (street1, suburb, city, postcode, username) VALUES($address, $suburb, $city, $postcode, $username)");
if ($accres === false) {die(mysqli_error($connection));}

他のクエリについても同様のことを行います。エラー メッセージが表示された場合は、修正するか、もう一度質問してください。

于 2013-10-01T01:35:24.637 に答える