0

我有一个表单,提交后,数据将插入三个表(用户、旅程、user_journey 表)。在插入数据之前,我想检查用户表中是否已经存在该用户。如果没有,那么没有问题,用户将被插入到用户表中,但是,如果用户已经存在于用户表中,我不想再次添加用户。我想获取用户的 user_id 并插入第三个表(user_journey)。

目前,当我提交表单时,即使用户已经存在于表中,用户也会被插入到用户表中。

我不确定我检查用户是否存在的方式是否正确以及我获取 user_id 的方式。任何意见,将不胜感激

$query = $db->query("SELECT COUNT(*) FROM user WHERE facebook_id = '.$hdnFacebookId.'");

        //$query->execute();
        //$countRows = $query->rowCount();//return number of rows
        //check to see if user is already in the database

        if  ($query->fetchColumn() > 0)
        {
            if ($oneWay)
            {
                $query_journey = $db->prepare("INSERT INTO journey
                (from_destination,to_destination,journey_type,depart_date,depart_time,seats_available,journey_message,user_type)
                VALUES('$pjFrom','$pjTo','$radioJourneyType', STR_TO_DATE('$departDate','%d/%m/%Y'),'$newDepTime','$seatcounter','$textareanotes','$radUserType')");
            }
            else
            {
                $query_journey = $db->prepare("INSERT INTO journey
                (from_destination,to_destination,journey_type,depart_date,depart_time,return_date,return_time,seats_available,journey_message,user_type)
                VALUES('$pjFrom','$pjTo','$radioJourneyType', STR_TO_DATE('$departDate','%d/%m/%Y'),'$newDepTime',STR_TO_DATE('$returnDate','%d/%m/%Y'),'$newRetTime ','$seatcounter','$textareanotes','$radUserType')");
            }
            $user_query = $db->prepare("SELECT user_id FROM user WHERE facebook_id = '$hdnFacebookId'");
            $result = $user_query->execute();
            $user_query_result = $user_query->fetch(PDO::FETCH_ASSOC);

            $query_journey->execute();//EXECUTE QUERY

            $lastJourneyID = $db->lastInsertId();
            $queryUserJourney = $db->prepare("INSERT INTO user_journey
                                (journey_id,user_id)
                                VALUES('$lastJourneyID','$user_query_result')");
            $queryUserJourney->execute();//EXECUTE QUERY

            //include('index.php');
        }
        else //insert user
        {
            //if $oneWay true, then omit $returnDate and $returnTime
            if ($oneWay)
            {
                $query = $db->prepare("INSERT INTO journey
                (from_destination,to_destination,journey_type,depart_date,depart_time,seats_available,journey_message,user_type)
                VALUES('$pjFrom','$pjTo','$radioJourneyType', STR_TO_DATE('$departDate','%d/%m/%Y'),'$newDepTime','$seatcounter','$textareanotes','$radUserType')");
            }
            else
            {
                $query = $db->prepare("INSERT INTO journey
                (from_destination,to_destination,journey_type,depart_date,depart_time,return_date,return_time,seats_available,journey_message,user_type)
                VALUES('$pjFrom','$pjTo','$radioJourneyType', STR_TO_DATE('$departDate','%d/%m/%Y'),'$newDepTime',STR_TO_DATE('$returnDate','%d/%m/%Y'),'$newRetTime ','$seatcounter','$textareanotes','$radUserType')");
            }
            $queryfb = $db->prepare("INSERT INTO user
                (facebook_id,facebook_username,facebook_first_name,facebook_last_name,facebook_image,facebook_link)
                VALUES('$hdnFacebookId','$hdnUsername','$hdnFirstName','$hdnLastName','$hdnFacebookImg','$hdnFacebookUrl')");

            $query->execute();
            $lastUserID = $db->lastInsertId();
            $queryfb->execute();
            $lastJourneyID = $db->lastInsertId();
            $queryUserJourney = $db->prepare("INSERT INTO user_journey
                                (user_id,journey_id)
                                VALUES('$lastJourneyID','$lastUserID')");
            $queryUserJourney->execute();

        }

更新

function userExists($db, $hdnFacebookId)
        {
            $userQuery = "SELECT * FROM user WHERE facebook_id = :user;";
            $stmt = $db->prepare($userQuery);
            $stmt->execute(array(':user'=>$hdnFacebookId));
            $result = $stmt->fetch(PDO::FETCH_ASSOC);
            if($result)
            {
                return true;
            }
            return false;
        }

        $userExists = userExists($db,$hdnFacebookId);
        if($userExists)
        {
            //don't insert user
            //get user's id from database
            $user_query = $db->prepare("SELECT * FROM user WHERE facebook_id = '$hdnFacebookId'");
            $result = $user_query->execute();
            $user_query_result = $user_query->fetch(PDO::FETCH_ASSOC);
            $userID = $user_query_result['user_id'];
            $query_journey->execute();//EXECUTE QUERY
            $lastJourneyID = $db->lastInsertId();
            $queryUserJourney = $db->prepare("INSERT INTO user_journey
                                (journey_id,user_id)
                                VALUES('$lastJourneyID','$userID')");
            $queryUserJourney->execute();//EXECUTE QUERY
        }
        else
        {
            //insert user
        }
4

1 回答 1

3

典型的“检查用户是否存在”:

function userExists($db, $user)
{
    $userQuery = "SELECT * FROM users u WHERE u.user=:user;";
    $stmt = $db->prepare($userQuery);
    $stmt->execute(array(':user' => $user));
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    if($result)
    {
         return true;
    }
    return false;
}

所以你可以做类似的事情

$user = isset($_POST['user']) ? $_POST['user'] : "Unknown";
$userExists = userExists($db, $user);
if($userExists)
{
    // Don't insert
]
else
{
    // Insert the user.
}
于 2013-04-14T18:41:16.013 回答