Break the loop if value is encountered
Posted: Wed Apr 21, 2004 11:04 pm
A few things suggested here didn't work, so I'll throw the problem back in here.
The user can input names of people, and PHP will split and trim based on the | delimiter. From there, I would like to insert the names into a database ONLY if it isn't there. Here's what I have, and isn't breaking if found:
It will echo 'found' it seems, at random, so I am obviously doing something wrong here.
The user can input names of people, and PHP will split and trim based on the | delimiter. From there, I would like to insert the names into a database ONLY if it isn't there. Here's what I have, and isn't breaking if found:
Code: Select all
<?php
mysql_connect('localhost', 'root');
mysql_select_db('people');
$actors = "John Wayne | Bruce Lee | Steven Segall | Tim Meadows | Jack Black";
$indiv = explode("|", $actors);
foreach($indiv as $key=>$value){
$t_value = trim($value);
echo $key.': '.$t_value.' '.'<br />';
$get = "SELECT name FROM people_table";
$get_query = mysql_query($get);
while($row = mysql_fetch_assoc($get_query)){
if($t_value = $row['name']){
echo 'found...';
break;
}
}
$sql = "INSERT INTO `people_table` (`name`) VALUES ('$value')";
$query = mysql_query($sql) or die(mysql_error());
}
?>