Forum Moderators: coopster
/* 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]
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
>>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] returnson success andTRUEon error. A non-FALSEreturn 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.FALSE
Launch a page? Just exit the parser and start into your HTML...
?>
<html>
<head>
...
$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.
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>
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
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]
$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
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