How to work around mysql group_concat limit? - php

I have a table that displays a list of tasks for a client. Each task has the ability to have multiple notes. Tasks and notes are kept in separate tables, but brought together in this table by a sub-query that does a group_concat to be able to show the many-to-one relationship between a task and the notes for that task. The problem is that lengthy notes do not show the full text. The field in the notes table contains the full text that is needed. It is a blob data type and the data length is 1191 bytes. My research tells me that doing this select with the group_concat makes the returning Note field over 1024 bytes, which is the limit. And indeed the 'group_concat_max_len' is set to 1024. I have been instructed by my boss that rather than increase the data limit, I should figure out how to work within the limit. Can anyone give me some suggestions on how to modify this select statement to allow for the one-to-many statement but not limit the data length?
<div class="portlet light bordered">
<div class="portlet-title">
<div class="col-md-8">
<div class="caption font-dark">
<i class="fa fa-tasks font-dark"></i>
<span class="caption-subject bold uppercase">Task List</span>
</div>
</div>
</div>
<div class="portlet-body">
<table class="table table-striped table-bordered table-hover dt-responsive"
width="100%" id="sample_3" cellspacing="0" width="100%">
<thead>
<tr>
<th class="all">Task Step</th>
<th class="all">Envr</th>
<th class="all">Task Name</th>
<th class="all">Task Description</th>
<th class="none">Notes:</th>
<th class="all">Due Date</th>
<th class="all">Current Status</th>
</tr>
</thead>
<tbody>
<?php
include "../includes/DBConn.php";
$sql = "SELECT ID, FID , TaskCategory, Environment, TaskName, TaskDescription,
CreateDate, Cast(date_format(DueDate, \"%m-%d-%Y\") as char) as DueDate,
CompletedDate, CompletedBy, CurrStatus,
(SELECT GROUP_CONCAT(Cast(date_format(DATE(CreateDate),\"%m-%d-%Y\") as
char), \" <strong>\" , Cast(TIME(CreateDate) as char) , \"</strong> : \" ,
Notes ORDER BY CreateDate ASC SEPARATOR '<br>')
FROM arc.notes B where A.id = B.taskid
) as Notes
FROM arc.task A
WHERE FID = '$FID'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$TaskID = $row['ID'];
$TaskStep = $row['TaskCategory'] . $row['ID'];
$Environment = $row['Environment'];
$TaskName = $row['TaskName'];
$TaskDescription = $row['TaskDescription'];
$Notes = $row['Notes'];
$DueDate = $row['DueDate'];
$CompleteDate = $row['CompletedDate'];
$CurrStatus = $row['CurrStatus'];
?>
<tr>
<td><?php echo "$TaskStep "; if(!empty($Notes)) { echo " <i class=\"fa fa-
sticky-note\"></i>";} ?></td>
<td><?php echo "$Environment"; ?></td>
<td><?php echo "$TaskName"; ?></td>
<td><?php echo "$TaskDescription"; ?></td>
<td><a class="btn btn-circle btn-xs red-mint" data-toggle="modal"
href="#newNote<?php echo$TaskID;?>"><i class="fa fa-plus"></i>Add Note
</a>
<br>
<?php echo $Notes; ?>
</td>
<td><?php echo "$DueDate"; ?></td>
<td><?php switch ($CurrStatus) {
case (0): echo "<strong><a class=\"btn btn-xs btn-primary btn-
block\" data-toggle=\"modal\" href=\"#StatusChange" .
$TaskStep . "\">AWAITING ACTION</strong>";
break;
case (1): echo "<strong><a class=\"btn btn-xs btn-success btn-
block\" data-toggle=\"modal\" href=\"#StatusChange" .
$TaskStep . "\">IN PROGRESS</strong>";
break;
case (2): echo "<strong><a class=\"btn btn-xs grey-gallery btn-
block\" data-toggle=\"modal\" href=\"#StatusChange" .
$TaskStep . "\">COMPLETED</strong>";
break;
default: echo "No Status";
}
?>
</td>
</tbody>
</div>
</div>
</div>

Related

how to show two mysql tables values at one while loop using php

I have created two tables, table1 and table2
table1 includes (id, codes and titles).
table2 includes id, table1_id, column1, column2, column3
I want Insert value on table1(codes , titles)
and show in table using PHP
and insert table2(column1, column2, column3) and show it
under table1 values.
This my codes
<?php
include('config.php');
$ret=" SELECT * from table1
";
$stmt= $mysqli->prepare($ret);
//$stmt->bind_param('i',$aid);
$stmt->execute() ;//ok
$res=$stmt->get_result();
$cnt=1;
while($row=$res->fetch_object())
{
?>
<div class="card shadow">
<div class="card-header py-0">
<table class="table">
<tr >
<td style="border: none"><?php echo $row->codes; ?></td>
<td style="border: none"><?php echo $row->titles; ?></td>
<td style="border: none">edit</td>
</tr>
</table>
</div>
<div class="card-body">
<div class="table-responsive table mt-2" id="dataTable" role="grid" aria-
describedby="dataTable_info">
<table class="table my-0" id="dataTable">
<thead>
<tr>
<th>column 1</th>
<th>column 2</th>
<th> column 3 </th>
</tr>
</thead>
<tbody>
<?php
$ret=" SELECT * from table2";
$stmt= $mysqli->prepare($ret);
$stmt->execute();
$res=$stmt->get_result();
while($row=$res->fetch_object() )
{
?>
<tr>
<td><?php echo $row->column1 ?></td>
<td> <?php echo $row->column3 ?></td>
<td> <?php echo $row->column3 ?></td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr></tr>
</tfoot>
</table>
</div>
<h3 class="text-dark mb-4"> <button class="btn btn-info " data-toggle="modal" data-
target="#login_itech3">adding column</button></h3>
</div>
</div>
<br>
<?php } ?>
I expected to shows me like this
but when I run that code it shows me , one value
You made two mistakes:
Used same variable name $row for iteration in outer and inner loop
Didn't specified table1_id in where condition in second query
Try below code, I am, sure It will work
<?php
include('config.php');
$ret = " SELECT * from table1
";
$stmt = $mysqli->prepare($ret);
//$stmt->bind_param('i',$aid);
$stmt->execute(); //ok
$res = $stmt->get_result();
$cnt = 1;
while ($row1 = $res->fetch_object()) {
?>
<div class="card shadow">
<div class="card-header py-0">
<table class="table">
<tr>
<td style="border: none"><?php echo $row1->codes; ?></td>
<td style="border: none"><?php echo $row1->titles; ?></td>
<td style="border: none">edit</td>
</tr>
</table>
</div>
<div class="card-body">
<div class="table-responsive table mt-2" id="dataTable" role="grid" aria- describedby="dataTable_info">
<table class="table my-0" id="dataTable">
<thead>
<tr>
<th>column 1</th>
<th>column 2</th>
<th> column 3 </th>
</tr>
</thead>
<tbody>
<?php
$ret = " SELECT * from table2 where table1_id = ".$row1->id;
$stmt = $mysqli->prepare($ret);
$stmt->execute();
$res = $stmt->get_result();
while ($row2 = $res->fetch_object()) {
?>
<tr>
<td><?php echo $row2->column1 ?></td>
<td> <?php echo $row2->column3 ?></td>
<td> <?php echo $row2->column3 ?></td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr></tr>
</tfoot>
</table>
</div>
<h3 class="text-dark mb-4"> <button class="btn btn-info " data-toggle="modal" data- target="#login_itech3">adding column</button></h3>
</div>
</div>
<br>
<?php } ?>

Button Does Not Update PHP SQL Server Table

I have this code and I want to make the BIT column Color 1 by pressing the check button and I really can't handle the php. I need help, please!
There is the table and how I access each data from the SQL server, it shows fine but the button is not doing anything when I'm trying to _POST in php. I have been looking for an answer but I couldn't find anything.
<table class="table table-hover">
<thead class="thead-light">
<tr>
<th scope="col">Nr Expediente</th>
<th scope="col">Fecha</th>
<th scope="col">Tipo</th>
<th scope="col">Responsable</th>
<th scope="col">Proveedor</th>
<th scope="col">Observaciones</th>
<th scope="col">Hora</th>
<th scope="col">Check</th>
</tr>
</thead>
<tbody>
<?php
$query="SELECT dbo.Transportes.IdExpediente, dbo.Operaciones.IdTransporte, dbo.Operaciones.Id, dbo.Operaciones.Color, dbo.Operaciones.CreadoPor, dbo.Expedientes.Numero, dbo.Expedientes.ProveedorNombre, dbo.Operaciones.Fecha, dbo.Operaciones.Tipo, dbo.Operaciones.Hora, dbo.Operaciones.Instrucciones FROM dbo.Expedientes
INNER JOIN dbo.Transportes ON dbo.Expedientes.Id = dbo.Transportes.IdExpediente INNER JOIN dbo.Operaciones ON dbo.Operaciones.IdTransporte = dbo.Transportes.Id
WHERE convert(date, convert(varchar(30), dbo.Operaciones.Fecha), 101) = CONVERT (date, GETDATE()) AND dbo.Operaciones.Tipo = 0 ORDER BY dbo.Operaciones.Id DESC";
$res=sqlsrv_query($con,$query);
while ($row=sqlsrv_fetch_array($res)){
?>
<tr <?php if ($row['Color'] == 1) {
echo 'class="table-success"';
} else { echo 'class="table-warning"';} ?> ><td><?php echo $row['Numero']; ?></td>
<td><?php echo $row['Fecha']->format('d/m/Y'); ?></td>
<td><?php if ($row['Tipo'] == 1) {
echo "Descarga";
} else { echo "Carga";} ?></td>
<td><?php echo $row['CreadoPor']; ?></td>
<td><?php echo $row['ProveedorNombre']; ?></td>
<td><?php echo $row['Instrucciones']; ?></td>
<td><?php echo $row['Hora']; ?></td>
<td>
<form method="post">
<div class="btn-group">
<button type="button" name="Checking" class="btn btn-success">Check</button>
<input type="hidden" name="id" value="' .$row['Id']. '"/>
<input type="hidden" name="color" value="1"/>
<button type="button" class="btn btn-success dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="sr-only">Toggle Dropdown</span>
</button>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">In progress</a>
<a class="dropdown-item" href="#">Save for later</a>
<div class="dropdown-divider"></div>
<button type="butoon" name="Unchecking">Uncheck</a>
</div>
</div> </form></td></tr>
<?php
}
?>
</tr>
</tbody>
</table>
<?php if(isset($_POST['Checking'])){
$sql = "UPDATE dbo.Operaciones SET
Color = '1'
WHERE Id = {_POST['id']} ";
$result = sqlsrv_query($con,$sql) or die(sqlsrv_errors());
}
?>
change type="button" to type="submit"
and
change
$sql = "UPDATE dbo.Operaciones SET
Color = '1'
WHERE Id = ".$_POST['id'] ;

Book quantity under a Category in library management system

I am building a library management system using PHP, Mysql. There is a category for each book. Users will be able to see how many books there are in each category. But I can't figure out how many books there are in each category. I want to find out how many books there are in each category.
<div class="row">
<div class="col-md-12">
<!-- Advanced Tables -->
<div class="panel panel-default">
<div class="panel-heading">
Categories Listing
</div>
<div class="panel-body">
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>#</th>
<th>Category</th>
<th>Book Amount</th>
<th>Status</th>
<th>Creation Date</th>
<th>Updation Date</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT tblcategory.CategoryName, tblcategory.Status, tblcategory.CreationDate, tblcategory.UpdationDate from tblcategory";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{ ?>
<tr class="odd gradeX">
<td class="center"><?php echo htmlentities($cnt);?></td>
<td class="center"><?php echo htmlentities($result->CategoryName);?></td>
<td class="center">#</td>
<td class="center"><?php if($result->Status==1) {?>
Active
<?php } else {?>
Inactive
<?php } ?></td>
<td class="center"><?php echo htmlentities($result->CreationDate);?></td>
<td class="center"><?php echo htmlentities($result->UpdationDate);?></td>
<td class="center">
<a href="edit-category.php?catid=<?php echo htmlentities($result->id);?>"><button class="btn btn-primary"><i class="fa fa-edit "></i> Edit</button>
<a href="manage-categories.php?del=<?php echo htmlentities($result->id);?>" onclick="return confirm('Are you sure you want to delete?');"" > <button class="btn btn-danger"><i class="fa fa-pencil"></i> Delete</button>
</td>
</tr>
<?php $cnt=$cnt+1;}} ?>
</tbody>
</table>
</div>
</div>
</div>
<!--End Advanced Tables -->
</div>
</div>
My Database:
The Output Should be:
Calculate category wise book count by using subquery. Then use LEFT JOIN with main table. Use COALESCE() for replacing NULL value to 0.
SELECT c.CategoryName
, COALESCE(b.book_amount, 0) book_amount
, CASE WHEN c.Status = 1 THEN 'Active' ELSE 'Inactive' END Status
, c.CreationDate
, c.UpdationDate
FROM category c
LEFT JOIN (SELECT catid
, COUNT(id) book_amount
FROM book
GROUP BY catid) b
ON c.id = b.catid
ORDER BY b.book_amount DESC

Edit users in database using sql

I'm trying to generate a table of all user accounts and then have buttons next to each user to either delete or change the account level of the user (admin or not admin). What would be the best way to go about this?
Here's my code:
<?php
$query = mysql_query("SELECT user_name,user_id,user_email,user_level
FROM users
ORDER BY user_name ASC");
echo '<table class="table table-hover">
<thead class="thead-default">
<tr>
<th>Username</th>
<th>User ID</th>
<th>Email</th>
<th>Level</th>
<th>Options</th>
</tr>
</thead>';
while($row = mysql_fetch_array($query)){
echo '<tbody>
<tr>
<td class="col-3">' .$row['user_name'].'</td>
<td class="col-3">' .$row['user_id'].'</td>
<td class="col-3">' .$row['user_email'].'</td>
<td class="col-3">' .$row['user_level'].'</td>
<td class="col-3"><a class="btn btn-success" href="#" data-toggle="tooltip" title="Edit"><span class="glyphicon glyphicon-pencil"></span></a>
<a class="btn btn-success" href="#" data-toggle="tooltip" title="Promote"><span class="glyphicon glyphicon-arrow-up"></span></a>
<a class="btn btn-danger" href="#"><span class="glyphicon glyphicon-arrow-down" data-toggle="tooltip" title="Demote"></span></a>
<a class="btn btn-danger" href="delete.php" data-toggle="tooltip" title="Delete"><span class="glyphicon glyphicon-trash"></span></a>
</td>
</tr>
</tbody>';
}
echo '</table>'; ?>
Any help would be appreciated :)
Edit: The admin/standard user is set via user_level with 0 being standard user and 1 being admin
edit 2: Added code
<?php
include 'connect.php';
include 'header.php';
mysql_query("UPDATE users SET user_level='1' WHERE user_id='".$_GET['user_id']."'");
die("User promoted to admin.");
include 'footer.php';
?>
Getting no luck with it, will try to add if statements for feedback on if database row changes
This here is example of code, because your questions lack some details, so i am sharing my code.
<table>
<tr>
<th>User Email </th>
<th>Date & Time </th>
<th>Complain Number</th>
<th>Complain Type</th>
<th>Description</th>
<th>Status</th>
</tr>
<?php
$ccount =1;
$email= $_SESSION["email"];
$query = mysqli_query($con,"Select * from new_complain where new_email =
'$email'");
while($rows = $query->fetch_assoc())
{
?>
<tr>
<input type="hidden" name="<?php echo 'sstd' . $ccount ; ?>" value="<?php
echo $rows['complain_type']; ?>" placeholder="Student Name" />
<td><?php echo $_SESSION["email"]; ?></td>
<td><?php echo $rows['complain_date']; ?></td>
<td><?php echo $rows['new_id']; ?></td>
<td><?php echo $rows['complain_type']; ?></td>
<td><?php echo $rows['new_complain']; ?></td>
<td><?php echo $rows['comlain_status']; ?></td>
</tr>
<?php
$ccount++;
} ?>
</table>
Without full details, it would be something like this:
<a class="btn btn-success" href="promote.php?id='.$row['user_id'].'" data-toggle="tooltip" title="Promote"><span class="glyphicon glyphicon-arrow-up"></span></a>
<a class="btn btn-danger" href="demote.php?id='.$row['user_id'].'"><span class="glyphicon glyphicon-arrow-down" data-toggle="tooltip" title="Demote"></span></a>
Then you need a promote.php in the same directory as this file, which would look like this:
<?php
mysql_query("UPDATE users SET user_level='1' WHERE user_id='".$_GET['user_id']."'");
die("User promoted to admin user.");
And a demote.php like so:
<?php
mysql_query("UPDATE users SET user_level='0' WHERE user_id='".$_GET['user_id']."'");
die("User demoted to standard user.");

PHP - while inside while in table

I have a table that I want to assign a staff member to for each row. I want a dropdown on the last column with all the staff members so I can assign a staff member by clicking their username in the dropdown.
I'm trying to do a while loop inside a while loop but am getting an error.
This is my current code:
<table class="table table-bordered table-striped js-dataTable-full table-header-bg">
<thead>
<tr>
<th>Actions</th>
<th>Username</th>
<th>Service</th>
<th>Price</th>
<th>Date Ordered</th>
<th>Account Email</th>
<th>Account Password</th>
<th>Status</th>
<th>Assign to staff</th>
</tr>
</thead>
<tbody>
<?php
$clients_result = mysqli_query($con, "SELECT * FROM boosting_orders ORDER BY id ASC");
$query = mysqli_query( $con, "SELECT * FROM users" );
if(mysqli_num_rows($clients_result) > 0) {
while($payment_row = mysqli_fetch_array($clients_result)) {
echo '
<tr id="no_enter">
<td style="text-align: center;">
<div class="btn-group">
<a class="btn btn-xs btn-default" type="button" data-toggle="tooltip" title="" data-original-title="Edit Order" href="orders_admin?action=edit&identification='.$payment_row['id'].'"><i class="fa fa-pencil"></i></a>
<a class="btn btn-xs btn-default" type="button" data-toggle="tooltip" title="" data-original-title="Remove Order" href="orders_admin?action=delete&identification='.$payment_row['id'].'"><i class="fa fa-times"></i></a>
</div>
</td>
<td>
'.$payment_row['users_name'].'
</td>
<td>
'.$payment_row['service'].'
</td>
<td>
$'.$payment_row['price'].'
</td>
<td>
'.$payment_row['date_ordered'].'
</td>
<td>
'.$payment_row['email'].'
</td>
<td>
'.$payment_row['password'].'
</td>
<td>
'.($payment_row['status'] == 'Completed' ? '<span class="label label-success" data-toggle="tooltip" title="" data-original-title="Your account has successfully been boosted">Completed</span>' : '<span class="label label-info" data-toggle="tooltip" title="" data-original-title="This order is either still pending or corrupt">Pending</span>').'
</td>
<td>
<select name="assign_staff" class="form-control">
'. while($row = mysqli_fetch_array($query)) { .'
<option value="'.$row['username'].'">'.$row['username'].'</option>
'. } .'
</select>
</td>
</tr>
';
} } else { } ?>
</tbody>
you can't concatenate string with while syntax,
the code in the last <td> should be:
echo '<select name="assign_staff" class="form-control">';
while ($row = mysqli_fetch_array($query)) {
echo '<option value="' . $row['username'] . '">' . $row['username'] . '</option>';
}
echo '</select>

Categories