acctman Posted September 3, 2007 Share Posted September 3, 2007 I need some assistance cleaning up my if statements. I know there is a better way to to write what I'm doing. I'm checking to see if $ref_by is present in the $membtable and if it is present then INSERT into the rate_referrals db. Does the coding look good, and do I have to put an else statement if $ref_by name is not found in the db? if ($_POST['ref_by'] != '') { $ref_by=$_POST['ref_by']; $result = sql_query("SELECT `m_user` FROM $membtable WHERE m_user='".$ref_by."'"); if (sql_num_rows($result) == $ref_by) { sql_query("INSERT INTO rate_referrals (ref_by, ref_mem, ref_status) VALUES ('".$ref_by."', $en['user'], "0")"); } } Quote Link to comment https://forums.phpfreaks.com/topic/67799-solved-if-statement-clean-up/ Share on other sites More sharing options...
acctman Posted September 3, 2007 Author Share Posted September 3, 2007 any idea? Quote Link to comment https://forums.phpfreaks.com/topic/67799-solved-if-statement-clean-up/#findComment-340789 Share on other sites More sharing options...
darkfreaks Posted September 3, 2007 Share Posted September 3, 2007 <?php $ref_by=$_POST['ref_by']; if ($ref_by!) { die("error");} else { $result = sql_query("SELECT `m_user` FROM $membtable WHERE m_user='".$ref_by."'");} if (sql_num_rows($result) == $ref_by) { sql_query("INSERT INTO rate_referrals (ref_by, ref_mem, ref_status) VALUES ('".$ref_by."', $en['user'], "0")"); } }?> Quote Link to comment https://forums.phpfreaks.com/topic/67799-solved-if-statement-clean-up/#findComment-340797 Share on other sites More sharing options...
corbin Posted September 3, 2007 Share Posted September 3, 2007 Umm.... Darkfreaks.... what does 'if ($_POST($ref_by!) {' do? From my understanding, that will try to run $ref_by! through a function which has the name of the $_POST variable, which obviously won't work since $_POST is an array, and that's not what is desired to happen, I would assume.... Also, what's the weird $ref_by! syntax? Never seen that before . Quote Link to comment https://forums.phpfreaks.com/topic/67799-solved-if-statement-clean-up/#findComment-340803 Share on other sites More sharing options...
darkfreaks Posted September 3, 2007 Share Posted September 3, 2007 fixed it my bad! Quote Link to comment https://forums.phpfreaks.com/topic/67799-solved-if-statement-clean-up/#findComment-340804 Share on other sites More sharing options...
darkfreaks Posted September 3, 2007 Share Posted September 3, 2007 it basically says if $variable false=! to die and error. Quote Link to comment https://forums.phpfreaks.com/topic/67799-solved-if-statement-clean-up/#findComment-340806 Share on other sites More sharing options...
hostfreak Posted September 3, 2007 Share Posted September 3, 2007 The not operator needs to be before the variable: if (!$ref_by) Quote Link to comment https://forums.phpfreaks.com/topic/67799-solved-if-statement-clean-up/#findComment-340815 Share on other sites More sharing options...
darkfreaks Posted September 3, 2007 Share Posted September 3, 2007 ah thanks Quote Link to comment https://forums.phpfreaks.com/topic/67799-solved-if-statement-clean-up/#findComment-340822 Share on other sites More sharing options...
acctman Posted September 3, 2007 Author Share Posted September 3, 2007 thanks for all the help, one quick question this section (see below) if I removed "die("error");" the code would still process? the user will not be able to see any errors so I just want it to skip or stop processing this section if $ref_by is empty or does not match 'm_user' from the database if ($ref_by!) { die("error");} Quote Link to comment https://forums.phpfreaks.com/topic/67799-solved-if-statement-clean-up/#findComment-340862 Share on other sites More sharing options...
darkfreaks Posted September 3, 2007 Share Posted September 3, 2007 its if(!$ref_by) { die ("error");} Quote Link to comment https://forums.phpfreaks.com/topic/67799-solved-if-statement-clean-up/#findComment-340866 Share on other sites More sharing options...
darkfreaks Posted September 3, 2007 Share Posted September 3, 2007 <?php if($_POST[whatever]=="") {echo "Sorry But this field was not filled out!"; }?> Quote Link to comment https://forums.phpfreaks.com/topic/67799-solved-if-statement-clean-up/#findComment-340868 Share on other sites More sharing options...
darkfreaks Posted September 3, 2007 Share Posted September 3, 2007 $num_rows = mysql_num_rows($result); Quote Link to comment https://forums.phpfreaks.com/topic/67799-solved-if-statement-clean-up/#findComment-340875 Share on other sites More sharing options...
trq Posted September 3, 2007 Share Posted September 3, 2007 <?php if (isset($_POST['ref_by'])) { $ref_by = $_POST['ref_by']; if ($result = sql_query("SELECT `m_user` FROM $membtable WHERE m_user = '$ref_by'")) { if (sql_num_rows($result)) { sql_query("INSERT INTO rate_referrals (ref_by, ref_mem, ref_status) VALUES ('$ref_by', {$en['user']}, 0)"); } } } Quote Link to comment https://forums.phpfreaks.com/topic/67799-solved-if-statement-clean-up/#findComment-340882 Share on other sites More sharing options...
acctman Posted September 3, 2007 Author Share Posted September 3, 2007 <?php if (isset($_POST['ref_by'])) { $ref_by = $_POST['ref_by']; if ($result = sql_query("SELECT `m_user` FROM $membtable WHERE m_user = '$ref_by'")) { if (sql_num_rows($result)) { sql_query("INSERT INTO rate_referrals (ref_by, ref_mem, ref_status) VALUES ('$ref_by', {$en['user']}, 0)"); } } } I forgot some stuff... minor change in the INSERT query... does that look good? $id = $en['memb_id'] = sql_insert_id(); // from another sql query insert prior to the process below if (isset($_POST['ref_by'])) { $ref_by = $_POST['ref_by']; if ($result = sql_query("SELECT `m_user`, `m_id` FROM $membtable WHERE m_user = '$ref_by'")) { if (sql_num_rows($result)) { sql_query("INSERT INTO rate_referrals (ref_by, ref_mem, ref_status) VALUES ($result['m_id'], $id, 0)"); } } } Quote Link to comment https://forums.phpfreaks.com/topic/67799-solved-if-statement-clean-up/#findComment-340929 Share on other sites More sharing options...
trq Posted September 4, 2007 Share Posted September 4, 2007 Complex variables need to be surrounded by {} when interpolated within quotes. Use... <?php $id = $en['memb_id'] = sql_insert_id(); // from another sql query insert prior to the process below if (isset($_POST['ref_by'])) { $ref_by = $_POST['ref_by']; if ($result = sql_query("SELECT `m_user`, `m_id` FROM $membtable WHERE m_user = '$ref_by'")) { if (sql_num_rows($result)) { sql_query("INSERT INTO rate_referrals (ref_by, ref_mem, ref_status) VALUES ({$result['m_id']}, $id, 0)"); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/67799-solved-if-statement-clean-up/#findComment-340985 Share on other sites More sharing options...
trq Posted September 4, 2007 Share Posted September 4, 2007 Actaully, you never fetch the row either so $result['m_id'] is undeclared. Try... <?php $id = $en['memb_id'] = sql_insert_id(); // from another sql query insert prior to the process below if (isset($_POST['ref_by'])) { $ref_by = $_POST['ref_by']; if ($result = sql_query("SELECT `m_user`, `m_id` FROM $membtable WHERE m_user = '$ref_by'")) { if (sql_num_rows($result)) { $row = sql_fetch_assoc($result); // I can only assume this sql_* thing your using is some sort of wrapper. sql_query("INSERT INTO rate_referrals (ref_by, ref_mem, ref_status) VALUES ({$row['m_id']}, $id, 0)"); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/67799-solved-if-statement-clean-up/#findComment-340986 Share on other sites More sharing options...
acctman Posted September 4, 2007 Author Share Posted September 4, 2007 thanks Quote Link to comment https://forums.phpfreaks.com/topic/67799-solved-if-statement-clean-up/#findComment-340991 Share on other sites More sharing options...
darkfreaks Posted September 4, 2007 Share Posted September 4, 2007 dont forget ot hit topic solved Quote Link to comment https://forums.phpfreaks.com/topic/67799-solved-if-statement-clean-up/#findComment-341014 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.