PHP select

He has: 1,380 posts

Joined: Feb 2002

hey...what i'm trying to do is have someone submit a bunch of information for a contest, but before its put into the MySQL table i want to search that table for some of the elements to make sure its not a double entry...see what i'm saying? but i've been trying to figure it out...having problems...heres what i've got:

the connection code...and now the real stuff

mysql_select_db ("westernc_emails");
$search=("SELECT * FROM Name, Address, City, StateProv, Country, Zip, Email, Domain, Phone")
if ($search==$insert) {
echo "You have already entered.\n"
}else{
$insert=mysql_query("INSERT INTO creationcon (Name, Address, City, StateProv, Country, Zip, Email, Domain, Phone) VALUES ('$Name', '$Address', '$City', '$StateProv', '$Country', '$Zip', '$Email', '$Domain', '$Phone') ");
mysql_close($dbh);
    }
?>
'

now, i realize that the $insert comes after the $search, but...i couldnt think of any other way, looked at webmonkey's php tutorials and such...

i get an error on the "if ($search==$insert) {" line, who knows if more comes after that. but, if you could help me out with this search and then submist think...that owuld be awesome. thanks

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

<?php
mysql_select_db
(\"westernc_emails\");

$result = mysql_query(\"SELECT * FROM creationcon WHERE\"
            .\" Name='
$Name'\"
            .\", Address='
$Address'\"
            .\", City='
$City'\"
            .\", StateProv='
$StateProv'\"
            .\", Country='
$Country'\"
            .\", Zip='
$Zip'\"
            .\", Email='
$Email'\"
            .\", Domain='
$Domain'\"
            .\", Phone='
$Phone'\");

if (mysql_num_rows(
$result)>0) {
    echo \"You have already entered.\n\"
}
else{
    mysql_query(\"INSERT INTO creationcon SET\"
            .\" Name='
$Name'\"
            .\", Address='
$Address'\"
            .\", City='
$City'\"
            .\", StateProv='
$StateProv'\"
            .\", Country='
$Country'\"
            .\", Zip='
$Zip'\"
            .\", Email='
$Email'\"
            .\", Domain='
$Domain'\"
            .\", Phone='
$Phone'\");
}
?>

Mark Hensler
If there is no answer on Google, then there is no question.

He has: 1,380 posts

Joined: Feb 2002

thanks!

He has: 1,380 posts

Joined: Feb 2002

now i'm getting some errors:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL

result resource in /home/.../creationcon.php on line 15

Warning: Cannot add header information - headers already sent by (output started at /home/.../creationcon.php:15) in /home/.../creationcon.php on line 31
'
and i pasted it just like you had it,
$result = mysql_query("SELECT * FROM creationcon WHERE"
            ." Name='$Name'"
            .", Address='$Address'"
            .", City='$City'"
            .", StateProv='$StateProv'"
            .", Country='$Country'"
            .", Zip='$Zip'"
            .", Email='$Email'"
            .", Domain='$Domain'"
            .", Phone='$Phone'");

if (mysql_num_rows($result)>0) {
echo "You have already entered.\n";
}
else{
    mysql_query("INSERT INTO creationcon SET"
            ." Name='$Name'"
            .", Address='$Address'"
            .", City='$City'"
            .", StateProv='$StateProv'"
            .", Country='$Country'"
            .", Zip='$Zip'"
            .", Email='$Email'"
            .", Domain='$Domain'"
            .", Phone='$Phone'");
   }
mysql_close($dbh);
header("Location: <a href="http://.../home.html" class="bb-url">http://.../home.html</a>");
'

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I dont see anything wrong with the code above. Try putting this above the IF statement...

<?php
echo \"Error (\".mysql_errno().\"): \".mysql_error();
?>

Mark Hensler
If there is no answer on Google, then there is no question.

He has: 1,380 posts

Joined: Feb 2002

the response:
Error (1064): You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near ' Address='12 Main Street, City='Anywhere', StateProv='VA

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Check to make sure you dont have any missing quotes.

If not, then replace the first "$result = mysql_query" with "die". That will print out the query that would have run. And maybe we'll see what's causing the syntax error.

Mark Hensler
If there is no answer on Google, then there is no question.

He has: 1,380 posts

Joined: Feb 2002

Error (0):
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/westernc/public_html/xroot/creationcon.php on line 15

Warning: Cannot add header information - headers already sent by (output started at /home/westernc/public_html/xroot/creationcon.php:14) in /home/westernc/public_html/xroot/creationcon.php on line 30
'

same as before...i'll pm you

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

If you're getting that message than you didn't place die() in the right place...

<?php
//$result = mysql_query
die(\"SELECT * FROM creationcon WHERE\"
            .\" Name='
$Name'\"
            .\", Address='
$Address'\"
            .\", City='
$City'\"
            .\", StateProv='
$StateProv'\"
            .\", Country='
$Country'\"
            .\", Zip='
$Zip'\"
            .\", Email='
$Email'\"
            .\", Domain='
$Domain'\"
            .\", Phone='
$Phone'\");

if (mysql_num_rows(
$result)>0) {
    echo \"You have already entered.\n\";
}
?>

Mark Hensler
If there is no answer on Google, then there is no question.

nike_guy_man's picture

They have: 840 posts

Joined: Sep 2000

Do you really have to check each field?
Couldn't you also create the MySQL fields as UNIQUE?

<?php
$result
= mysql_query(\"INSERT INTO table (fields) VALUES ('$values') \");
if (
$result) {
echo \"Inserted\";
} else {
echo \"Not inserted \" . mysql_error();
}
?>

That will return "Not inserted:" then say that it is a unique field if it is filled, right?
Otherwise, Kyle, you are missing a quote or something.
I know it's probably wrong or bad style, but try it like this:
<?php
$result
= mysql_query(\"SELECT * FROM creationicon WHERE (Name='$Name', Address='$Address', City='$City', StateProv='$StateProv', Country='$Country'\", Zip='$Zip', Email='$Email', Domain='$Domain', Phone='$Phone' \");
if (mysql_num_rows(
$result) > 0) {
echo \"already in there\";
}
?>

Of course, if this is being form submitted, you need to make all the select varialbes $_POST[] or $_GET[] (Depending on your form METHOD)

Laughing out loud

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

You could make each field a UNIQUE key, but you'll loose performance.
I don't remember what exactly was said, but somewhere in the MySQL
docs it said that making every field a key can reduce table performace
to the point where it would run faster with no keys.

Ideally, you would make a combination of few fields a single UNIQUE key.

At this point, I just want to see the exact query which is being run.
This can allow us to see what is causing the syntax error. If we can't
spot an error, we can copy & paste the query into phpMyAdmin or a
terminal and run it manually. If running the same query manually
doesn't result in an error, then the mistake is somewhere else in the
PHP code.

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 447 posts

Joined: Oct 1999

ideally you create a primary key. select where primarykey=1 is infinitely faster than select where this=that and that=lat and lat=pat and pat=matt and ...

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Kyle's trying to prevent duplicate records. Primary keys won't prevent that.

He has: 1,380 posts

Joined: Feb 2002

the info was getting posted, but it would appear to have errors when submitted, and did not redirect to a new page like it should

i replaced the $result=mysql_query with "die", and heres what it gave:SELECT * FROM creationcon WHERE Name='test', Address='test', City='test', StateProv='test', Country='USA', Zip='21431', Email='tetst', Phone='3141241'' what does that do for me/us? all it did was show the inputs i ...inputted.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

What that did is show me that you do have all of your quotes, and that the entire query appears to be correct in syntax.

In the php above (your post on May 5), there doesn't appear to be a typo between the mysql_query and the mysql_num_rows. (I'll sometimes mispell $result)

At this point, I'm at a loss. I don't know what's causing the error.

Mark Hensler
If there is no answer on Google, then there is no question.

He has: 1,380 posts

Joined: Feb 2002

when it says "headers already sent", is that referring to my redirection? if so, how can i redirect without using a header

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Header() cannot be called after the script has printed (echoed) anything. This includes white space.

He has: 1,380 posts

Joined: Feb 2002

well, i did a search on php.net for url redirection, and all i could come up with was the header() to automatically redirect...is there some way i can get around this?

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Eliminate all output prior to the header call.

mairving's picture

They have: 2,256 posts

Joined: Feb 2001

The header can be a difficult one to work with. Like Mark said, get rid of any output prior to calling it. In a script, I had this meant moving an include file to be included after I called the header.

Mark Irving
I have a mind like a steel trap; it is rusty and illegal in 47 states

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.