PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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:
There are a couple of errors in you code .... ($t_value = $row['name'] ) ... this will assign $row['name'] to $t_value which will almost always evaluate to true... your break will only break out of the while loop, which will then execute the insert statement.
$conn = mysql_connect('localhost','root','root_pass');
$db = mysql_select_db($conn,'people');
$actor_list = "John Wayne | Bruce Lee | Steven Segall | Tim Meadows | Jack Black";
$actor_array = explode("|", $actor_list);
foreach($actor_array as $actor)
{
$query = <<<SQL
INSERT INTO
name
(name)
VALUES
('$actor')
SQL;
$result = @mysql_query($query);
if(mysql_error() == '1062')
{
echo "$actor was already found, and was not added.";
}
}
This will work if the name is the primary key.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
<?php
// define the array
$actors = "John Wayne | Bruce Lee | Steven Segall | Tim Meadows | Jack Black";
$indiv = explode("|", $actors);
//count the fields
$num_name = count($indiv);
// mysql_connect('localhost', 'root');
// mysql_select_db('people');
$i = 0;
for ($i = 0; $i < $num_name; $i ++) {
$sql = "SELECT * FROM people_tbl WHERE strtoupper(name) = strtoupper($indivї$i])";
$result1 = mysql_query($sql);
if (!$result1) {
// put your name was NOT found code here
} else{
// put your name was found code here
}
}
?>