Using Checkboxes with HTML and PHP (SQLite Database) - php

I am trying to set the value of my checkbox. I do have a SQLite database with a table with some rows. Some rows contain STATUS = 1, some contain STATUS = 0.
... "STATUS" BOOL NOT NULL DEFAULT 1, ...
So I check the current row with the fetchArray function and everything else works fine, but in my HTML site, all checkboxes are checked. Is there a logical mistake?
while ($row = $table->fetchArray(SQLITE3_ASSOC))
{
$output .= '<tr>';
if ($row['STATUS'] == 1) {
$output .= '<td class="STATUS" data-id1="' . $row['ID'] . '" bgcolor="#cecece"><input type="checkbox" name="STATUS" checked="true"></td>';
}
else {
$output .= '<td class="STATUS" data-id1="' . $row['ID'] . '" bgcolor="#cecece"><input type="checkbox" name="STATUS" checked="false"></td>';
}
$output .= '<td class="USER" data-id2="' . $row['ID'] . '" contenteditable="true">' . $row['USER'] . '</td>
...
<td class="DESCRIPTION" data-id11="' . $row['ID'] . '" contenteditable="true">' . $row['DESCRIPTION'] . '</td>
<td><button type="button" name="btn_edit" data-id12="' . $row['ID'] . '" class="btn btn-xs btn-warning btn-block btn_edit">Edit</button></td>
<td><button type="button" name="btn_delete" data-id13="' . $row['ID'] . '" class="btn btn-xs btn-danger btn-block btn_delete">Delete</button></td>
</tr>';
}

This is the solution:
if ($row['STATUS'] == 1) {
$output .= '<td class="STATUS" data-id1="' . $row['ID'] . '" bgcolor="#cecece"><input type="checkbox" name="STATUS" checked>' . $row['STATUS'] . '</td>';
}
else {
$output .= '<td class="STATUS" data-id1="' . $row['ID'] . '" bgcolor="#cecece"><input type="checkbox" name="STATUS">' . $row['STATUS'] . '</td>';
}
As you can see, I just changed the syntax of the HTML code. I don't know why the checked property has to be used like this, but to be honest I don't really care. ;-)

Related

Data not showing when changing Input to textarea

This is my code to display code out of my database:
<?php
while($row = mysqli_fetch_array($myData)){
echo '<form action="details.php?ID='.$row['ID'].'" method="post">';
echo '<tr>';
echo '<input type="hidden" name="ID" value="'.$row['ID'].'">';
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . '<input name="title" value="' . $row['Title'] . '">' .'</td>' ;
echo '<td>' . '<input size=85 name="detail" value="' . $row['Detail'] . '" cols="85" rows="2">'.'</td>';
echo '<td>' . $row['eventDate'] . '</td>';
echo '<td>' . $row['dateAdded'] . '</td>';
echo '<td>' . '<input type="submit" name="update" value="update" class="btn btn-default"> ' . '</td>';
echo '<td>' . '<input type="submit" name="delete" value="delete" class="btn btn-default"> ' . '</td>';
echo '</tr>';
echo '</form>';
}
?>
When I try to change the Inputs to <textarea>'s it stops showing the data from the database but only the text field. When I check Page Source it shows the data. How to format the fields like this:
echo '<td>' . '<textarea name="title" value="' . $row['Title'] . '">'. '</textarea>' .'</td>' ;
echo '<td>' . '<textarea size=85 name="detail" value="' . $row['Detail'] . '" cols="85" rows="2">'. '</textarea>' .'</td>';
textareas don't have a value attribute. Place the value between the opening and closing tags instead:
echo '<td><textarea name="title">'. $row['Title'] . '</textarea></td>' ;
echo '<td><textarea size=85 name="detail" cols="85" rows="2">'. $row['Detail'] .'</textarea></td>';
Try to insert the text between textarea tags instead of adding value attribute
echo '<td>' . '<textarea name="title">'. $row['Title'] .'</textarea>' .'</td>' ;
The general format for a text are field is <textarea name="name" cols="width" rows="height" wrap="type"> </textarea>
So there shouldn't be a value attribute. Something like this:
echo '<td>' . '<textarea name="title">'. '</textarea>' .'</td>' ;

Why is this variable not being posted and giving an error?

Why is bakeryid variable not being posted in my form? The error I am receiving is
"Notice:Undefined variable bakeryid"
I have two pages, one displays the form and the second is the action for the form. The second form kept saying that it was undefined also. The bakeryid is the ID for each cakes order.
$sql = mysqli_query($con,"SELECT `firstname`, `bakeryid`, `order` FROM cakes");
$bakeryid = $_POST['bakeryid'];
?>
<table border='2'>
<th>First Name</th>
<th>Order</th>
<?php
echo '<form name="display" method="POST" action="cakephp.php">';
while($row = mysqli_fetch_array($sql))
{
echo "<tr>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['order'] . "</td>";
echo '<td><input type="hidden" name="bakeryid" value="' . $bakeryid . '"/></td>';
echo '<td><input type="hidden" name="memid" value="' . $memid . '"/><input type="submit" name="takeorder" value="Take Order" ></td>';
echo "</tr>";
}
echo "</form>";
echo "</table>";
Answer:
echo '<td><input type="hidden" name="bakeryid" value="' . $row['bakeryid'] . '"/></td>';
You set $bakeryid with $_POST['bakeryid'], but then define bakeryid with $bakeryid.
Please try:
$sql = mysqli_query($con,"SELECT `firstname`, `bakeryid`, `order` FROM cakes c INNER JOIN members m ON c.memid = m.memid");
$bakeryid = $_POST['bakeryid']; // this line is unnecessary
?> <table border='2'>
<th>First Name</th>
<th>Order</th>
<?php
echo '<form name="display" method="POST" action="cakephp.php">';
while($row = mysqli_fetch_array($sql))
{
echo "<tr>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['order'] . "</td>";
echo '<td><input type="hidden" name="bakeryid" value="' . $row['bakeryid'] . '"/></td>'; // this line changed
echo '<td><input type="hidden" name="memid" value="' . $memid . '"/><input type="submit" name="takeorder" value="Take Order" ></td>';
echo "</tr>";
}
echo "</form>";
$sql = mysqli_query($con,"SELECT `firstname`, `bakeryid`, `order` FROM cakes c INNER JOIN members m ON c.memid = m.memid");
$bakeryid = $_POST['bakeryid']; ?>
<table border='2'>
<th>First Name</th>
<th>Order</th>
<?php
echo '<form name="display" method="POST" action="cakephp.php">';
while($row = mysqli_fetch_array($sql))
{
echo '<tr>
<td>' . $row['firstname'] . '</td>
<td>' . $row['order'] . '</td>
<td><input type="hidden" name="bakeryid" value="' . $row['bakeryid'] . '"/></td>
<td><input type="hidden" name="memid" value="' . $memid . '"/>
<input type="submit" name="takeorder" value="Take Order" >
</td>
</tr>';
}
echo '</form></table>';
The reason bakeryid isn't appearing in the form, is when setting the fields value you are using $bakeryid which is set to a post that hasn't happened yet. You want to set the value to $row['bakeryid'] as above.

Loop table to INSERT back to MySQL

I have a PHP page that queries a database, and populates multiple tables with values. Those values need to be modified and placed in a separate table.
I know how to look through the database to show all the tables, but I can't find anything about how to loop through the html tables to reinsert the new values. I can't simply use an id and a ton of functions because there is an indeterminate number of rows.
Here is a list of the columns in the table I am accessing:
Here is a snippet of my code that I am using to access the database and populate the tables:
function tableAddData( $tableNum )
{
// Access Global Variables
global $noteId, $itemId, $quantityId, $costId, $laborHoursId, $totalHoursId, $materialCostId, $laborCostId, $totalCostId, $itemNumberId, $servername, $username, $password, $dbname, $quantityChangeId, $qtyRow;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "select
`note` as `Note`,
`item` as `Item`,
`qty` as `Quantity`,
`cost` as `Cost`,
`hrsLabor` as `Labor Hours`,
`rate` as `Labor Rate`,
`qty` * `hrsLabor` as `Total Hours`,
FORMAT(`qty` * `cost`, 2) as `Material Cost`,
FORMAT(`qty` * `rate` * `hrsLabor`, 2) as `Labor Cost`,
FORMAT(( `qty` * `cost` ) + ( `qty` * `hrsLabor` * `rate` ), 2) as `Total Cost`,
`itemNo` as `Item Number`
from
`test_newtable`,
labor_rates
where
test_newtable.laborRate = labor_rates.id AND test_newtable.tableNo = " . $tableNum . ";";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if ($row["Note"] != "" ) {
$noteHtml = '<td id="Note" class="text-center">
<span data-toggle="tooltip" data-placement="top" title="' . $row["Note"] . '">
<span class="glyphicon glyphicon-comment text-danger"></span>
</span>
</td>';
}
else {
$noteHtml = '<td></td>';
};
echo '
<tr>'
. $noteHtml . '
<td id="Item" >' . '<input class="text-left" value="' . $row["Item"] . '" disabled/></td>
<td id="Quantity">' . '<input class="text-center qty" id="qty-' . $qtyRow . '" value="' . $row["Quantity"] . '" /></td>
<td class="dollarRight" style="padding-left:0;padding-right:0; width:20px;">' . '<input class="text-center text-muted" value="$" disabled/></td>
<td id="Cost" class="dollarLeft">' . '<input class="text-right" id="cost-' . $qtyRow . '" value="' . $row["Cost"] . '" /></td>
<td id="Labor Hours">' . '<input class="text-right" id="laborHours-' . $qtyRow . '" value="' . $row["Labor Hours"] . '" /></td>
<td id="Total Hours">' . '<input class="text-right totalHours-' . $tableNum . '" id="totalHours-' . $qtyRow . '" value="' . $row["Total Hours"] . '" disabled/></td>
<td class="dollarRight" style="padding-left:0;padding-right:0; width:20px;">' . '<input class="text-center text-muted" value="$" disabled/></td>
<td id="Material Cost" class="dollarLeft">' . '<input class="text-right" id="materialCost-' . $qtyRow . '" value="' . $row["Material Cost"] . '" disabled/></td>
<td class="dollarRight" style="padding-left:0;padding-right:0; width:20px;">' . '<input class="text-center text-muted" value="$" disabled/></td>
<td id="Labor Cost" class="dollarLeft">' . '<input class="text-right" id="laborCost-' . $qtyRow . '" value="' . $row["Labor Cost"] . '" disabled/></td>
<td class="success dollarRight" width="1%" style="padding-left:0">' . '<input class="text-muted" value="$" disabled/></td>
<td id="Total Cost" class="success dollarLeft">' . '<input class="text-right totalCost-' . $tableNum . '" id="totalCost-' . $qtyRow . '" value="' . $row["Total Cost"] . '" disabled/></td>
<td id="Item Number">' . '<input class="text-left" value="' . $row["Item Number"] . '" disabled /></td>
</tr>
';
update($tableNum);
$qtyRow = $qtyRow + 1;
}
}
}
Here is an example of what the page looks like now:
You need to update the database with news values from a form in html?
you could use the name field in the input like name="tablename-columnname-rownumber", then in post php
foreach($_POST as $key=> $value){
$array = preg_split("/-/",$key);
if(count($array)==3) // if had 3 values
{
// update table $array[0] set $array[1] = $value where id = $array[2]
// update table (table name) set (column name) = (value from post) where id = (row number)
}
}
but is better if you put all in an array and just do one update by row
$arraytemp = array();
foreach($_POST as $key=> $value){
$array = preg_split("/-/",$key);
if(count($array)==3) // if had 3 values
{
$arraytemp[$array[0]][$array[2]][$array[1]]=$value;
}
}
foreach($arraytemp as $tablename => $tabledata){
$query = "update ".$tablename;
foreach($tabledata as $id => $columndata){
foreach($columndata as $columnname => $value){
$query .=' set '.$columnname'='.$value.';
}
$query.=' where id='.$id;
//exceute query
}
}
(i don't try it, just is what came to my mind, hope it works)

PHP displays tags in different order

I have following problem, I have list of products in a database and want to display them in table unfortunately it plays some tricks for me because it displays one td before table even begins.
Here are the PHP functions:
<?php
function displayProduct($code,$url)
{
echo '<form method="post" action="cart_update.php"><input type="hidden" name="code" value="' . $code . '"/>';
echo '<input type="hidden" name="return_url" value="' . $url . '" />';
echo '<input type="hidden" name="type" value="add" /><input type="submit" value="Add" /></form>';
}
function displayItem($obj,$url)
{
echo '<tr>';
echo '<td>' . $obj->menuposition . '</td><td>' . $obj->name . '</td><td>' . '£'.$obj->price . '</td><td>' . displayProduct($obj->code,$url) .'</td>';
echo '</tr>';
if(strlen($obj->description) > 2)
{
echo '<tr><td colspan="4" style="font-size: 10px;">' . $obj->description . '</td></tr>';
}
}
?>
And here is the HTML output that I get:
Could someone help me ?
The echo call from displayProduct happens before the echo call of displayItem occurs.
I can see two solutions.
1: displayProduct should return the things to write and not echo them.
2:
echo '<td>' . $obj->menuposition . '</td><td>' . $obj->name . '</td><td>' . '£'.$obj->price . '</td><td>';
displayProduct($obj->code,$url);
echo '</td>';
displayProduct($code,$url) should return the string instead of printing it out:
function displayProduct($code,$url)
{
$result = '<form method="post" action="cart_update.php"><input type="hidden" name="code" value="' . $code . '"/>';
$result .='<input type="hidden" name="return_url" value="' . $url . '" />';
$result .='<input type="hidden" name="type" value="add" /><input type="submit" value="Add" /></form>';
return $result
}
[Edit] I should read better the questions...
But this still applies:
Also as Adrian stated, you should not echo the lines in "displayProducts", but return a string.

How to echo multiple rows with data based on checkbox input?

Got stuck trying to echo out multiple rows with data based on checkbox input. As of current code it processes data from only one checkbox, no matter how many checkboxes are ticked. Please help!
while ($row = mysql_fetch_assoc($r)) {
$pals .= '<input type="checkbox" name="pal_num[]" value="'
. $row['pal_num'] . '">' . $row['pal_num'] . '<br>';
}
if ($pal == '') {
echo '';
} else {
echo '<form name="get_pal" action="post2.php" method="POST">';
echo $pals;
echo '<input type="submit" name="post" value="Go!">';
echo '</form>';
}
post2.php:
$w = $_POST['pal_num'];
$rrr = mysql_query("SELECT * FROM pl_tab WHERE pal_num" . $w[0]);
while ($row = mysql_fetch_array($rrr)) {
echo '<tr><td>' . '&nbsp' . '</td>';
echo '<td rowspan="5">' . $row['descr'] . '</td>';
echo '<td><b>' . 'Total weight' . '<b></td>';
echo '<td>' . '&nbsp' . '</td><td>' . '&nbsp' . '</td></tr>';
echo '<td>' . '&nbsp' . '</td>';
echo '<td colspan="3">' . '&nbsp' . '</td>';
//this part should multiple based on how many checkboxes are ticked.
echo '<tr><td>' . $row['l_num'] . '</td>';
echo '<td>' . $row['pal_num'] . '</td>';
echo '<td>' . $row['weight 1'] . '</td><td>' . $row['weight 2'] . '</td></tr>';
}
echo "</table>";
}
May be this will work :
$w = "'".implode("','",$_POST['pal_num'])."'";
$rrr = mysql_query("SELECT * FROM pl_tab WHERE pal_num in (".$w.");");
...and may be you forgot a echo "<table>"; before the while :)

Categories