Forum Moderators: coopster

Message Too Old, No Replies

is this query right?

         

Bigjohn

2:32 pm on Mar 4, 2004 (gmt 0)

10+ Year Member



What I'm trying to do here is:
1 - test to see if the customer is already in the database by matching name and phone number.
2 - if no match, insert a new record
3 - get the customer number in either case.


/* query customer database for a match */
$sql = 'SELECT cid, email, fullname, phone FROM customer where fullname ='.$cname.'AND phone ='.$cphone;
$result = mysql_query($sql);
if (!$result) {$cpost= "INSERT (cid, fullname, company, address1, address2, city, state, zip, country, billsame, baddress1, baddress2, bcity, bstate, bzip, bcountry, phone, fax, email, maillist)";
$cpost .= " INTO cdata VALUES ('','$cname','$ccompany','$caddr1','$caddr2','$ccity','$cstate', '$czip','$ccountry','$isbill','$baddr1','$baddr2','$bcity','$bzip', '$bcountry','$cphone','$cfax','$cemail','$maillist')";
mysql_query($cpost);
$cnum = mysql_insert_id();
} else $cnum = $result['cid'];

Please advise!

John

[edited by: jatar_k at 5:35 pm (utc) on Mar. 4, 2004]
[edit reason] fixed sidescroll [/edit]

justageek

2:47 pm on Mar 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Change this "if (!$result)" to "if mysql_num_rows($result)>0"

If it is greater than 0 then the cid can be assigned by doing something like this:

$mydata = mysql_fetch_array($result);

Which would make $mydata['cid'] have the value.

JAG

Bigjohn

2:56 pm on Mar 4, 2004 (gmt 0)

10+ Year Member



Hmmm.
my IF (!$result) is there to say 'if no match at all' then insert a new row.

so I don't understand why to do:
"if mysql_num_rows($result)>0" . I would think that if using this test instead I'd want the test to be =0, not greater than (if it's greater than, there is a match, and i want to execute the ELSE).

so, can I not use (!$result) and put the "mysql_fetch_array" in the ELSE section? I do understand why THAT is necessary - I want to get the row.

John

Timotheos

4:17 pm on Mar 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi again Bigjohn,

Your original code looks good to me. What's wrong with it?

Tim

coopster

5:03 pm on Mar 4, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I think JAG was trying to clarify something for Bigjohn. And looking at Bigjohn's response,

>>my IF (!$result) is there to say 'if no match at all' then insert a new row.

I'd say JAG was on the mark.

mysql_query() [php.net] returns
TRUE
on success and
FALSE
on error. A non-
FALSE
return value means that the query was legal and could be executed by the server. It does not indicate anything about the number of rows affected or returned. It is perfectly possible for a query to succeed but affect no rows or return no rows.

Timotheos

5:27 pm on Mar 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Op you're right, again ;-)

I went back to check to see how I'd done it in the past and yes I was using mysql_num_rows. Whew.

Bigjohn

5:35 pm on Mar 4, 2004 (gmt 0)

10+ Year Member



Ok, but I still want the negative trigger, so I test for '= zero', not '> zero', right?

Now, how do I launch a page when this processing is done?

coopster

6:50 pm on Mar 4, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Well, it's actually '== 0' (assignment is one equal sign, equality is two equal signs), but to the best of my knowledge, mysql_num_rows [php.net] won't return a negative value.

Launch a page? Just exit the parser and start into your HTML...

?> 
<html>
<head>
...

Or redirect using the header [php.net] function.

Bigjohn

7:08 pm on Mar 4, 2004 (gmt 0)

10+ Year Member



coopster -

If I echo any ERRORS to the browser from the code on this page, will HEADER'location' still work?

Call me dumb, but I can't for the life of me recall how to make an HTML page launch another page without user interaction.

John

slade7

7:18 pm on Mar 4, 2004 (gmt 0)

10+ Year Member



If you are just looking to see if the customer is already there, it would be easier to do:

$sql = 'SELECT COUNT(*) FROM customer where fullname ='.$cname.'AND phone ='.$cphone;

$result = mysql_query($sql);

if (!mysql_result($result,0)){

OR

if(mysql_result($result,0) == 0){

Coopster might could tell you which of the if statements would be best.

coopster

7:37 pm on Mar 4, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



slade7, it looks like Bigjohn might be getting ready to show a bit more information in his error message and that is why he is selecting some columns from the table.

Bigjohn, no, you can't echo anything to the browser prior to a header [php.net] function:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

It isn't clear where this code snippet is going so I tried to answer your question, how do I launch a page when this processing is done? Well, you could use header to redirect to a different url (before any other output) or you could let the code flow until your processing is complete and simply push HTML to the output and when the script is complete, the page is sent.

<?php 
// your php code here
?>
<html>
<head>
...
<?php "Your customer number is $cnum!";?>
...
</body>
</html>

Bigjohn

8:39 pm on Mar 4, 2004 (gmt 0)

10+ Year Member



@coopster -

I'm checking for matches against several values to confirm. If I check NAME, EMAIL, and PHONE number, it's very unlikely that a 'new' john smith is going to match all three, and so he'll be added as a new customer.

If however Mr Smith whos phone number is 555-1515 and his email address is johnsmith@aol.com, I don't want to add him again!

I need to match case-insensitive too, i just realized... Arrrgh. Time to strlower() again...

John

Bigjohn

8:44 pm on Mar 4, 2004 (gmt 0)

10+ Year Member



@coopster!

do 'require' and 'include' count as having echod to the browser? What about calling $_GET?

John

coopster

9:00 pm on Mar 4, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Not if you haven't output anything.

Bigjohn

9:25 pm on Mar 4, 2004 (gmt 0)

10+ Year Member



and DOH! to me... if I'm using header'location' to redirect, I don't need to include my normal web-page header on this PHP page anyway!

John

Bigjohn

3:19 pm on Mar 5, 2004 (gmt 0)

10+ Year Member



Guys and Gals...

Here are lines 47-64 of my code:


/* query customer database for a match */
$sql = 'SELECT cid, email, fullname, phone FROM customer where fullname ='.$cname.'AND phone ='.$cphone;
$result = mysql_query($sql);
if (mysql_num_rows($result)=="0") {$cpost= "INSERT (cid, fullname, company, address1, address2, city, state, zip, country, billsame, baddress1, baddress2, bcity, bstate, bzip, bcountry, phone, fax, email, maillist)";
$cpost .= " INTO cdata VALUES ('','$cname','$ccompany','$caddr1','$caddr2','$ccity', '$cstate','$czip','$ccountry','$isbill','$baddr1','$baddr2', '$bcity','$bzip','$bcountry','$cphone','$cfax','$cemail', '$maillist')";
mysql_query($cpost);
$cnum = mysql_insert_id();
} else {$custdata = mysql_fetch_array($result);$cnum = $custdata['cid'];}
/* now post sale */
$result='';
$sql = 'SELECT * from sales WHERE reltable = '.$class.' AND relitemno = '.$itemno;
$result = mysql_query($sql);
if (mysql_num_rows($result)=="0") {$spost = "INSERT (invnum, relitemno, reltable, soldprice, relcid, paymethod, instructions)";
$spost .= "INTO sales VALUES ('','$itemno','$class','$price','$cnum','$payby','$instruct')";
mysql_query($spost);
$invnum = mysql_insert_id();
} else header('Location: error.php');
/* now go to thankyou page */

I get the following errors... arrgh.

warning: mysql_num_rows() supplied argument is not a valid MySql result Resource on line 50..

and a few more like that... mysql_fetch_array on line 54, the num_rows again on line 59...

Any clue whats wrong?

[edited by: jatar_k at 7:43 pm (utc) on Mar. 5, 2004]
[edit reason] sidescroll [/edit]

justageek

3:35 pm on Mar 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is not complete:

$sql = 'SELECT cid, email, fullname, phone FROM customer where fullname ='.$cname.'AND phone ='.$cphone;

If the phone is a character field then do this:

$sql = "SELECT cid, email, fullname, phone FROM customer where fullname ='".$cname."'AND phone ='".$cphone."'";

But if it's a number then do this:

$sql = "SELECT cid, email, fullname, phone FROM customer where fullname ='".$cname."'AND phone =".$cphone;

Your other errors should go away when the query works. You can see the exact error if you echo out mysql_error($result).

JAG

Bigjohn

4:07 pm on Mar 5, 2004 (gmt 0)

10+ Year Member



Ok. But I think you have your concatenate dots in the wrong place with ref to the " marks.

like THIS:?


$sql = 'SELECT cid, email, fullname, phone FROM customer where fullname ="'.$cname.'" AND phone ="'.$cphone.'"';

Bigjohn

6:12 pm on Mar 5, 2004 (gmt 0)

10+ Year Member



Ok.

I put echo's in the code to show me the SQL that I was building. Then I tried to run the query (cut and paste the ECHO into phpMYadmin SQL block).

I get this 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
'( cid,
fullname,
company,
address1,
address2,
city,
state,
zip,

Here is the code that builds the query:


if (mysql_num_rows($result)=="0") {$cpost= "INSERT (cid, fullname, company, address1, address2, city, state, zip, country, billsame, baddress1, baddress2, bcity, bstate, bzip, bcountry, phone, fax, email, maillist)";
$cpost .= " INTO cdata VALUES ('','$cname','$ccompany','$caddr1','$caddr2','$ccity','$cstate', '$czip','$ccountry','$isbill','$baddr1','$baddr2','$bcity','$bzip', '$bcountry','$cphone','$cfax','$cemail','$maillist')";
mysql_query($cpost);

Where is my syntax error? This is my first INSERT query...

OK, I caught ONE error: INSERT INTO tablename, not INSERT (stuff) INTO tablename. But it's still not workin! DRAT!

John
Thanks in advance!

[edited by: jatar_k at 7:41 pm (utc) on Mar. 5, 2004]

Found it! Thanks for pointing me in the right directions!

John