Jump to content

[php/Mysql] I need some help with an idea


jensvanherck

Recommended Posts

Greetings all,(I hope this is the right board to ask this kind of question)

I just got an idea for something but I don't really know how to implement / write the script...

I hope I'm not to vague...

so i have a form with several fields.

when the fields are submitted I want to  check if the value in 1 of the fields already exists (like a city name for example) => when it exist get the ID that is given to it by autoincrement

=>when it doesn't exist create the value in the mysql db and then get the ID

....

=> then use the ID for use with the other values that are within the other fields and submit it to an other mysql table

 

:o I hope somebody understands what i mean

 

Thank you for your time,

Jens Van Herck

Link to comment
https://forums.phpfreaks.com/topic/185813-phpmysql-i-need-some-help-with-an-idea/
Share on other sites

It sounds like your after something like this...

 

$result = mysql_query("SELECT id FROM table WHERE name=$name");

if(mysql_num_rows($result) != 0) {
   $id = mysql_fetch_result($result, 0);
} else {
   mysql_query("INSERT INTO table (field1, field2) VALUES ('$value1', '$value2')");
   $id = mysql_insert_id();
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.