Jump to content

if exsists use id if not insert and use id.


brown2005

Recommended Posts

i have two tables....

 

1) people

 

people_id

people_name

 

2) names

 

names_id

names_name

 

now when someone enters their name on the register form and process i want to check if the name is in the names table and if so use that id as people_name in the people table, else insert the new name in the names table and use the id.

 

 

Link to comment
Share on other sites

php way:

mysql_query('insert into names values (.......)');
$result=mysql_query('select names_id from name where names_name=.....');
$row=mysql_fetch_array($result);
mysql_query('insert into people values(....,$row[names_id])')

 

mysql way

create trigger your_trigger
after insert on names
for each row
insert into people values (new.names_id,new.names_name)
end

 

the above code iif the name does not exists on names.

to apply completely your request you have to add somes if here and there in the code.

Link to comment
Share on other sites

$postedname = $_POST['name'];

$name = mysql_query("SELECT names_id, names_name FROM names WHERE names_name='$postedname'");

while($row = mysql_fetch_array($name)){

if ( mysql_row_count($name) == 0 ) {

	mysql_query("INSERT INTO names(names_id,names_name) VALUES('','$postedname')");

	$insert_name = mysql_insert_id();

}else{

	$insert_name = $row['names_id'];


}

}

 

then $insert_name would go into the people table

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.