What am i doing wrong here? - php

I've been staring at this page for over an hour. My update function just doesnt not seem to update. When i tried it through sql it seems ok. I have a form at the bottom of this page which updates a field in a table. Can anyone spot the mistakes?
<?php
// First of all initialise the user and check for permissions
require_once "/var/www/users/user.php";
$user = new CHUser(2);
// Initialise the template
require_once "/var/www/template/template.php";
$template = new CHTemplate();
// And create a cid object
require_once "/var/www/Testing/DisplayWIPOnLocation.php";
$BundleProgress= new CHWIPProgress();
if(isset($_GET['Reference'])){
$todays_date = date("Y-m-d H:i:s");
$content .= " <h3> Details for Bundle : $reference </h3> ";
$bundle = $BundleProgress->GetBundle($_GET['Reference']);
$reference = $_GET['Reference'];
// Now show the details
foreach($bundle as $x){
$content .= "
<table>
<tr>
<th> Location </th>
<td>" . $x['Description'] . "</td>
</tr>
<tr>
<th> Works Order Number </th>
<td>" . $x['WorksOrder'] . "</td>
</tr>
<tr>
<th> Bundle Number </th>
<td>" . $x['Number'] . "</td>
</tr>
<tr>
<th>Qty Issued</th>
<td>" . $x['Qty'] . "</td>
</tr>
<tr>
<th>Bundle Reference </th>
<td>" . $x['Reference'] . "</td>
</tr>
<tr>
<th>Style description</th>
<td>" . $x['Stock'] . "</td>
</tr>
<tr>
<th>Due Date</th>
<td>" . $x['DueDate'] . "</td>
</tr>
<tr>
<th>Date In </th>
<td>" . $x['DateIN'] . "</td>
</tr>
<tr>
<th>Date Out</th>
<td>" . $x['DateOUT'] . "</td>
</tr>
<tr>
<th>Last Code</th>
<td>" . $x['Last'] . "</td>
</tr>
</table>
<br> ";
}
$content .= " </table>
<form action='viewBundle.php?step=2' method='post'>
<p>Reason: <input type='text' name='reason' /><br
/><p>
<p><input type='hidden' name='bundlereference'
id='Username' value='" . $x['Reference'] . "' />
<input type='submit' name ='add'/></form>
</table> ";
if($_GET['step'] == 2) {
$BundleProgress->UpdateReason($_POST['reason'],$_POST['bundlereference']);
$content .= " <a href='index.php?location=" .
$x['Description'] . "'> updated</a> ";
}
}
else {
$content .= "<h3>Something has gone wrong</h3>
<br>
<a href='index.php?location=" . $x['Description'] . "'> Return to Previous
Page </a>
";
}
$template->SetTag("content", $content);
echo $template->Display();
?>
Function
public function UpdateReason($reason, $bundlereference) {
$sql = "UPDATE `ArchiveBundle`
SET `Issue` = " . $reason . "
WHERE `BundleReference` = " . $bundlereference .
";";
mysql_select_db(DB_DATABASE_NAME, $this->conn);
return mysql_query($sql, $this->conn);
}

change:
if($_GET['step'] == 2)
to:
if((int)$_GET['step'] === 2)
and:
public function UpdateReason($reason, $bundlereference) {
$sql = "UPDATE `ArchiveBundle`
SET `Issue` = " . $reason . "
WHERE `BundleReference` = " . $bundlereference .
";";
mysql_select_db(DB_DATABASE_NAME, $this->conn);
return mysql_query($sql, $this->conn);
}
to:
public function UpdateReason($reason, $bundlereference) {
mysql_select_db(DB_DATABASE_NAME, $this->conn);
$_reason = mysql_real_escape_string($reason,$this->conn);
$_bundlereference = mysql_real_escape_string($bundlereference,$this->conn);
$sql = "UPDATE `ArchiveBundle`
SET `Issue` = '" . $_reason . "'
WHERE `BundleReference` = '" . $_bundlereference . "'";
return mysql_query($sql, $this->conn);
}
Try that. Code hasn't been tested but it's a good place to start.
To try and debug what's going on here do the following:
public function UpdateReason($reason, $bundlereference) {
error_reporting(E_ALL ^ E_NOTICE);
$db_selected = mysql_select_db(DB_DATABASE_NAME, $this->conn);
if (!$db_selected) {
die("Can't use db : " . mysql_error());
}
$_reason = mysql_real_escape_string($reason,$this->conn);
$_bundlereference = mysql_real_escape_string($bundlereference,$this->conn);
$sql = "UPDATE `ArchiveBundle`
SET `Issue` = '" . $_reason . "'
WHERE `BundleReference` = '" . $_bundlereference . "'";
mysql_query($sql, $this->conn);
die(mysql_error());
}
Also, it looks like on your form submission you're not passing in the Reference parameter so the if(isset($_GET['Reference'])) will fail when you post the form. I've change the table and form code below to make it more readable, pass in the Reference param on form submission, and also to update the db record BEFORE fetching the dataset so you'll see the updated records in the table returned.
// First of all initialise the user and check for permissions
require_once "/var/www/users/user.php";
$user = new CHUser(2);
// Initialise the template
require_once "/var/www/template/template.php";
$template = new CHTemplate();
// And create a cid object
require_once "/var/www/Testing/DisplayWIPOnLocation.php";
$BundleProgress= new CHWIPProgress();
if(isset($_GET['Reference'])){
if($_GET['step'] == 2) {
$BundleProgress->UpdateReason($_POST['reason'],$_POST['bundlereference']);
}
$todays_date = date("Y-m-d H:i:s");
$content .= " <h3> Details for Bundle : $reference </h3> ";
$bundle = $BundleProgress->GetBundle($_GET['Reference']);
$reference = $_GET['Reference'];
// Now show the details
foreach($bundle as $x){
$content .= "
<table>
<tr><th> Location </th><td>" . $x['Description'] . "</td></tr>
<tr><th> Works Order Number </th><td>" . $x['WorksOrder'] . "</td></tr>
<tr><th> Bundle Number </th><td>" . $x['Number'] . "</td></tr>
<tr><th>Qty Issued</th><td>" . $x['Qty'] . "</td></tr>
<tr><th>Bundle Reference </th><td>" . $x['Reference'] . "</td></tr>
<tr><th>Style description</th><td>" . $x['Stock'] . "</td></tr>
<tr><th>Due Date</th><td>" . $x['DueDate'] . "</td></tr>
<tr><th>Date In </th><td>" . $x['DateIN'] . "</td></tr>
<tr><th>Date Out</th><td>" . $x['DateOUT'] . "</td></tr>
<tr><th>Last Code</th><td>" . $x['Last'] . "</td></tr>
</table>
<br>";
}
$content .= "<table>
<form action='viewBundle.php?Reference=" . $_GET['Reference'] . "&step=2' method='post'>
<p>Reason: <input type='text' name='reason' /></p><br/>
<p><input type='hidden' name='bundlereference' id='Username' value='" . $x['Reference'] . "' /></p>
<input type='submit' name ='add'/>
</form>
</table>";
} else {
$content .= "<h3>Something has gone wrong</h3>
<br>
<a href='index.php?location=" . $x['Description'] . "'> Return to Previous Page </a>
";
}
$template->SetTag("content", $content);
echo $template->Display();

Can I advise you break this down by first checking what mysql_query returns. It maybe that this particular variable is defined incorrectly. Also remember add quotes to the values in your query.

Related

Phpmyadmin not opening html table (potential server issue)

I am having issues with linking mysql to html through a server, the ports are all in order and the I have had no issues in linking the server to phpmyadmin. I have no password on the server and the username is 'root'.
please help :)
<?php
$servername = "localhost";
$username = "root"
$password= "";
$database = "stock_order";
$connection = new mysqli($servername, $username, $password, $database)
I believe this might be an error because my server port is 3008 however, that shouldn't be an issue
// checking connection
if ($connection)->connect_error {
die("connection failed: " . $connection ->connect_error);
}
//read the data from the customer table
$sql = "SELECT * FROM customertable";
$result = $connection->query($sql);
if (!$result) {
die("Invalid query: " .$connection->error);
}
// while loop - reads data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>" . $row["ItemID"] . "</td>
<td>" . $row["ItemName"] . "</td>
<td>" . $row["Quantity"] . "</td>
<td>" . $row["Description"] . "</td>
<td>" . $row["DeliveryMerchant"] . "</td>
<td>" . $row["Cost_of_item"] . "</td>
<td>" . $row["VAT_Percentage"] . "</td>
<td>" . $row["VAT"] . "</td>
<td>" . $row["Total_cost_of_item"] . "</td>
<td>
<a class='btn btn-primary btn-sm' href='update'>Update</a>
<a class='btn btn-danger btn-sm' href='delete'>Delete</a>
</td>
</tr>";
}
?>
</thead>
<tbody>
<tr>
<td>78956</td>
<td>Merch 2008 Monitor 4K</td>
<td>1</td>
<td>Fragile</td>
<td>TPL Resources</td>
<td>£1080</td>
<td>20%</td>
<td>£216</td>
<td>£1296</td>
<td>
<a href='update'>Update</a>
<a href='delete'>Delete</a>
</td>
</tr>
</tbody>
</table>
<body>
</body>
<?php
$conn-mysqli_connect("localhost", "root", "", "customer_order_table");
$sql = "SELECT * FROM stock_order";
$result = $conn ->query($sql);
if ($result->num_rows > 0) {
while ($row = $result -> fetch_assoc()) {
echo "<tr><td>" . $row["itemID"] . "<tr><td>" . $row["ItemName "] . "<tr><td>" .
$row["Quantity"] .
"<tr><td>" . $row["Description"] . "<tr><td>" . $row["DeliveryMerchant"] . "<tr>
<td>" . $row["VAT_Percentage"]
. "<tr><td>" . $row["VAT"] . "<tr><td>" . $row["Total_cost_of_item"] . "<tr><td>"
}
}
else {
echo "No Results";
}
$conn ->close();
?>
</style>
</table>
</body>
</html>
when I type in 'localhost/customertable/' in the address bar the page cannot be found, displaying an error 404.
Thanks in advance :)

How can I check if all checkboxes have been checked in PHP?

I am implementing a list of items and each has a checkbox. I currently can see which checkboxes have been checked but what I want to do is check if all of them have been checked. How can I implement that?
Here is my code:
<form action="" method="post">
<?php
echo "<table>
<tr>
<th>Customer ID</th>
<th>Report ID</th>
<th>Report message</th>
<th>Device</th>
<th>Device no.</th>
<th>Barcode</th>
<th>IMEI</th>
<th>Sale-date</th>
</tr>";
while ($row2 = $clientUsername->fetch_assoc()) {
$_SESSION['cl_username'] = $row2["username"];
while ($row = $message->fetch_assoc()) {
$_SESSION['accept'] = $row["acceptance"];
$_SESSION['client_comment'] = $row["message"];
$_SESSION['name'] = $row["name"];
$_SESSION['sales_date'] = $row["sales_date"];
$_SESSION['date_sent'] = $row["date_sent"];
$_SESSION['countable_array'] = $row;
?>
<?php if ($row['acceptance'] == 3) {
echo "<tr> <td>
" . '<input type=checkbox name=devices[] value=' . $row['dev_id'] . '>' . "
</td> <td>" . $cus_id . " </td> <td>" . $rep_id . "</td> <td>" . $_SESSION['client_comment'] . "</td> <td>" . $_SESSION['name'] . "</td> <td>" . $row["device_no"] . "</td> <td>" . $row["barcode"] . "</td> <td>" . $row["serial_imei"] . "</td> <td>" . $row["serial_no"] . "</td> <td>" . $row["sales_date"] . "</td></tr>";
echo "</table>";
}
}
}
</form>
if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['rejected'])) {
if (count($count_devices) == 1) {
...
}
}
<?php
while ($row2 = $clientUsername->fetch_assoc()) {
$_SESSION['cl_username'] = $row2["username"];
$i = 0; // initiate the variable here
$total_number_of_rows = $message->num_rows(); // Get total number of rows from your object
while ($row = $message->fetch_assoc()) {
$_SESSION['accept'] = $row["acceptance"];
$_SESSION['client_comment'] = $row["message"];
$_SESSION['name'] = $row["name"];
$_SESSION['sales_date'] = $row["sales_date"];
$_SESSION['date_sent'] = $row["date_sent"];
$_SESSION['countable_array'] = $row;
if ($row['acceptance'] == 3) {
$i++; // When conditions trues, increment the variable.
echo "<tr> <td>
" . '<input type=checkbox name=devices[] value=' . $row['dev_id'] . '>' . "
</td> <td>" . $cus_id . " </td> <td>" . $rep_id . "</td> <td>" . $_SESSION['client_comment'] . "</td> <td>" . $_SESSION['name'] . "</td> <td>" . $row["device_no"] . "</td> <td>" . $row["barcode"] . "</td> <td>" . $row["serial_imei"] . "</td> <td>" . $row["serial_no"] . "</td> <td>" . $row["sales_date"] . "</td></tr>";
echo "</table>";
}
}
if($i == $total_number_of_rows){ // Here implement this condition, If both equal then all inputs have checked.
echo "Check box checked all inputs";
}
}
?>
I think you expecting the same as above. We need to check the Total number of rows with Incremental variable value.
Please review my comment inside the code part, so that you can understand terms.

Merge and display 2 database and 2 tables

Hey guys i have a problem, there is a page where i need to display data acording to date, but from 2 databases and from 2 tables from each databases,
1 Database name = highmob_comenzi Tables = players and vanzari
2 Database name = highmob_comenzi2 Tables = players and vanzari
this is the code what i got , i have tryed "select * from players, vanzari" but still no luck, even that wont extract data from both databases :(
<table class='table table-responsive-sm table-bordered table-striped table- sm' border="2px">
<thead>
<tr>
<th>Locatia Vanzari</th>
<th>Tip. Cert.</th>
<th>Nr.</th>
<th>Status Comanda Mobila</th>
<th>Status Comanda Tapiterii</th>
<th>Status Livrare</th>
<th>Ora Livrare</th>
<th>Detalii Comanda</th>
<th>Total</th>
<th>Avans</th>
<th>Rest</th>
<th></th>
</tr>
<?php
$servername = "localhost";
$username = "id";
$password = "pw";
$dbname = "highmob_comenzi2";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = ("select *
from highmob_comenzi.players
cross join highmob_comenzi.vanzari
union all
select *
from highmob_comenzi2.players
cross join highmob_comenzi2.vanzari
WHERE statuslivrare >= CURRENT_DATE()");
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "
</thead>
<tbody>
<tr>
<td>HERE DATABASE NAME</td>
<td><a href='vezibilettransportcomenzi.php?id=" . $row["id"] . "' target='_blank' class='btn btn-sm btn-warning'>Tip. Cert.</a></td>
<td>" . $row["id"] . "</td>
<td>
" . $row["statuscomanda"] . "
</td>
<td>" . $row["statuscomandatapiterii"] . "</td>
<td>" . $row["statuslivrare"] . "</td>
<td>" . $row["oralivrare"] . "</td>
<td>" . $row["detaliicomanda"] . "</td>
<td>
" . $row["totaldeplata"] . "</td>
<td>" . $row["avans"] . " <br><a style='color:red;'>" . $row["banipreluati"] . "</a></td>
<td>" . $row["restdeplata"] . "<br><a style='color:red;'>" . $row["banipreluatirest"] . "</a></td>
<td><a href='edit.php?id=" . $row["id"] . "' target='_blank' class='btn btn-sm btn-primary' >Vezi comanda</a></td>
</tr>
";
}
} else {
echo "Nu sunt transporturi!";
}
$conn->close();
?>
</tbody>
</table>
you can access to the table in the same query without problem eg (assuming the tables have the same struct in both the database):
select *
from highmob_comenzi.players
cross join highmob_comenzi.vanzari
union all
select *
from highmob_comenzi2.players
cross join highmob_comenzi2.vanzari
this is only a sample for accessing to the 2 database in a single query. you must rethink the query with you join and condition
but based on you comment seem you need just (assuming that statuslivrare is a colum of the vanzari table)
select *
from highmob_comenzi.players
cross join highmob_comenzi.vanzari
WHERE vanzari.statuslivrare >= CURDATE()");
<?php
$servername = "localhost";
$username = "ID";
$password = "PW";
$dbname1 = "highmob_comenzi2";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname1 );
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = ("select * from highmob_comenzi2.players cross join highmob_comenzi2.vanzari
");
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "
</thead>
<tbody>
<tr>
<td>HERE DATABASE NAME</td>
<td><a href='vezibilettransportcomenzi.php?id=" . $row["id"] . "' target='_blank' class='btn btn-sm btn-warning'>Tip. Cert.</a></td>
<td>" . $row["id"] . "</td>
<td>
" . $row["statuscomanda"] . "
</td>
<td>" . $row["statuscomandatapiterii"] . "</td>
<td>" . $row["statuslivrare"] . "</td>
<td>" . $row["oralivrare"] . "</td>
<td>" . $row["detaliicomanda"] . "</td>
<td>
" . $row["totaldeplata"] . "</td>
<td>" . $row["avans"] . " <br><a style='color:red;'>" . $row["banipreluati"] . "</a></td>
<td>" . $row["restdeplata"] . "<br><a style='color:red;'>" . $row["banipreluatirest"] . "</a></td>
<td><a href='edit.php?id=" . $row["id"] . "' target='_blank' class='btn btn-sm btn-primary' >Vezi comanda</a></td>
</tr>
";
}
} else {
echo "Nu sunt transporturi!";
}
$conn->close();
?>
This is what i got and its working, it displays data from 1 database nammed highmob_comenzi2 and tables players and vanzari, but im getting alot of duplicates on display and also i need to insert a WHERE clause :
WHERE statuslivrare >= CURRENT_DATE()
any ideea?

How to put radio button inside the output table?

How to put radio button inside the status, 2 radio buttons (approved and disapproved) and the checked radio button will be save in database as well under status field.
This is my codes.. patient[6] is the status
$sql = "select patientid, firstname, lastname, gender, patienttype, philhealth, status from patients where lastname LIKE '%" . $_POST["key"] . "%' or philhealth LIKE '%" . $_POST["key"] . "%' ";
$result = mysql_query($sql, $connection);
$rownum = 0;
$bgcolor = "";
while($patient = mysql_fetch_array($result))
{
$rownum += 1;
if($rownum == 2)
{
$bgcolor = "#FFF";
$rownum = 0;
}
else
{ $bgcolor = "#f9f9f9"; }
$approved_checked = $patient[6] == 'Approved' ? 'checked' : '';
echo "
<tr id='" . $patient[0] . "' style='background: " . $bgcolor . "' onclick='openphilhealthapproval() '>
<td id='td27_cell1' style='height: 25px;'>" . $patient[5] . " </td>
<td id='td27_cell2' style='height: 25px;'>" . $patient[0] . "</td>
<td id='td27_cell3' style='height: 25px;'>" . $patient[1] . " " . $patient[2] . "</td>
<td id='td27_cell4' style='height: 25px;'>" . $patient[3] . "</td>
<td id='td27_cell5' style='height: 25px;'>" . $patient[4] . "</td>
<td id='td27_cell6' style='height: 25px;'>" . $patient[6] . "</td>
</tr>
";
}
You should put the content of this TD element into your status TD element. Also you need to replace ***patientID*** signs with the patientID when you build your HTML. You could see the working example here.
<td>
<input type="radio" name="statusradio***patientID***" value="approved" id="approvedradio***patientID***"/>
<label for="approvedradio***patientID***">approved</label>
<input type="radio" name="statusradio***patientID***" value="disapproved" id="disapprovedradio***patientID***"/>
<label for="disapprovedradio***patientID***">disapproved</label>
</td>

How would I get an id/name onclick using buttons generated from an SQL query?

I am creating a table from a MySQL database. Currently, I can manually enter the Course ID, then click 'select' to display a roster based on the Course ID entered.
However, I would like to streamline this with buttons on the right side of the table after each row. What is the best way to do this? I can provide a screenshot, but I still need more reputation.
So the code for generating this table is as follows.
<?php
// Connection information to database has been removed.
// $Row[1] - first name of faculty
// $Row[2] - last name of faculty
// $fieldRow[0] - course ID
// $fieldRow[1] - course name
// $fieldRow[2] - course year
// $fieldRow[3] - number of units
echo "<table>
<caption>....for " . $Row[1] . " " . $Row[2] . "</caption>
<tr>
<th>Course ID</th>
<th>Course Name</th>
<th>Course Year</th>
<th>Units</th>
</tr>";
while ($fieldRow = mysqli_fetch_row($result2))
{
echo "<tr><td>" . $fieldRow[0] . "</td>
<td>" . $fieldRow[1] . "</td>
<td>" . $fieldRow[2] . "</td>
<td>" . $fieldRow[3] . "</td>
<td><button name='" . $fieldRow[0] . "'>Select</button></td>
</tr>";
}
echo "</table>";
?>
<form method="get" action="class.php">
<p>Enter Course ID
<input autofocus="autofocus" tabindex="1" type="text" maxlength="5" name="classSelect" title="Enter Class Number" />
</p>
<p>
<button tabindex="2" type="submit">Select</button>
</p>
</form>
Thanks for looking at this!
You could have something like
if(isset($_GET['id'])
{
$course_id = intval($_GET['id']);
// Do your query against the course id.
}
while ($fieldRow = mysqli_fetch_row($result2))
{
echo "<tr><td>" . $fieldRow[0] . "</td>
<td>" . $fieldRow[1] . "</td>
<td>" . $fieldRow[2] . "</td>
<td>" . $fieldRow[3] . "</td>
<td><a href='" . ?id=$fieldRow[0] . "'>Select</a></td>
</tr>";
}
echo "</table>";
?>
you can use JQuery to do that
in your code
echo "<tr><td>" . $fieldRow[0] . "</td>
<td>" . $fieldRow[1] . "</td>
<td>" . $fieldRow[2] . "</td>
<td>" . $fieldRow[3] . "</td>
<td><button name='" . $fieldRow[0] . "' class='selecter'>Select</button></td>
</tr>";
and add a head like this
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".selecter").click(function(){
$("input").val($(this).attr("name"));
});
});
</script>
</head>
Complete code
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".selecter").click(function(){
$("input").val($(this).attr("name"));
});
});
</script>
</head>
<body>
<?php
// Connection information to database has been removed.
// $Row[1] - first name of faculty
// $Row[2] - last name of faculty
// $fieldRow[0] - course ID
// $fieldRow[1] - course name
// $fieldRow[2] - course year
// $fieldRow[3] - number of units
echo "<table>
<caption>....for " . $Row[1] . " " . $Row[2] . "
</caption>
<tr>
<th>Course ID</th>
<th>Course Name</th>
<th>Course Year</th>
<th>Units</th>
</tr>";
while ($fieldRow = mysqli_fetch_row($result2))
{
echo "<tr><td>" . $fieldRow[0] . "</td>
<td>" . $fieldRow[1] . "</td>
<td>" . $fieldRow[2] . "</td>
<td>" . $fieldRow[3] . "</td>
<td><button name='" . $fieldRow[0] . "' class='selecter'>Select</button></td>
</tr>";
}
echo "</table>";
?>
<form method="get" action="class.php">
<p>Enter Course ID
<input autofocus="autofocus" tabindex="1" type="text" maxlength="5" name="classSelect" title="Enter Class Number" />
</p>
<p>
<button tabindex="2" type="submit">Select</button>
</p>
</form>
</body>
</html>

Categories