PHP: Determine whether its a checkbox is checked or not - php

My checkbox looks like this:
<input type="checkbox" name="activate[]" class="setSetting" value="<?php echo $row["id"]; ?>">
And then i have a foreach:
$activate = $_POST['activate'];
foreach($activate as $a){
echo $a ."<br>";
}
Works fine to get the value out of. But how can i determine if the checkbox has been checked?
$activate = $_POST['activate'];
foreach($activate as $a){
$query_email = mysql_query("SELECT id FROM lp_email_settings ORDER BY id ASC");
while($ro = mysql_fetch_row($query_email)){
$getUserSettings = mysql_query("SELECT * FROM users_email_settings WHERE uID = '$USER' AND eSetting = '$ro[0]'");
if($ro[0] == $a){
if(mysql_num_rows($getUserSettings) != 1){
mysql_query("INSERT INTO users_email_settings (uID, eSetting) VALUES ($USER, $ro[0])");
}
}else{
mysql_query("DELETE FROM users_email_settings WHERE uID = '$USER' AND eSetting = '$ro[0]'");
}
}
echo $a."<br>";
}

Only those checkboxes will be submitted that are considered successful (i.e. checked). That means only the checked checkboxes are available in $_POST['activate'].
And to determine whether a checkbox has been checked, you can use array_search to check whether a particular ID value is in $_POST['activate']:
array_search('12345', $_POST['activate'], true)
And if you change your control’s name to use the ID as key like this:
<input type="checkbox" name="activate[<?php echo $row["id"]; ?>]" class="setSetting" value="<?php echo $row["id"]; ?>">
Then you can simply use isset or array_key_exists on $_POST['activate']:
isset($_POST['activate']['12345'])
array_key_exists('12345', $_POST['activate'])
Edit    As already said in the comments, you should rather iterate the available options and check for each option whether it’s already active and needs to be activated or deactivated. You can do this as follows:
$activate = array_flip($_POST['activate']);
$query = "SELECT t1.id, t2.eSetting
FROM lp_email_settings t1 LEFT OUTER JOIN users_email_settings t2 ON (t1.id = t2.eSetting)
ORDER BY t1.id ASC";
$result = mysql_query($query);
$insert = array();
$delete = array();
while ($row = mysql_fetch_row($result)) {
if ($row[1] === null) {
// option is not set yet
if (isset($activate[$row[0]])) {
// option needs to be set
$insert[] = $row[0];
}
} else {
// option is already set
if (!isset($activate[$row[0]])) {
// option needs to be unset
$delete[] = $row[0];
}
}
}
if (!empty($insert)) {
$query = "INSERT INTO users_email_settings (uID, eSetting)
VALUES ($USER, " . implode("), ($USER, ", $insert) . ")";
mysql_query($query);
}
if (!empty($delete)) {
$query = "DELETE FROM users_email_settings
WHERE uID = $USER AND eSetting IN (" . implode(", ", $delete) . ")";
mysql_query($query);
}
The first query will select a left join of all available options and the already active options. So the result set is the ID of the option in the first column and the second column is either NULL if the options is not active or otherwise again the ID. So if there are three options (e.g. 1, 2, and 3) and only the options 1 and 3 are already set, the result should look like this:
id | eSetting
----+----------
1 | 1
2 | NULL
3 | 3
So if $row[1] is null (inactive option), the ID in $row[0] is added to the $insert array if the corresponding option was set in the request ($activate[$row[0]], the keys/values are flipped to have a direct access). The same is done for those options that are already active but were not set in the request.
At the end the collected options in $insert are inserted and those in $delete are removed.

If you get a value it has been checked, otherwise you get nothing...
Here is a pretty good tutorial explaining how it works: http://www.html-form-guide.com/php-form/php-form-checkbox.html

Checkboxes will only be submitted at all if they are ticked. If they are unticked, PHP won't see them in $_POST.
I assume since you're using square brackets (ie activate[]) that you have more than one of them all with the same name. This will make it very hard to tell which ones were ticked, since you'll just get an array of the ones which were ticked; you'll know how many, but not which ones.
To get around this, you either need to give them all different names, or specify the array key for each one in the HTML code - ie name="activate[4]" or name="activate[bob]". Depending on the context, this could be an ID of the data you're activating, or the name of the feature, or anything else you decide, as long as it's unique for each field.
You will then still get the array only containing the ones which were ticked, but you will be able to tell which ones they were in your foreach loop by looking at the array key.
Hope that helps.

You can check against each submitted value ($activate) to see if it does actually contain anything/fits your criteria:
$activate = $_POST['activate'];
foreach($activate as $a){
if($activate){
echo $a ."<br>";
}
}
Though the array should only contain those values checked.

Related

Which is the Fastest method to check Rows existence while fetching?

From the following Queries, Which one is the most optimal and fastest to use?
[COUNT(id)]
$SQL = "SELECT name, COUNT(id) as Count FROM names WHERE name = :name";
$row = $stmt->fetch();
if ($data['count'] > 0) {
while ($row) {
$name = $row['name'];
}
} else {
return;
}
OR [rowCount()]
$SQL = "SELECT name FROM names WHERE name = :name";
if ($stmt->rowCount() > 0) {
while ($row = $stmt->fetch()) {
$name = $row['name'];
}
} else {
return;
}
OR [EXISTS]
$SQLEX = "SELECT EXISTS (SELECT name FROM names WHERE name = :name LIMIT 1)";
if ($stmt->fetchColumn == 1) {
$SQL = "SELECT name FROM names WHERE name = :name";
while (row = $stmt->fetch()){
$name = $row['name'];
}
} else {
return;
}
OR [RAW]
$SQL = "SELECT name FROM names WHERE name = :name";
$row = $stmt->fetch();
if ($row) {
while($row) {
$name = $row['name'];
}
} else {
return;
}
Also i wanted to know, Why does using $stmt->fetch() with $stmt->rowCount() allows me to fetch data, But using it with $stmt->fetchColumn doesn't?
First, if you have an index on names(name), then all should be quite comparable in speed.
Second, it is always worth trying such performance tests on your own system.
Third, if names are declared as unique (or primary key) in the names table, then all should be quite fast.
In general, though, the fastest way to determine if a row is available is:
SELECT EXISTS (SELECT name FROM names WHERE name = :name)
The LIMIT 1 in the subquery is unnecessary -- EXISTS stops at the first row (whether the database uses an index or a table scan).
In general, the first method using an aggregation is the worst solution. Without an index, it is going to result in a full table scan that reads the entire table. The second might or might not read the entire table, depending on whether the database starts returning matching rows as they are available. It also has the downside of returning more data.
Ok, it seems this question needs more than one answer...
f you need to check the existence only,
if there is an unique index for the field, all methods are equal, but some of them just make no sense.
if there is no unique index, then go for EXISTS
If you need to fetch the actual data and see if there was anything returned, then just select your data and fetch it:
if only one column from a single row is expected, then use fetchColumn()
if only one row is expected, then use fetch()
if multiple rows are expected, then use fetchAll()
and then use the resulting value to see whether your query returned any data.
So if you finally made your mind as to what you're asking about, here is the most optimal code for you:
$SQL = "SELECT name FROM names WHERE name = :name";
$data = $stmt->fetchAll(PDO::FETCH_COLUMN);
if (!$data) {
return;
}
foreach ($data as $name) ...
And there is nothing wrong with fetchColumn() other than your idea to use it.

Create a "Secret Santa" generator using MySQL and PHP

I am trying to create a Secret Santa system using a PHP page and a MySQL database to store the details so if someone forgets their match they can re-request it.
Step 1: I created a random number generator based on the number of people in the list in the database.
Count Function:
$maxSQL = "SELECT COUNT(id) as total FROM secretsanta";
$maxRS = mysqli_query($conn, $maxSQL);
$maxQuery = mysqli_fetch_array($maxRS);
$maxpersons = $maxQuery['total'];
Then the Random Number Generator:
$assigned = rand(1,$maxpersons);
Step 2: Test if the random number matches the persons own id and regenerate a new number if true.
do {
$assigned = rand(1,$maxpersons);
} while ($assigned==$id);
Step 3: Write the paired id to the persons database record.
$assignSQL = "UPDATE secretsanta SET assigned = '".$assigned."' WHERE secretsanta.id = ".$id;
if (mysqli_query($conn, $assignSQL)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
The Problem: Now I need to check that other people aren't assigned to that person or otherwise some could miss out and others would get more than others.
I tried to implement a function that contained a query to test each record to see if that number already existed and was hoping to add it as a condition to perhaps a while or do while statement?
if (!function_exists('checkRandom')){
function checkRandom($funcid){
$Check_SQL = "SELECT assigned FROM secretsanta ORDER BY id ASC";
$Check_RES = mysqli_query($conn, $Check_SQL);
if (Check_RES) {
while ($CheckArray = mysqli_fetch_array($Check_RES, MYSQLI_ASSOC)) {
$CheckAsgn = $CheckArray['assigned'];
if ($funcid==$CheckAsgn) {return true;}else{return false;}
}
}
}
}
Then implement it into the do while statement like this:
do {
$assigned = rand(1,$maxpersons);
} while ($assigned==$id||checkRandom($assigned));
No luck so far...HELP!.. please :)
P.S. I know there are websites that already do this, I just don't trust them to give out mine and family email address' if I can make my own private version myself.
Using your method, the first few assignments will be done with no problem, but imagine the last unassigned entry and how many times it will try a random number only to find the person with that id is already assigned..
I'm gonna give you another approach to your problem: for each user that you want to assign a santa to, make a new SELECT statement with a WHERE clause that lets you select only those users that are not assigned yet.
check out my code and see if that helps you. I just typed this and didnt test it so there could be some mistakes.
// load all unassigned users into an array
$unassignedUsers = [];
$query = "SELECT id, assigned FROM secretsanta WHERE assigned is NULL";
$res = mysqli_query($conn, $query);
while($row = mysqli_fetch_assoc($res){
$unassignedUsers[] = $row;
}
if(count($unassignedUsers) == 1){
echo 'There is only 1 unassigned user. Therefore he cannot be matched';
} else {
// for loop for each user in DB that is not assigned yet
//for ($i = 1;$i <= count($unassignedUsers); $i++){
$i = 0;
foreach($unassignedUsers as $user)
// if its the second-to-last iterations of the for-loop, check for legality of the last one
if(count($unassignedUsers) - $i == 1){
$lastUserID = $unassignedUsers[count($unassignedUsers)-1]['id'];
$query = "SELECT id FROM secretsanta WHERE assigned is NULL AND id = ".$lastUserID;
$res = mysqli_query($conn, $query);
$rowcount = mysqli_num_rows($res);
if ($rowcount){
// last user is still unassigned
$query = "UPDATE secretsanta SET assigned = '".$lastUserID."' WHERE id = ".$user['id'];
if(mysqli_query($conn, $query)){
echo "Record with id ".$user['id']." updated successfully";
} else {
echo "Error updating record: ".mysqli_error($conn);
}
}
} else {
// select all unassigned users
$unassignedIDs = [];
$query = "SELECT id FROM secretsanta WHERE assigned is NULL AND id <> ".$user['id'];
$res = mysqli_query($conn, $query);
while($row = mysqli_fetch_assoc($res){
$unassignedIDs[] = $row['id'];
}
// get a random id from $unassignedIDs
$randomIndex = rand(0, count($unassignedIDs)-1);
$randomID = $unassignedIDs[$randomIndex];
// assign $randomID to user
$query = "UPDATE secretsanta SET assigned = '".$randomID."' WHERE id = ".$user['id'];
if(mysqli_query($conn, $query)){
echo "Record with id ".$user['id']." updated successfully";
} else {
echo "Error updating record: ".mysqli_error($conn);
}
}
$i++;
}
}
last edit: refactored whole code so it is able to be run multiple times and only assigns new users who are not assigned yet.
Step 1 is dependent on have a contiguous set of ids for the people. Think what happens if '3' leaves the company and it hires 6 to replace them....1,2,4,5,6 ($maxpersons=5)
"Now I need to check" - no you are still trying to solve the problem by guessing then seeing if your guess worked. Use an algorithm which is always going to return a correct result. The method below requires the addition of a temporary field 'sequence' of type float.
mysqli_query($conn,"UPDATE secretsanta SET sequence=RAND()");
$first=false;
$prev=false;
$all=mysqli_query($conn, "SELECT * FROM secretsanta ORDER BY sequence, id");
while ($r=mysqli_fetch_assoc($all)) {
if (false===$first) {
$first=$r['id'];
} else {
save_pair($prev, $r['id']);
}
$prev=$r['id'];
}
save_pair($prev, $first);
(but with better error checking)

2 for each statements at same time?

I recently have been trying to make a way to easily add more fields onto my form without having to go back and add more rows to my database structure. So, to begin working on this, I created a table where the structure is this:
OptionTitle
Option1
Option2
Option3
Option4
Option5
Option6
As you can see, it goes up to 6 options, and OptionTitle is the label name of the form. Then I made another table, one that reflects the users input of the previous table. This table is named usersoption
fid
OptionTitle
Option1
Ok, so FID reflects which form it is referencing to. This way, when displaying the submitted form, it'll pull information from this table where the FID is the same. OptionTitle is the label of the form, and Option1 is the option the user submitted.
Now, onto the form where it actually includes the options to select from. Here is a simplified version of how my code is included:
$query100 = $db->query("SELECT * FROM options WHERE fid='" . $id . "'");
while($row2 = mysqli_fetch_array($query100))
{
echo "
<div class=\"divform\" id=\"optiontitle\">
<label for=\"optiontitle\">$row2[optiontitle]:</label>
<select name=\"option1[]\" id=\"option1\">";
echo "<option value='$row6[option1]'>$row6[option1]</option>";
echo "<option value='$row6[option2]'>$row6[option2]</option>";
echo "<option value='$row6[option3]'>$row6[option3]</option>";
echo "<option value='$row6[option4]'>$row6[option4]</option>";
echo "<option value='$row6[option5]'>$row6[option5]</option>";
echo "<option value='$row6[option6]'>$row6[option6]</option>";
echo "
</select>
</div>
";
}
As you can see, the select name is option1[]. This is so I can have multiple select fields on the same form, and in return this will bring over the multiple difference select fields onto the submitted process. So now onto where my issue is, in the submission process. Here is what I have so far:
foreach($_POST['option1'] as $val){
$val = $db->escape_string($val);
$query30 = $db->query("INSERT `usersoption` SET `gid` = '".$id."', `fid` = '".$fid."', `optiontitle` = 'Where OptionTitle should go', `option1` = '$val'")or die( mysqli_error());
}
As you can see, I can successfully bring the option through a foreach statement. What I can't do, is bring in the OptionTitle. It seems almost unnecessary to bring in the OptionTitle, but it is necessary for the person reading the submitted form to know which option was being submitted. I'm not sure how to carry the OptionTitle over, it seems simple but all my attempts failed miserably. I did some research and one of the suggestions was to create a hidden input with the name and carry it over that way. Here is the addon that would be in the form:
<input type=\"hidden\" name=\"optiontitle[]\" value=\"test\">
This would be added on to the form and then carried over, but the issue is how do I bring it over? I would need to do a multiple foreach statement which does not work. For example, here was what I tried to bring over (it did not work):
foreach($_POST['option1'] as $val) && ($_POST['optiontitle'] as $val2)){
$val = $db->escape_string($val);
$val2 = $db->escape_string($val2);
$query30 = $db->query("INSERT `usersoption` SET `gid` = '".$id."', `fid` = '".$fid."', `optiontitle` = '$val2', `option1` = '$val'")or die( mysqli_error());
}
Have you tried giving your option array a key?
echo "<select name=\"option1[$row2[optiontitle]]\" id=\"option1\">";
Then change your foreach to:
foreach($_POST['option1'] as $title=>$val)
You can use key in foreach to access more array:
Try this code:
foreach($_POST['option1'] as $key=>$val){
$val = $db->escape_string($val);
$val2 = $db->escape_string(isset($_POST['optiontitle'][$key])?$_POST['optiontitle'][$key]:'');
$query30 = $db->query("INSERT `usersoption` SET `gid` = '".$id."', `fid` = '".$fid."', `optiontitle` = '$val2', `option1` = '$val'")or die( mysqli_error());
}
For the hidden input solution: Just do each query as you normally would, but add $_POST['optiontitle']:
foreach($_POST['option1'] as $val){
$val = $db->escape_string($val);
$query30 = $db->query("
INSERT `usersoption` SET
`gid` = '".$id."',
`fid` = '".$fid."',
`optiontitle` = '".$_POST['optiontitle']."',
`option1` = '$val'
")or die(mysqli_error());
}
By the way you should read up on prepared statements. These allow you to sanitise your data before inserting into the database. They are essential to good coding practise.

Seeing if variable is in array

I am using a MySQL database. I am completely sure that the ID does actually exist in the database. Why is it going to the last else (where is says //incorrect id) ?
<?php
//Localise user id.
$userid = $_SESSION['userid'];
//Get content of the article.
$sql = "SELECT * FROM articles WHERE creatorid = '$userid'";
$result = mysql_query($sql) or die(mysql_error()); //Execute. If fails, show error.
$array = mysql_fetch_array($result);
if(in_array($articleid, $array)) //If the URL id exists in the database (array)
{
//The article does actually exist for that user. They requested it.
$sql = "SELECT * FROM articles WHERE id = '$articleid'";
$result = mysql_query($sql) or die(mysql_error()); //Execute. If fails, show error.
$array = mysql_fetch_array($result);
$content = $array['content'];
if($content != '') //If the article has actually been written.
{
include($_SERVER['DOCUMENT_ROOT'] . '/includes/renderimage.php');
} else
{
//Article actually hasn't been written.
}
} else
{
//Incorrect ID.
}
?>
You're only looking in the first row that's returned. You need to call mysql_fetch_array in a loop to get each row. Also, you shouldn't use in_array(), since the article ID might appear in some other column (what if you're checking for article #3 and user #3?).
But if you just want to see if the article was created by this user, you can use a different query:
SELECT * FROM articles WHERE creatorid = '$userid' AND articleid = '$articleid';
This should return either 0 or 1 row depending on whether the user created the article. You can then use mysql_num_rows() to test for this.
It appears you are accessing the array incorrectly. On top of that you are returning multiple articles if the creator posted more than one so your in_array() is totally invalid. Change the limit on your query to one record (LIMIT 0,1) and access the creator id by calling:
$result[0]->creatorid or $result['creatorid']
depending on how your resource is queried

Delete database entry if checkbox is unchecked

I have a form that allows engineers to be added to a job by checking checkboxes which works fine. However, I would like the user to remove an engineer by unchecking their checkbox on another form.
The table (a many to many table) has 3 columns id_ce (the primary key), call_ce (foreign key) and engineer_ce (foreign key). I would like the form to check if a checkbox is empty and if it is check to see if there is an entry in the table with the call_ce and engineer_ce and delete it if it exists but I'm failing miserably.
This is what I have so far...
foreach($_POST['engineer'] as $engineer_id){
if(!isset($_POST['engineer'])){
$sql = "SELECT * FROM calls_engineers WHERE call_ce = '$diary_id' AND engineer_ce = '$engineer_id'";
$result = mysql_query($sql)or die(mysql_error());
if(mysql_num_rows($result) > 0){
$sql = "DELETE FROM calls_engineers WHERE engineer_ce = '$engineer_id'";
$result = mysql_query($sql)or die(mysql_error());
}
}
}
I suspect the problem may be something like the checkboxes that aren't checked don't post so they aren't going through the foreach loop but I'm not sure of another way of doing it.
Any help will be greatly appreciated
You can put on page a hidden field with engineer ids. Say, it will be called engineer_on_page
Now you can
foreach($_POST['engineer_on_page'] as $engineer_id) {
if(!in_array($engineer_id, $_POST['engineer'])){
//do delete here
}
}
I expect that checkboxes looks similar to this:
<input type="checkbox" name="engineer[]" value="[engineer_id]"/>
and hidden field
<input type="hidden" name="engineer_on_page[]" value="[engineer_id]"/>
Also you may specify checkbox field like this:
<input type="checkbox" name="engineer[[engineer_id]]" value="[engineer_id]"/>
where [engineer_id] is actual id (1, 2,3 etc)
In second case you may use
if(!isset($_POST['engineer'][$engineer_id])){
//do delete here
}
/* BY ROHIT BHAYAL */
$checkbox1=$_POST['stu2'];
$chk="";
/* print_r($_POST['stu3']); */
$order1=$_POST['stu3'];
if(isset($_POST['subject']))
{
$string = implode(',', $checkbox1);
echo $del="delete from student_subject where sub_id not in ($string)";
mysql_query($del);
}
// mysql_query($del);
foreach ($checkbox1 as $key => $chk1)
{
$chk .= $chk1.",";
$check="SELECT * FROM student_subject WHERE sub_id = '".$chk1."' AND form_id='".$_GET['sub']."'";
$rs = mysql_query($check);
$data = mysql_fetch_array($rs);
mysql_num_rows($rs);
if($data[0] > 1)
{
echo "Subject is Already Exists<br/>";
}
else
{
if(isset($_POST['subject']))
{
$a= "sub_id='".$chk1."'";
echo $ins = "insert into student_subject set form_id='".$_POST['stu1']."',
".$a.",
order_no='".$chk1."'";
}
if (mysql_query($ins))
{
echo "Now New subject Is Added<br/>";
}
else
{
echo "Error adding user in database<br/>";
}
}
}
?>

Categories