PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Jun 24, 2019 9:35 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Wed Apr 21, 2004 11:04 pm 
Offline
Forum Contributor

Joined: Sun Nov 23, 2003 10:05 pm
Posts: 416
Location: San Jose CA
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:
Syntax: [ Download ] [ Hide ]
<?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());

}

?>

It will echo 'found' it seems, at random, so I am obviously doing something wrong here.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 21, 2004 11:09 pm 
Offline
Forum Donator

Joined: Mon Oct 20, 2003 8:31 pm
Posts: 1661
Location: Maine, USA
Syntax: [ Download ] [ Hide ]
$found = FALSE;

while($row = mysql_fetch_assoc($get_query) && $found == FALSE){

       if($t_value = $row['name']){

           $found = TRUE;

           echo 'found...';

           break;

       }

   }


Try that :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 21, 2004 11:17 pm 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA
uhm......
Syntax: [ Download ] [ Hide ]
if($t_value = $row['name']){

== not =


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 22, 2004 3:17 am 
Offline
Forum Contributor
User avatar

Joined: Wed Jun 05, 2002 10:59 am
Posts: 339
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.

try this
Syntax: [ Download ] [ Hide ]
<?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...';

           continue 2;

       }

   }



$sql = "INSERT INTO `people_table` (`name`) VALUES ('$value')";

$query = mysql_query($sql) or die(mysql_error());

}

?>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 22, 2004 10:01 am 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6446
Location: 53.01N x 112.48W
Well, I'm going to throw my code in here too:

Syntax: [ Download ] [ Hide ]
$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.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 11, 2004 7:21 am 
Offline
Forum Contributor

Joined: Sat Mar 27, 2004 6:54 am
Posts: 194
my $.02

Syntax: [ Download ] [ Hide ]
&lt;?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 &lt; $num_name; $i ++) &#123;
 $sql = "SELECT * FROM people_tbl WHERE strtoupper(name) = strtoupper($indiv&#1111;$i])";
  $result1 = mysql_query($sql);
  if (!$result1) &#123;
    // put your name was NOT found code here
  &#125; else&#123;
   // put your name was found code here
  &#125;
&#125;

?&gt;


Lite...


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Exabot [Bot], Majestic-12 [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group