Jump to content


Photo

Newbie with problems


  • Please log in to reply
3 replies to this topic

#1 enkidu72

enkidu72
  • Members
  • PipPipPip
  • Advanced Member
  • 51 posts

Posted 13 September 2006 - 12:50 PM

Hello everybody ...
I'm quite new with this and I'm having some problems ...
That's what I'm trying to do :

$query="SELECT id,name,surname FROM autori WHERE surname='$surname_autor' ";
$result=mysql_query($query);
if (mysql_num_rows($result)) {
while($row = mysql_fetch_array( $result )){
$autorID=$row['id'];
if (($row['name']===$name_autor ) &&  ($row['surname']===$surname_autor )){
echo "Existing record with ID :$autorID <br>";
break ;
} else {
mysql_query("INSERT INTO autors(name,surname) VALUES
('$name_autor','$surname_autor')") or die(mysql_error());
$autorID=mysql_insert_id();
echo "Autor ID is :$autorID <br>";

}
}

}else{
mysql_query("INSERT INTO autors(name,surname) VALUES
('$name_autor','$surname_autor')") or die(mysql_error());
$autorID=mysql_insert_id();
echo "Autor ID is :$autorID <br>";
}


What i get is that if autor doesn't exist , I have a new record inserted .
If exists , it is inserted again twice !

Someone can help here ?
I'm sure it's a very silly thing but I'm trying to solve it since 2 days with no result :'-(


thx in advance

David





#2 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 13 September 2006 - 01:01 PM

I have a question, if you already have $surname_autor and $name_autor, why not just include them in the original sql statement and only add them if neither are a match?

$query="SELECT id, name, surname FROM autors WHERE name = '$name_autor' AND surname='$surname_autor'";
$result=mysql_query($query);
if (mysql_num_rows($result)) {
   $autorID=$row['id'];
   echo "Existing record with ID :$autorID
}
else {
   mysql_query("INSERT INTO autors (name, surname) VALUES ('$name_autor', '$surname_autor')") or die(mysql_error());
   $autorID=mysql_insert_id();
   echo "Autor ID is :$autorID
}


Regards
Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#3 enkidu72

enkidu72
  • Members
  • PipPipPip
  • Advanced Member
  • 51 posts

Posted 13 September 2006 - 01:05 PM

That's a very good question :D
Anyway , did I make something wrong here :

if (($row['name']===$name_autor ) &&  ($row['surname']===$surname_autor )){

?

( Just to know ... I'm trying to learn this ) :)

Many thx

David

#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 13 September 2006 - 01:08 PM

David, that IF statement appears to be constructed correctly yes.

Regards
Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users