could someone help on below code? I have table data on that can allow user to click button to delete the row. I got error said " Undefined index: tenant_id " , Undefined variable: row. Below are delete.php and pg-t-payment-view.php script :
<?php
$db = pg_connect("host=10.0.32.204 port=5432 dbname=postgres user=postgres password=postgres");
$id = $_POST['tenant_id'];
$sql2 ="DELETE FROM payment_ref_tenancy WHERE tenant_name = '$id'";
$result = pg_query($sql2);
$cmdtuples = pg_affected_rows($result);
echo $cmdtuples . " record affected.\n";
if (!$result) {
$errormessage = pg_last_error();
echo "Error with query: " . $errormessage;
exit();
}
pg_close();
header('location:pg-t-payment-view.php');
?>
-------------pg-t-payment-view.php script
<div class="box-body table-responsive no-padding">
<?php
$db = pg_connect("host=10.0.32.204 port=5432 dbname=postgres user=postgres password=postgres");
$sql2 ="select tenant_id,to_char(last_update_time, 'MM-dd-yyyy HH24:MI') as last_update_time , tenant_name, tenant_cost_category, invoice_no,tenant_agreed_cost,
to_char(submission_date, 'MM-dd-yyyy') as submission_date,to_char(cpr_submission_to_finance, 'MM-dd-yyyy') as cpr_submission_to_finance,io,cheque_no
FROM payment_ref_tenancy order by insert_datetime desc limit 10";
$result = pg_query($db,$sql2);
if (!$result) {
$errormessage = pg_last_error();
echo "Error with query: " . $errormessage;
exit();
}
pg_close();
echo "<table class='table table-hover table-striped'>";
echo "<th align='center' >Date</th>";
echo "<th align='center' >Payee</th>";
echo "<th align='center' >Category</th>";
echo "<th align='center' >Cost (RM)</th>";
echo "<th align='center' >Invoice No</th>";
echo "<th align='center' >Payment Submission Date</th>";
echo "<th align='center'>CPR Submission to Finance</th>";
echo "<th align='center'>IO</th>";
echo "<th align='center'>Cheque No</th>";
echo "<th align='center' div style ='color:#ff0000'>Action</th>";
echo "<th align='center'></th>";
echo "<th align='center'></th>";
while($row=pg_fetch_assoc($result))
{ $id = $row['tenant_id'];
echo "<tr>";
echo "<td>" . $row['last_update_time'] . "</td>";
echo "<td>" . $row['tenant_name'] . "</td>";
echo "<td>" . $row['tenant_cost_category'] . "</td>";
echo "<td>" . $row['tenant_agreed_cost'] . "</td>";
echo "<td>" . $row['invoice_no'] . "</td>";
echo "<td>" . $row['submission_date'] . "</td>";
echo "<td>" . $row['cpr_submission_to_finance'] . "</td>";
echo "<td>" . $row['io'] . "</td>";
echo "<td>" . $row['cheque_no'] . "</td>";
echo "<td><a href='pg-t-payment-edit.php'>Edit</a></td>";
echo "<td><a href='delete.php?id=$id'><input type='hidden' name='id' value=$id>Delete</a></td>";
echo "</tr>";}
echo "</table>";
?>
Try to move pg_close(); in pg-t-payment-view.php somewhere after you have already worked with data fetched from database.
So basically after this loop executes: while($row=pg_fetch_assoc($result))
Change name of argument you pass in your delete button to reflect column name or change in delete.php how you get said ID.
<div class="box-body table-responsive no-padding">
<?php
$db = pg_connect("host=10.0.32.204 port=5432 dbname=postgres user=postgres password=postgres");
$sql2 ="select tenant_id,to_char(last_update_time, 'MM-dd-yyyy HH24:MI') as last_update_time , tenant_name, tenant_cost_category, invoice_no,tenant_agreed_cost,
to_char(submission_date, 'MM-dd-yyyy') as submission_date,to_char(cpr_submission_to_finance, 'MM-dd-yyyy') as cpr_submission_to_finance,io,cheque_no
FROM payment_ref_tenancy order by insert_datetime desc limit 10";
$result = pg_query($db,$sql2);
if (!$result) {
$errormessage = pg_last_error();
echo "Error with query: " . $errormessage;
pg_close();
exit();
}
echo "<table class='table table-hover table-striped'>";
echo "<th align='center' >Date</th>";
echo "<th align='center' >Payee</th>";
echo "<th align='center' >Category</th>";
echo "<th align='center' >Cost (RM)</th>";
echo "<th align='center' >Invoice No</th>";
echo "<th align='center' >Payment Submission Date</th>";
echo "<th align='center'>CPR Submission to Finance</th>";
echo "<th align='center'>IO</th>";
echo "<th align='center'>Cheque No</th>";
echo "<th align='center' div style ='color:#ff0000'>Action</th>";
echo "<th align='center'></th>";
echo "<th align='center'></th>";
while($row=pg_fetch_assoc($result))
{ $id = $row['tenant_id'];
echo "<tr>";
echo "<td>" . $row['last_update_time'] . "</td>";
echo "<td>" . $row['tenant_name'] . "</td>";
echo "<td>" . $row['tenant_cost_category'] . "</td>";
echo "<td>" . $row['tenant_agreed_cost'] . "</td>";
echo "<td>" . $row['invoice_no'] . "</td>";
echo "<td>" . $row['submission_date'] . "</td>";
echo "<td>" . $row['cpr_submission_to_finance'] . "</td>";
echo "<td>" . $row['io'] . "</td>";
echo "<td>" . $row['cheque_no'] . "</td>";
echo "<td><a href='pg-t-payment-edit.php'>Edit</a></td>";
echo "<td><a href='delete.php?tenant_id=$id'><input type='hidden' name='id' value=$id>Delete</a></td>";
echo "</tr>";}
pg_close();
echo "</table>";
?>
Change in your delete.php how you get and use ID. If your tenant_id is integer, you can omit quotes from DELETE query.
<?php
$db = pg_connect("host=10.0.32.204 port=5432 dbname=postgres user=postgres password=postgres");
$id = $_GET['tenant_id'];
$sql2 ="DELETE FROM payment_ref_tenancy WHERE tenant_id = '$id'";
$result = pg_query($sql2);
$cmdtuples = pg_affected_rows($result);
echo $cmdtuples . " record affected.\n";
if (!$result) {
$errormessage = pg_last_error();
echo "Error with query: " . $errormessage;
exit();
}
pg_close();
header('location:pg-t-payment-view.php');
?>
Just so you know, this code can be SQL Injected, this is security issue if untrusted people will use it.
Related
I have a PHP page with the below code. I do not want to run the code directly after the page is opened. I want it to wait about 5 min and then run the SQL code.
<?php
$link = mysqli_connect("localhost","root","****#","****");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT * FROM ver_truck WHERE sla_client = 'Woodford' ORDER BY RAND() DESC LIMIT 1";
$array = "";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table width='100%' style='font-size: small'>";
echo "<tr align='center'>";
echo "<th style='font-weight: bolder'>Reg</th>";
echo "<th style='font-weight: bolder'>Model</th>";
echo "<th style='font-weight: bolder'>Client</th>";
echo "<th style='font-weight: bolder'>Action</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr align='left'>";
echo "<td align='center' style='color: red; font-weight: bold'>" . $row['reg'] . "</td>";
echo "<td align='center'>" . $row['make'] . "</td>";
echo "<td align='center'>" . $row['sla_client'] . "</td>";
echo "<td align='center'><a href='/cases/verifications/verification_alarm_new.php?imei=" . $row['imei'] . "&type=" . $row['event_desc'] . "®=" . $row['name'] . "&event_id=" . $row['event_id'] . " '><button>Action</button></a></td>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No Current Verifications.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
Will the sleep() command in the beginning of this code prevent it from running for 5 minutes?
I want to add "book" button to each car product.But it only display only one button for first car only.
$sql = "SELECT * FROM car";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Id</th>";
echo "<th>Name</th>";
echo "<th>Price(RM)</th>";
echo "<th>Colour</th>";
echo "<th>Mode</th>";
echo "<th>Image</th>";
echo "<th>Status</th>";
echo "<td><button onclick=\"book_car('" . $row['car_id'] .
"')\">Book</button></td>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['car_id'] . "</td>";
echo "<td>" . $row['car_name'] . "</td>";
echo "<td>" . $row['car_price'] . "</td>";
echo "<td>" . $row['car_colour'] . "</td>";
echo "<td>" . $row['car_mode'] . "</td>";
echo "<td><img src='" . $row['car_image'] . "' height='100'
width='100'></td>";
echo "<td>" . $row['car_status'] . "</td>";
echo "</tr>";
}
There is no error.But i just want "book" button display for each car products.
This is simply because your button is out of the while loop !
Also you did not close first tr tag .
Correct code :
$sql = "SELECT * FROM car";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Id</th>";
echo "<th>Name</th>";
echo "<th>Price(RM)</th>";
echo "<th>Colour</th>";
echo "<th>Mode</th>";
echo "<th>Image</th>";
echo "<th>Status</th>";
echo "<th>action</th>";<!-- Added this line -->
echo "</tr>";<!-- Added this line -->
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['car_id'] . "</td>";
echo "<td>" . $row['car_name'] . "</td>";
echo "<td>" . $row['car_price'] . "</td>";
echo "<td>" . $row['car_colour'] . "</td>";
echo "<td>" . $row['car_mode'] . "</td>";
echo "<td><img src='" . $row['car_image'] . "' height='100'
width='100'></td>";
echo "<td>" . $row['car_status'] . "</td>";
echo "<td><button onclick=\"book_car('" . $row['car_id'] .
"')\">Book</button></td>";<!-- Replaced This line -->
echo "</tr>";
}
echo "</table>";
I hope this helps you :)
This is my code (horrible one):
<?php
include 'connect/con.php';
$result = mysqli_query($con,"SELECT id, vidTitle FROM newsvid");
$result1 = mysqli_query($con,"SELECT imgCover, vidSD FROM newsvid");
$result2 = mysqli_query($con,"SELECT published FROM newsvid");
echo "<table width=\"600\" border=\"1\"><tbody>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo '<td width=\"10%\">'.$row['id'].'</td>';
echo "<td width=\"90%\">" . $row['vidTitle'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
echo "<table width=\"600\" border=\"1\"><tbody>";
while($row = mysqli_fetch_array($result1)) {
echo "<tr>";
echo "<td width=\"40%\">" . $row['imgCover'] . "</td>";
echo "<td width=\"60%\">" . $row['vidSD'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
echo "<table width=\"600\" border=\"1\"><tbody>";
while($row = mysqli_fetch_array($result2)) {
echo "<tr>";
echo "<td >" . $row['published'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
mysqli_close($con);
?>
</body>
</html>
The question is how to show data from database in this layout:
--------------------------------
-id----------vidTitle-----------
--------------------------------
-imgCover------vidSD------------
--------------------------------
----------published-------------
So every time I will add more data , another block like I showed before will add up under existing one.
........................................................................................
There's no need to write 3 queries. You could do that with only one select, and then put all the echos inside a while. That way you're writing, it would run all the ids and titles first, then it would put a table after the table, with cover and vidSD.
Try to make a single query:
SELECT id, vidTitle, imgCover, vidSD, published FROM newsvid
That way you will have, on each row returned from database, all the information about the same row.
Now, running a while is the same as you're doing, just adapting some HTML:
echo "<table width='600' border='1'><tbody>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo '<td width=\"10%\">'.$row['id'].'</td>';
echo "<td width=\"90%\">" . $row['vidTitle'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td width=\"40%\">" . $row['imgCover'] . "</td>";
echo "<td width=\"60%\">" . $row['vidSD'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='2'>" . $row['published'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
You may want to order it too. Adding ORDER BY id DESC, would do that.
I've been working on creating an internal site for our company. I haven't had many issues until now. I have been able to retrieve and insert data into my database, but now for some reason when I try to UPDATE an entry, the database can't be selected for some strange reason. I've attached a copy of my code thus far. I don't know what I am missing. Thank you!
This is my code for looking up the information in the database:
<?php
session_start();
$transport = mysqli_connect("localhost", "user", "pw", "db_name");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<?php
$raw_date = $_POST['appt_date'];
$date = date("Y-m-d", strtotime($raw_date));
if ($raw_date == '') {
echo "Please go back and pick a date";
exit;
}
$sql = "SELECT * FROM appointments WHERE date = '".$date."' ORDER BY appttime";
$result = mysqli_query($transport, $sql);
$i=0;
echo "<h2 align='center'>Schedule for $raw_date</h2>";
echo "<table border='2' style='width: 100%; margin: auto; border-width: 1px'><tr><th>Resident Name</th><th>APT #</th><th>Appt. Time</th><th>Location Phone</th><th>Location Name</th><th>Address</th><th>City</th><th>Zip</th><th>Bus or Car</th><th>Escort Name</th><th>Transfer</th><th>Comments</th><th>Dparting Times</th></tr>";
echo "<form name='update_times' method='post' action='depart.php'>\n";
while($row = mysqli_fetch_array($result))
{
echo "<input type='hidden' name='id[$i]' value=" . $row['id'] . "";
echo "<tr>";
echo "<td align='center'>" . $row['r_name'] . "</td>";
echo "<td align='center'>" . $row['room'] . "</td>";
echo "<td align='center'>" . date("g:i A", strtotime($row['appttime'])) . "</td>";
echo "<td align='center'>" . $row['apptphone'] . "</td>";
echo "<td align='center'>" . $row['l_name'] . "</td>";
echo "<td align='center'>" . $row['address'] . "</td>";
echo "<td align='center'>" . $row['city'] . "</td>";
echo "<td align='center'>" . $row['zip'] . "</td>";
echo "<td align='center'>" . $row['buscar'] . "</td>";
echo "<td align='center'>" . $row['escort_name'] . "</td>";
echo "<td align='center'>" . $row['transfer'] . "</td>";
echo "<td align='center'>" . $row['comments'] . "</td>";
echo "<td align='center'><input name='out[$i]' style='width: 70px' type='text' value='" . date("g:i A", strtotime($row['depart'])) . "' /></td>";
echo "</tr>";
++$i;
}
echo "<input type='submit' value='Set Depart Times'>";
echo "</form>";
echo "</table>";
$_SESSION['sessionVar'] = $raw_date;
?>
This is the update code:
<?php
session_start();
$transport = mysqli_connect('localhost', 'user', 'pw', 'db_name');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<?php
$size = count($_POST['out']);
$i=0;
while ($i < $size)
{
$departing = $_POST['out'][$i];
$departing = date("H:i:s:u",strtotime($departing));
$id = $_POST['id'][$i];
$sql = "UPDATE transport.appointments SET depart = $departing WHERE id = $id";
mysqli_query($transport, $sql) or die ("Error in query: $sql");
echo "Depart times updated!";
++$i;
}
mysql_close($transport);
?>
For some reason the update code doesn't want to select my database. Thank you again!
The MySQL UPDATE syntax is:
UPDATE table_name SET column1=value1,column2=value2,... WHERE some_column=some_value;
It looks like you've tried to reference your database and your table in the UPDATE query: transport.appointments. I might be wrong, but I can't find anything on the internet saying that is valid syntax.
Try just referencing the table:
$sql = "UPDATE appointments SET depart = $departing WHERE id = $id";
I want to create a compare sort of page. I currently have two tables with the information, but want it to be on table or join both together. The first row would show Name of brand, second would show cost, etc... I am grabbing the values of selected checkboxs and based on what they selected it get compared.
$testName = $_POST['testCompare'];
$rpl = str_replace("_", " ", $testName);
$firstOne = "$rpl[0]";
$secondOne = "$rpl[1]";
echo "$firstOne";
echo "<br/>";
echo "$secondOne";
$query = "SELECT * FROM test_table WHERE test_name = '$firstOne'";
$query2 = "SELECT * FROM test_table WHERE test_name = '$secondOne'";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
$result2 = mysql_query($query2) or die ("Error in query: $query2. " . mysql_error());
if (mysql_num_rows($result) > 0 && mysql_num_rows($result2) > 0) {
//if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_row($result)) {
if ($firstOne == $row[1]) {
{
echo "<table border=1>";
echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "<tr>";
echo "<td>" . $row[1] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row[2] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row[3] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row[4] . "</td>";
echo "</tr>";
}
}
}echo "</table>";
while($row2 = mysql_fetch_row($result2)) {
if ($secondOne == $row2[1]) {
{ echo "<table border=1>";
echo "<tr>";
echo "<td>" . $row2[0] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row2[1] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row2[2] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row2[3] . "</td>";
echo "</tr>";
echo "<td>" . $row2[4] . "</td>";
echo "</tr>";
}
}
}
echo "</table>";
}
else {
echo "No Results";
}[/CODE]
Thanks
Remove the </table> after the first loop.
Remove <table border=1> from both loops.
Add <table border=1 before the first loop.
Currently, you're defining a new <table border=1> each time you enter the loop. This will result in this HTML code:
<table ..>
<tr>...
<table ..>
..
<table>
..
Et cetera
</table>
<table ..>
Et cetera
</table>