douzhuo6270
2015-02-26 00:29
浏览 35
已采纳

bind_param新手,这段代码出了什么问题?

        // Insert the new user into the database
        // This WORKS, and was copied from an example
        if ($insert_stmt = $mysqli->prepare("INSERT INTO members (username, email, password, salt) VALUES (?, ?, ?, ?)")) {
            $insert_stmt->bind_param('ssss', $username, $email, $password, $random_salt);
            // Execute the prepared query.
            if (! $insert_stmt->execute()) {
                header('Location: ../error.php?err=Registration failure: MEMBER. Please contact the developer.');
            }
            $insert_stmt->close();



            // If user inserted, add place with user as owner
            // This DOESN'T work, and was added by me
            //$ownerid = $mysqli->lastInsertId();
            $placename = $_POST['placename'];
            $placename = mysqli_real_escape_string($mysqli, $placename);
            $location = $_POST['autocomplete'];
            $location = mysqli_real_escape_string($mysqli, $location);
            if ($place_stmt = $mysqli->prepare("INSERT INTO places (member_owner, location, name) VALUES (?, ?, ?)")) {
                $place_stmt->bind_param('iss', 1, $location, $placename);
                if (! $place_stmt->execute()) {
                    header('Location: ../error.php?err=Registration failure: PLACE. Please contact the developer.');
                }
            }
            $place_stmt->close();
        }

        header('Location: ./register_success.php');

I can confirm that the 2 variables $location and $placename are successfully retrieved. The result I get from running this code is that the members table is successfully updated, but the places table is not and the script dumps me into a blank HTML.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doouzlrvb01417498 2015-02-26 03:30
    已采纳

    I figured out that bind_param doesn't like to accept hard-coded values. I was trying to "test" my code by inserting a value of 1 into a column before I messed around with trying to get the last inserted ID. The error reporting suggested by Fred really helped (as did other suggestions, as you can see I've implemented).

    The altered code:

        // Insert the new user into the database 
        if ($insert_stmt = $mysqli->prepare("INSERT INTO members (username, email, password, salt) VALUES (?, ?, ?, ?)")) {
            $insert_stmt->bind_param('ssss', $username, $email, $password, $random_salt);
            // Execute the prepared query.
            if (! $insert_stmt->execute()) {
                header('Location: ./error.php?err=Registration failure: MEMBER. Please contact the developer.');
                exit;
            }
            $insert_stmt->close();
    
            // If user inserted, add place with user as owner
            $ownerid = $mysqli->insert_id;
            if ($place_stmt = $mysqli->prepare("INSERT INTO places (member_owner, location, name) VALUES (?, ?, ?)")) {
                $place_stmt->bind_param('iss', $ownerid, $location, $placename);
                if (! $place_stmt->execute()) {
                    header('Location: ./error.php?err=Registration failure: PLACE. Please contact the developer.');
                    exit;
                }
            }
            $place_stmt->close();
            header('Location: ./register_success.php');
        }
    

    Thanks for all the help!

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题