-1

I have a form that uses a radio button, this button decides if it shows a piece of data or not:

<input type="radio" name="IncludeData" value="1" />Yes
<input type="radio" name="IncludeData" value="0" />No

Yes = 1 and No = 0.

I'm then using this query to call the data:

$IncludeData = $_POST['IncludeData'];
$sql = "SELECT data1, data2, data3, IF($IncludeData='0',Null,data4), data4, data5 FROM Database ORDER BY data1 asc";

I've tried following the answer from this question: MYSQL hide field data if value in another field is set.

However, it always seems to keep the data no matter what I select on the radio button. Any help would be greatly appreciated.

This is where I grab the data from:

<td>
Sort table on =
<select name="Sort">
<option value="data1" selected="selected">Example1</option>
<option value="data2">Example2</option>
<option value="data3">Example3</option>
<option value="data4">Example4</option>
<option value="data5">Example5</option>
</select>
</td>
Community
  • 1
  • 1
Joe
  • 4,877
  • 5
  • 30
  • 51
  • You have `data4` in the field after that as well. So when you select `No`, you'll get `data1, data2, data3, NULL, data4, data5`, and when you select `Yes` you'll get `data1, data2, data3, data4, data4, data5`. – Barmar Jan 13 '17 at 22:38
  • Maybe you meant that to be an alias for this `IF`, so it should be `IF(...) AS data4`. – Barmar Jan 13 '17 at 22:39
  • I thought that at first too @Barmar. But when I removed `data4` from after the IF statement, nothing showed at all. Not even when I selected yes... – Joe Jan 13 '17 at 22:40
  • How are you showing this field? Have you given an alias to the `IF()` so that you can refer to it by name? – Barmar Jan 13 '17 at 22:41
  • You need to post more code so we can see how you're accessing the result of the query. – Barmar Jan 13 '17 at 22:42
  • Just added the code to where I grab the data from @Barmar – Joe Jan 13 '17 at 22:44
  • Please, please, don't ever embed user-provided variables directly into your SQL query. (I hope this was only for demonstration purposes!) See, e.g., http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – alttag Jan 13 '17 at 22:45
  • Don't worry @alttag ! This is demo purposes only. Thank you though :) – Joe Jan 13 '17 at 22:47
  • @thickguru I meant the code that processes the result of the query, e.g. `$data4 = $row['data4']` – Barmar Jan 13 '17 at 22:49

1 Answers1

3

What about something like this?:

$IncludeData = $_POST['IncludeData'] ? 'data4, ' : '';
$sql = "SELECT data1, data2, data3, $IncludeData data5 FROM Table ORDER BY data1 ASC";
aperpen
  • 718
  • 7
  • 10
  • you beat me to it – RST Jan 13 '17 at 22:42
  • Sorry but no. `$IncludeData` is not associated to the field, it is only associated to 0 and 1. So yes the data is included, or no, the data is not. – Joe Jan 13 '17 at 22:46
  • I know, with that code if $includeData is 1, data4 column will be included and if $includeData is 0, data4 won't be included... – aperpen Jan 13 '17 at 22:48
  • Sorry! Mis-read it the first time! That worked like a dream. Thank you very much – Joe Jan 13 '17 at 22:52