Lassie Posted February 23, 2008 Share Posted February 23, 2008 I have a function and need to establish whether it executed correctly and if show an error message. I have the following to call the function and check the return, true or false. Is this the way to go? //new customer so create user account and password user_account($first_name,$last_name,$email); if (user_account($first_name,$last_name,$email)!=TRUE) { echo 'A system error occurred.Please contact eBooks4U '; exit(); } //display cart, not allowing changes and without pictures display_cart($_SESSION['cart'], false, 1); The function is function user_account($first_name,$last_name,$email) { //create short variables. $fn=$first_name; $ln=$last_name; $e=$email; //generate password // Create a new, random password. $p = substr ( md5(uniqid(rand(),1)), 3, 10); //register details if ($fn && $ln && $e && $p) { // If everything's OK. //connect to db registration already connected? $connection=reg_connect(); // Make sure the email address is available.Maybe already checked $query = "SELECT user_id FROM users WHERE email='$e'"; $result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error()); if (mysql_num_rows($result) == 0) { // Available. // Create the activation code. $a = md5(uniqid(rand(), true)); //Modify update for password by taking out md5, already done. // Add the user. $query = "INSERT INTO users (email, pass, first_name, last_name, active, registration_date) VALUES ('$e', md5('$p'), '$fn', '$ln', '$a', NOW() )"; $result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error()); if (mysql_affected_rows() == 1) { // If it ran OK. //add from $to = $_POST['email']; $subj = "Activating Your Account"; $mess = "Thank you. You are now registered with e-Books4U<br />To activate your account, please click on this link;<br />\n\n"; $mess .="<a href='http://217.46.159.226/e_cart21/reg/activate.php?x=" . mysql_insert_id() ."&y=$a'>Activate account</a>"; $mess.="<br />Your password for login is, $p\n\n"; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; mail($to,$subj,$mess,$headers); return true; }else{ return false; } } } } Quote Link to comment Share on other sites More sharing options...
drisate Posted February 23, 2008 Share Posted February 23, 2008 try this if (!user_account($first_name,$last_name,$email)){ echo 'A system error occurred.Please contact eBooks4U '; exit(); } Quote Link to comment Share on other sites More sharing options...
Lassie Posted February 23, 2008 Author Share Posted February 23, 2008 Hi Thanks. I tried that and get a failure message. ie 'A sytem...' I checked the function works as expected. Should I return a value and check that? ie return 1;or return 0; Quote Link to comment Share on other sites More sharing options...
drisate Posted February 23, 2008 Share Posted February 23, 2008 you can try it the other way if (user_account($first_name,$last_name,$email)){ // is true // bla bla bla ... }else{ // is false echo 'A system error occurred.Please contact eBooks4U '; } so that way you probably don't need the exit(); anymore Quote Link to comment Share on other sites More sharing options...
Lassie Posted February 23, 2008 Author Share Posted February 23, 2008 I'll try that. Just to understand does if (user_account($first_name,$last_name,$email)){ // is true test for true? Quote Link to comment Share on other sites More sharing options...
drisate Posted February 23, 2008 Share Posted February 23, 2008 yeah you put your code of what to do if true and finish with what to do if false. Much better then exit(); Quote Link to comment Share on other sites More sharing options...
Lassie Posted February 23, 2008 Author Share Posted February 23, 2008 Fine. Many thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.