checkboxes generated dynamically not inserting all values into array - php

I have a query that selects all of the users in a database that are not approved.
$query = "SELECT * FROM users WHERE approved = '0';
$data = mysqli_query($dbc, $query);
Then, what I want to do is display that list of people in a table that has a column with a checkbox to approve that person.
<table class="table mb-none">
<thead>
<tr>
<th>Picture</th>
<th>Username</th>
<th>Profile Type</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Phone Number</th>
<th>Approve</th>
<th>Deny/Delete</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_array($data)){
echo '<tr>'
echo '<td><img width="70px" height:"90px" src="' . EHS_UPLOADPATH_STUDENTS . $row['picture'] . '"/></td>';
echo '<td>' . $row['username'] . '</td>';
echo '<td>Student</td>';
echo '<td>' . $row['first_name'] . '</td>';
echo '<td>' . $row['last_name'] . '</td>';
echo '<td>' . $row['email'] . '</td>';
echo '<td>' . $row['phone_number'] . '</td>';
echo '<td style="text-align:center; padding:20px; background-color:#DFF0D8;">';
echo '<input type="checkbox" name="approve[]" value = "'.$row['user_id'].'"></td>';
echo '<td style="text-align:center; padding:20px; background-color:#FCDEDE;"><button type="button" class="mb-xs mt-xs mr-xs btn btn-danger">Deny/Delete</button> </td>';
echo '</tr>';}
This is how I am trying to process this:
if(isset($_POST['submit'])){
if(!empty($_POST['approve'])){
$users = $_POST['approve'];
foreach($users as $user){
$query = "UPDATE users SET approved = '1' WHERE user_id = '". $user . "'";
mysqli_query($dbc, $query);}}} ** here goes the rest of the code
Now, the problem I am having is that when I click on the "approve" checkboxes and hit "Submit" only the first item in the array approve[] is being processed and not the rest. I have no idea why. I have been thinking about this for the last 2 hours and can't figure it out.

try the following approach :
if(isset($_POST['submit'])){
if(!empty($_POST['approve'])){
foreach($_POST['approve'] as $user){
$query = "UPDATE users SET approved = '1' WHERE user_id = '". $user . "'";
mysqli_query($dbc, $query);}}} ** here goes the rest of the code
If that does not work ,do a print_r($_POST['approve']) and see what the contents of the array are.

I found the solution... I put the closing bracket for the "foreach" in the wrong place. Thanks for your help!

Related

Show table with results grouped 3 by 3

I am trying to show results that I get from a SQL table, it is this:
what I want to do is show results 3 by 3, like this:
I mean a table for every 3 results that the "assigned_bank" field matches, and if there are 4 results with the same number in "assigned_bank", I also show it in that same table, that is; one table for each different "assigned_bank" id.
I've been trying most of the day and the closest thing I've come to is this:
This is my last code:
<?php
$tables = sizeof($search) / 3;
for ($i = 0; $i < $tables; $i++) {
?>
<table class="table customers">
<thead class="thead-blue">
<tr>
<th scope="col-xs-2">Name</th>
<th scope="col-xs-2">Lastname</th>
<th scope="col-xs-2">Bank ID</th>
</tr>
</thead>
<tbody>
<?php
foreach ($search as $item){
echo '<tr align="left">';
echo '<td class="col-xs-2">' . $item["p_name"] . '</td>' . "\r\n";
echo '<td class="col-xs-2">' . $item["p_lastname"] . '</td>' . "\r\n";
echo '<td class="col-xs-2">' . $item["assigned_bank"] . '</td>' . "\r\n";
echo '</tr>';
}
?>
</tbody>
</table>
<?php
echo "\r\n";
}
?>
Thank you very much for any possible help or comments and thank you for taking the time to respond.
<?php
$result = array();
foreach ($search as $key => $item) {
$result[$item['assigned_bank']][$key] = $item;
}
foreach($result as $key=>$search_items){
echo '<table class="table customers" border="2" >
<thead class="thead-blue">
<tr>
<th scope="col-xs-2">Name</th>
<th scope="col-xs-2">Lastname</th>
<th scope="col-xs-2">Bank ID</th>
</tr>
</thead>
<tbody>';
foreach($search_items as $skey=>$item){
echo '<tr align="left">';
echo '<td class="col-xs-2">' . $item["p_name"] . '</td>' . "\r\n";
echo '<td class="col-xs-2">' . $item["p_lastname"] . '</td>' . "\r\n";
echo '<td class="col-xs-2">' . $item["assigned_bank"] . '</td>' . "\r\n";
echo '</tr>';
}
echo '</tbody>
</table>';
}
<?>
You can use order by on assigned_bank column with ascending order:
SELECT p_name, p_lastname, assigned_bank FROM your_table order by
assigned_bank asc

Pass value from one page to another in PHP without using URL

I used to pass value from one page to another using session. E.g
page1.php
<?php
session_start();
$_SESSION["id"] = $id;
?>
page2.php
<?php
session_start();
echo $_SESSION["id"];
?>
But for the following case i don't know how can i pass the id to the next page. This code is used to display group of people and the action column have an option to view the people details.
<table>
<thead>
<tr>
<th>Name</th>
<th>Status</th>
<th>Created</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$DBSystemAccess = dbSelectByWhere("SystemAccess", "", "ORDER By Timestamp");
while ($SystemAccessDB = dbFetchArray($DBSystemAccess)) {
?>
<tr>
<?php
echo '<td>' . dbGroupNamebyId($_SESSION['PI_ID']) . '</td>';
echo '<td>' . $Status . '</td>';
echo '<td>' . $SystemAccessDB['timeStamp'] . '</td>';
echo '<td>' . '<a href="adetails.php?ID=' . $SystemAccessDB['id'] . '" >View</a> </td>'
?>
</tr>
<?php } ?>
</tbody>
How can i replace <a href="adetails.php?ID=' . $SystemAccessDB['id'] . '" >View</a> to not showing ID in the URL?
Please try this with the hyper link it is not possible to hide the GET data.We need to use POST method.
<table>
<thead>
<tr>
<th>Name</th>
<th>Status</th>
<th>Created</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$DBSystemAccess = dbSelectByWhere("SystemAccess", "", "ORDER By Timestamp");
while ($SystemAccessDB = dbFetchArray($DBSystemAccess)) {
?>
<tr>
<?php
echo '<td>' . dbGroupNamebyId($_SESSION['PI_ID']) . '</td>';
echo '<td>' . $Status . '</td>';
echo '<td>' . $SystemAccessDB['timeStamp'] . '</td>';
echo '<form name="test" method="post" action="testing.php">';
echo '<input type="hidden" name="hidden_name" value="'.$SystemAccessDB['id'].'"/>';
echo '<button type="submit">View</button>';
echo '</form>';
?>
</tr>
<?php } ?>
</tbody>
When you will click it will redirect to testing.php with hidden value 'hidden_name' on which you can further query according to that value.

Update value if the checkbox if checked multiple rows php

I am trying to update each row with a value of 1 if the checkbox is checked and do nothing if not.
$query= "SELECT Name, Surname, Age, Club, age_group, School, team_select FROM players WHERE Age < 9";
$statement = $db->prepare($query);
$statement->execute();
$players = $statement->fetchAll(PDO::FETCH_ASSOC);
echo '<form nethod="POST" action="add_to_team.php">';
echo '<p align="center"><a href="new.php" >Add Player</a></p>';
echo '<table class="table table-bordered"';
echo '<tr><th>Name</th>
<th>Surname</th>
<th>Club</th>
<th>Age Group</th>
<th>School</th>
<th>Edit</th>
<th>Delete</th>
<th>Add to Team</th></tr>';
// loop through results of database query, displaying them in the table
foreach ($players as $player) {
// echo out the contents of each row into a table
echo '<tr>';
echo '<td>' . $player['Name'] . '</td>';
echo '<td>' . $player['Surname'] . '</td>';
echo '<td>' . $player['Club'] . '</td>';
echo '<td>' . $player['age_group'] . '</td>';
echo '<td>' . $player['School'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo '<td><input type="checkbox" name="team_select" value="1"> </td>';
echo '</tr>';
}
echo '</table>';
echo'<input type="submit" value="Submit" name="submit"><br/>';
Code (It just won't work)
<?php
include 'connect.php';//database connection
isset($_POST['team_select'])
?>
You need to give the checkboxes different names. If they're all named team_select there will just be one $_POST['team_select'], but you won't be able to tell which checkboxes were checked.
Use name="team_select[]" and they'll all be put into an array. Then you can put the player name into the value, so the array will contain the names of all the players who should be added.
echo '<td><input type="checkbox" name="team_select[]" value="' . $player['Name'] . '"> </td>';
When you're processing the form, you can do:
foreach ($_POST['team_select'] as $name) {
...
}
to process all the players who were selected.

Having an issue with deleting entry from database on my website

I have echoed my database table onto my website and have tick option next to the table where you can tick it and delete any of the entries from the website, it should delete it from the database. For some reason its not working, I'm a beginner and any help would be greatly appreciated thanks.
<form method="" action="tester.php">
<?php
include 'connect_to_mysql.php';
$count=mysql_num_rows($result);
$result = mysql_query("SELECT * FROM booking ORDER BY ID ASC");
echo "<table border='1'>
<tr>
<th>DEL</th>
<th>Name</th>
<th>Email ID</th>
<th>Phone Number</th>
<th>Collection Address</th>
<th>Collection Date</th>
<th>Collection Time</th>
<th>Make & Model</th>
<th>Message</th>
</tr>";
while($row = mysql_fetch_array($result))
{
?>
<td align="center" bgcolor="#FFFFFF"><input name="delete_these[]" type="checkbox" id="checkbox[]" value="<?php echo $row['ID']; ?>"></td>
<?php
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['phonenumber'] . "</td>";
echo "<td>" . $row['collectionaddress'] . "</td>";
echo "<td>" . $row['collectiondate'] . "</td>";
echo "<td>" . $row['collectiontime'] . "</td>";
echo "<td>" . $row['makemodel'] . "</td>";
echo "<td>" . $row['message'] . "</td>";
echo "</tr>";
}
echo "</table>";
?> <br>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?php
// Check if delete button active, start this
if(isset($_GET['delete'])) {
for($i=0;$i<$count;$i++){
$id = $checkbox[$i];
print_r($_GET['delete_these']);
$ids = implode(', ', $_POST['delete_these']);
$sql = "DELETE FROM booking WHERE id IN($ids)";
echo "<br />SQL: $sql<br />";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
}
}
mysql_close();
?>
</form>
you have add method in your form tag--
<form method="post" action="tester.php" >
and also replace if(isset($_GET['delete'])) to if(isset($_POST['delete']))
Replace your form tag by:
<form method="post" action="tester.php">
then these 3:
if(isset($_POST['delete']))
$id =(int)$_POST['delete_these'][$i];
$sql = "DELETE FROM booking WHERE id=$id";
at the place of these 3:
if(isset($_GET['delete']))
$id = $checkbox[$i];
$sql = "DELETE FROM booking WHERE id IN($ids)";
Your $id = $checkbox[$i]; works only if register_globals directive is on, which is now deprecated (and removed as of PHP 5.4) because of security issues it causes, and if it exists in the version of PHP you are using most hosting services turn it off.
You'll be sending the data through post so you access them through $_POST. the (int) before the $_POST is to cast anything to integer so if the user try to send a string it will become 0, this will protect you from sql injection.
SIDE NOTE you should stop using mysql_* functions as they are deprecated. Check out PDO or mysqli

Show records from MySQL Database

I have this PHP page which is supposed to be a list of employees with their positions..
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>View Records</title>
</head>
<body>
<?php
// connect to the database
include('connection.php');
?>
<?php
// get results from database
$result = "SELECT employees.id, CONCAT( fname, lname ) AS FullName, employees.hphone, employees.cphone, employees.email, position.pos\n"
. "FROM employees\n"
. "INNER JOIN position ON employees.posid = position.id\n"
. "ORDER by employees.id ASC LIMIT 0, 30 ";
or die(mysql_error());
// display data in table
echo "<p><b>View All</b> | <a href='view-paginated.php?page=1'>View Paginated</a></p>";
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>Employee Name/th> <th>Home Phone</th> <th>Cell Phone</th> <th>Email</th> <th>Position</th> </tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['employees.id'] . '</td>'
echo '<td>' . $row['FullName'] . '</td>';
echo '<td>' . $row['employees.hphone'] . '</td>';
echo '<td>' . $row['employees.cphone'] . '</td>';
echo '<td>' . $row['employees.email'] . '</td>';
echo '<td>' . $row['position.pos'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete </td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
<p>Add a new record</p>
</body>
</html>
But I can only get a white page..no errors no nothing....can someone help me please..i am using linux mysql and PHP....i know the sql works because i can get the records from it via MyPHPAdmin..
Please help.
Try this:
$con=mysqli_connect("example.com","peter","abc123","my_db");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$sql = "SELECT employees.id, CONCAT( fname, lname ) AS FullName, employees.hphone, employees.cphone, employees.email, position.pos\n"
. "FROM employees\n"
. "INNER JOIN position ON employees.posid = position.id\n"
. "ORDER by employees.id ASC LIMIT 0, 30 ";
$result = mysqli_query($con,$sql);
// display data in table
echo "<p><b>View All</b> | <a href='view-paginated.php?page=1'>View Paginated</a></p>";
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>Employee Name/th> <th>Home Phone</th> <th>Cell Phone</th> <th>Email</th> <th>Position</th> </tr>";
// loop through results of database query, displaying them in the table
while($row = mysqli_fetch_array($result)) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['employees.id'] . '</td>';
echo '<td>' . $row['FullName'] . '</td>';
echo '<td>' . $row['employees.hphone'] . '</td>';
echo '<td>' . $row['employees.cphone'] . '</td>';
echo '<td>' . $row['employees.email'] . '</td>';
echo '<td>' . $row['position.pos'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete </td>';
echo "</tr>";
}
// close table>
echo "</table>";
At first glance, you did not run $result = mysqli_query($conn, $some_query_string_here) and are calling or die() on a string declaration. Since we cannot see what is in your connection.php file we don't see what you do there, but your issue is you don't execute a query on the SQL string and assign that to result, you just assign the string to result.
To troubleshoot in future, you should enable errors on your server:
Find the php.ini file on your computer/server and set these as shown:
error_reporting = E_ALL & ~E_NOTICE | E_STRICT
display_errors = On
Restart your web server ( Apache or Nginx or PHP-FPM depending on how you set it up )
View errors on the screen and fix them one at a time until it works.
Alternate way to enable error reporting is .htaccess file in your script directory or a parent dir with the following php_value error_reporting 0
If you cannot view errors then comment out the php lines in your code, save, then refresh your browser until page renders. The last thing you commented out is typically the culprit and then debug.

Categories