Jump to content

[SOLVED] If Statement clean up...


acctman

Recommended Posts

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")");
    }
    	}

Link to comment
https://forums.phpfreaks.com/topic/67799-solved-if-statement-clean-up/
Share on other sites

<?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")");
    }
    	}?>

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 :).

 

 

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");}

<?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)");
    }
  }
}

<?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)");
}
}
  }

 

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)");
    }
  }
}
?>

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)");
    }
  }
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.