检查行是否存在

时间:2013-04-14 18:38:00

标签: php mysql

我有一个表单,在提交时,数据将被插入三个表(user,journey,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
        }

1 个答案:

答案 0 :(得分: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.
}