仅在fb_id尚不存在时插入 - 否则什么也不做 - PHP

时间:2022-01-17 10:18:54

I'm sure there are very similar questions to this one on here but upon doing countless searches like this, all I have been coming up with is ones for other languages other than PHP or slightly different constructs that throw off what I am trying to do and causes errors. SO please excuse me if it sounds a little redundant.

我确信在这里有一些非常类似的问题,但是在进行无数次这样的搜索之后,我所提出的所有内容都是针对除PHP之外的其他语言或稍微不同的结构,这些都是我想要做的事情。并导致错误。如果听起来有点多余,请原谅我。

I have the following code that enters data into the MySQL databse just fine and works...

我有以下代码将数据输入MySQL数据库就好了并且工作...


mysql_connect("" . $db_server_name . "", "" . $db_username . "", "" . $db_password . "") or die(mysql_error()); mysql_select_db("" . $db_database_name . "") or die(mysql_error()); 

mysql_query("INSERT INTO users (fb_id,fb_name,fb_first_name,fb_last_name,fb_profile_link,fb_username,fb_birthday,fb_gender,fb_email,fb_locale,reg_date,app_points,app_submissions,app_sub_available,app_wins) VALUES ( 'item','item','item','item','item','item','item','item','item','item','item','item','item','item','item' )"); 

echo "Your table has been populated";

But I simply want the following...

但我只想要以下......

  1. To check and see if the fb_id already exists (which is known locally as $user_id variable) ...in other words it will see if $user_id already exists in the tabe where fb_id is.
  2. 检查并查看fb_id是否已经存在(在本地称为$ user_id变量)...换句话说,它将查看fb_id所在的tabe中是否已存在$ user_id。

  3. Insert the data (from the working code above) if it does not exist.
  4. 如果数据不存在,则插入数据(来自上面的工作代码)。

  5. Print "It already exists" if it does exist.
  6. 如果确实存在,则打印“它已存在”。

I know this seems like a simple fix for many of you but oddly enough, I have never had to do this before so it's not so simple for me.

我知道这对你们很多人来说似乎是一个简单的解决方案,但奇怪的是,我以前从来没有这样做过,所以对我来说并不是那么简单。

Thanks A Bunch for your time!

谢谢你的时间!

2 个解决方案

#1


1  

$data = mysql_query("SELECT * FROM table_name WHERE fb_id = $fb_id") or die(mysql_error());
if(!mysql_num_rows($data)){
mysql_query("INSERT INTO users (fb_id,fb_name,fb_first_name,fb_last_name,fb_profile_link,fb_username,fb_birthday,fb_gender,fb_email,fb_locale,reg_date,app_points,app_submissions,app_sub_available,app_wins) VALUES ( 'item','item','item','item','item','item','item','item','item','item','item','item','item','item','item' )"); 

    echo "Your table has been populated";
}esle{
    echo "Id Already exist!";
}

#2


0  

so why not do a "select fb_id as user_id from users where fb_id=$user_id" then if that returns no rows do the insert.

那么为什么不做“从fb_id = $ user_id的用户选择fb_id作为user_id”然后如果返回没有行进行插入。

#1


1  

$data = mysql_query("SELECT * FROM table_name WHERE fb_id = $fb_id") or die(mysql_error());
if(!mysql_num_rows($data)){
mysql_query("INSERT INTO users (fb_id,fb_name,fb_first_name,fb_last_name,fb_profile_link,fb_username,fb_birthday,fb_gender,fb_email,fb_locale,reg_date,app_points,app_submissions,app_sub_available,app_wins) VALUES ( 'item','item','item','item','item','item','item','item','item','item','item','item','item','item','item' )"); 

    echo "Your table has been populated";
}esle{
    echo "Id Already exist!";
}

#2


0  

so why not do a "select fb_id as user_id from users where fb_id=$user_id" then if that returns no rows do the insert.

那么为什么不做“从fb_id = $ user_id的用户选择fb_id作为user_id”然后如果返回没有行进行插入。