This is one thing I can't understand. I'm using a function which fetches records from the database. And if that record doesn't exist. It performs an insert query. I even echoed out to ensure that those 2 parameters contains something. And it really contain something. What I can't understand is that it doesn't perform the insert query. I also tested the insert query below on another script which doesn't use function. And it worked.
function fetch_customer($cust, $credit){
$getcnum=query_database("SELECT Cust_Name, CUSID FROM customer_credit WHERE Cust_Name='$cust'","onstor",$link);
if(mysql_num_rows($getcnum)==0){
$fullname=explode(",", $cust);
$lname= $fullname[0];
$fname= $fullname[1];
query_database("INSERT INTO customer_credit(Cust_Name, CREDIT) VALUES('$cust','$credit')",'onstor' ,$link);
echo "customer: ".$cust."<br/>";
echo "credit: ".$credit;
query_database("INSERT INTO customer_table(CLNAME, CFNAME) VALUES('$lname', '$fname')",'onstor',$link);
}
Then I would call the function later on.
fetch_customer($customer, $custcred);
Where do I start if I want to debug this one?
I'm thinking that your insert queries aren't working because, to wit, there is no query_database
function defined (unless it's one that you've defined yourself).
The function that (I believe) you are looking for is mysql_query
.
Here's a link to the docs: mysql_query
Are you sure that it goes into the if statement? mysql_num_rows($getcnum)==0 echo mysql_num_rows($getcnum) and see how many rows contains, I have also notice the single quotes into 'onstor' , could that be a problem in your function?
It's hard to answer without seeing the query_database() function. I'm not sure why you're doing that instead of just using mysql_query("INSERT INTO...") or whatever. It's possible that your query_database() function isn't returning the proper query variable.
The only thing I can tell you is that in the code above, you're missing a closing bracket. The if statement is closed, but the function isn't. That might be the problem.
Good luck.
Here is php code to query mysql
function fetch_customer($cust, $credit){
$link = mysql_connect( 'localhost' , 'username' , 'password' );
mysql_select_db( 'onstor' , $link );
$getcnum=mysql_query("SELECT Cust_Name, CUSID FROM customer_credit WHERE Cust_Name='$cust'");
if(mysql_num_rows($getcnum)==0){
$fullname=explode(",", $cust);
$lname= $fullname[0];
$fname= $fullname[1];
mysq_query("INSERT INTO customer_credit(Cust_Name, CREDIT) VALUES('$cust','$credit')");
echo "customer: ".$cust."<br/>";
echo "credit: ".$credit;
mysql_query("INSERT INTO customer_table(CLNAME, CFNAME) VALUES('$lname', '$fname')");
}
You are passing the $link
to the database in your query_database($sql, $db, $link)
, but it is not defined anywhere. So it must be a global variable, that you forget to declare. So, declare $link
as being global:
function fetch_customer($cust, $credit)
{
global $link;
// rest of your function
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.