One block works but not the other

Thought it was too good to last! Smitten with my own success(?) I have also done a form which allows the sending of the same email to either all members or just the committee members. Form contains 2 radio buttons, same name (sendit) but with the values all / admins. For test purposes I am simply listing email addresses and using barebones code on the webserver. Obviously when uploading I shall use prepared statements etc.
Form is processed by a php page. Choosing the all option works perfectly, every member’s email address is listed, no probs. Choosing the admin option produces nothing, zilch, nada, just a blank screen, not even an error message. The only difference is in the sql but I’m sure that’s correct.
Because there are only 2 possible choices I have just written an if for each one. Should I write an else instead? Code follows:

<?php
include('joinin.inc.php');
$conn = dbconnect('query');
//get the radio button the user checked
$alloradmin = $_POST['sendit'];
if ($alloradmin=="all")
{
	//send to all members
$sql = 'select emailadd from members';
$allres = mysqli_query($conn,$sql);
	while ($row =  mysqli_fetch_assoc($allres))
            {foreach ($row as $allmems){echo $allmems."<br/>";} 
}
}
if ($alloradmin=="admins")
{
	//send only to committee members
$somesql = "select emailadd from members where status='cm'";
$someres = mysqli_query($conn,$somesql);
	while ($row =  mysqli_fetch_assoc($someres))
            {foreach ($row as $somemems){echo $somemems."<br/>";} 
}
}
?>

What debugging have you performed? Have you determined what the $_POST data actually is? (var_dump($_POST)) Have you determined if that branch of logic is being executed? (echo a string/message as a test.)

Some points about the posted code -

  1. Use ‘require’ for things your code must have.
  2. Include/require are not functions. The () around the path/filename do nothing and should be left out.
  3. Post method form processing code needs to detect if a post method form was submitted before referencing any of the form data.
  4. You need to trim input data, mainly so that you can detect if all white-space characters were entered, before validating it.
  5. You need to validate all input data before using it. For radio/checkbox fields, they may not be set if nothing was checked. You need to test if a radio/checkbox field isset() before referencing the value. To validate a radio/checkbox field, create an array with the permitted values, then use in_array() to test if the submitted value is or is not one of the permitted values.
  6. Don’t copy variables to other variables for nothing. This is just a waste of typing time.
  7. Don’t use multiple names for the same piece of data. The form field should be named as to what its meaning is. You should then use that name throughout the rest of the code.
  8. If a query doesn’t match any data, you should output a message stating so, rather than outputting nothing.
  9. Don’t create a bunch of bespoke variables for nothing. Just use/reuse simple variable like - $sql, $result, $col(umn).
  10. You should reference database data via associate index names so that your code will continue to work should a database table get rearranged. This also makes your code self-documenting, so that anyone reading the code will know what it is trying to do.
  11. You need to validate the resulting web pages at validator.w3.org
  12. Don’t Repeat Yourself (DRY.) The only thing that’s different between the two blocks of code is the sql query statement. This is the only thing that should be done conditionally. You should then have one instance of the code executing the query, testing if the query matched any data, and looping to use the result from the query.

Hi again PHDR, and thanks (yet again!)
This isn’t the first time this has happened, but what didn’t work on my webserver actually works fine online! (the HTML form checks out OK). There must be some setting on the WS that I hadnt taken into account.
Mike

If you have two different systems and a query with a WHERE … clause doesn’t match data on one of them, either the data doesn’t exist or the datatype/character set/collation is different.

Well, something of an improvement but still not working properly. I took PHDR’s advice and had both the html and php checked. Eventually both were given the OK. I also then tidied the code up as PHDR suggested.
The ‘all members’ radio button works perfectly - all the members’ emails (about 20) are listed, so no probs there.
However, checking the ‘committee members only’ button ALSO lists all the members exactly as in the above example, despite a ‘where’ clause in the query. There should only be 3 records here. I have checked all column names, form element names - including doing a print_r for both radio buttons. Checks out OK as well. This is what we have:

<?php
include('joinin.inc.php');
$conn = dbconnect('query');
//get the radio button the user checked
$alloradmin = $_POST['sendit'];
if ($alloradmin="all")
	{
		$sql = 'select emailadd from members'; //send to all members
		$result = mysqli_query($conn,$sql);
		while($row = $result->fetch_assoc())
		{echo $row['emailadd']." ".$row['lastname']."<br/>";}
		}
else		
	{$sql = "select emailadd from members where memstatus = 'cm'"; //send just to committee
	$result =	mysqli_query($conn,$sql);
	while ($row = $result->fetch_assoc())
		{echo $row['emailadd']." ".$row['lastname']."<br/>";}
		}		
?>

One = is an assignment operator. This is assigning the string “all” to the variable, then testing the result, which is a boolean true. Two == is a comparison operator. This line of code was originally correct. You are randomly making changes to the code without ever looking at, reading, and learning what the syntax means.

You also apparently are still doing this with php’s error related settings turned off, as there’s no way that the code echoing $row['lastname'] is showing anything.

Sorry - I can’t recall how that happened but it must have been today. Of course I know that two equal signs are what goes into a comparison (there were originally two, as you say).
Actually I am testing this on the remote server where the error checks must be on (I haven’t changed them there)
But… of course that did the trick! Both now working as they should. So…
once again, thanks lots!

Sponsor our Newsletter | Privacy Policy | Terms of Service