I have a code that displays the data that is stored in a database. I want to make an edit button or link that allows me to edit the data (I have a table that displays the data in columns and rows).
Snippet of my edit code
// once saved, redirect back to the view page
header("Location: insertchart.php");
}
}
else
{
// if the 'id' isn't valid, display an error
echo 'Error!123';
}
}
else
// if the form hasn't been submitted, get the data from the db and display the form
{
// get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
if (isset($_GET['newId']) && is_numeric($_GET['newId']) && $_GET['newId'] > 0)
{
// query db
$newId = $_GET['newId'];
$result = mysql_query("SELECT * FROM charts WHERE newId=$newId")
or die(mysql_error());
$row = mysql_fetch_array($result);
// check that the 'id' matches up with a row in the databse
if($row)
{
// get data from db
$charts_date = $row['charts_date'];
$charts_retrace = $row['charts_retrace'];
$charts_start_of_swing_trade = $row['charts_start_of_swing_trade'];
$charts_end_of_swing_trade = $row['charts_end_of_swing_trade'];
$charts_bullflag = $row['charts_bullflag'];
$charts_bearflag = $row['charts_bearflag'];
$charts_ema_crossover = $row['charts_ema_crossover'];
$charts_trading_instrument = $row['charts_trading_instrument'];
// show form
renderForm($newId, $charts_date, $charts_retrace, $charts_start_of_swing_trade, $charts_end_of_swing_trade, $charts_bullflag, $charts_bearflag, $charts_ema_crossover, $charts_trading_instrument, '');
}
else
// if no match, display result
{
echo "No results!";
}
}
else
// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
{
echo 'Error!345';
}
}
?>
I get Error!345 when I click on my Edit button so I know it is quering the database fine but I have no idea why it is telling me that there is an error with my ID.
As requested, here is what my table code looks like that displays the Edit link:
$result = $conn->query($sql);
if($result && $result->num_rows > 0) {
// output data of each row
echo "<h2>What is currently inside the database?</h2><br><br>
<table style='border: solid #000000 1px;border-collapse:collapse;'>
<tr>
<td style='border: solid #000000 1px;padding:15px;'><strong><u>Chart</u></strong></td>
<td style='border: solid #000000 1px;padding:15px;'><strong><u>Date</u></strong></td>
<td style='border: solid #000000 1px;padding:15px;'><strong><u>Retrace</u></strong></td>
<td style='border: solid #000000 1px;padding:15px;'><strong><u>Start of Swing Trade</u></strong></td>
<td style='border: solid #000000 1px;padding:15px;'><strong><u>End of Swing Trade</u></strong></td>
<td style='border: solid #000000 1px;padding:15px;'><strong><u>Bull flag</u></strong></td>
<td style='border: solid #000000 1px;padding:15px;'><strong><u>Bear flag</u></strong></td>
<td style='border: solid #000000 1px;padding:15px;'><strong><u>EMA Crossover</u></strong></td>
<td style='border: solid #000000 1px;padding:15px;'><strong><u>Trading Instrument</u></strong></td>
</tr>";
while ($row=mysqli_fetch_array($result)) {
echo "<tr><td style='border: solid #000000 1px;'><img src=". $row["charts_URL"]. " width='200px'></td>";
echo "<td style='border: solid #000000 1px;'>" . $row["charts_date"]. "<br>"; echo "<a href='edit.php?id=" . $row['newId'] . "'>Edit</a></td>";
echo "<td style='border: solid #000000 1px;'>" . $row["charts_retrace"]. "</td>";
echo "<td style='border: solid #000000 1px;'>" . $row["charts_start_of_swing_trade"]. "</td>";
echo "<td style='border: solid #000000 1px;'>" . $row["charts_end_of_swing_trade"]. "</td>";
echo "<td style='border: solid #000000 1px;'>" . $row["charts_bullflag"]. "</td>";
echo "<td style='border: solid #000000 1px;'>" . $row["charts_bearflag"]. "</td>";
echo "<td style='border: solid #000000 1px;'>" . $row["charts_ema_crossover"]. "</td>";
echo "<td style='border: solid #000000 1px;'>" . $row["charts_trading_instrument"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
The Edit code is underneath the charts_date cell.
Related
I try to insert data from my php array into an html table. The code for this looks like
echo "<table style='border: 1px solid black'><tbody>";
foreach(['country','counter'] as $attribute){
echo "<tr><td>".$attribute."</td>";
foreach($analysis_data as $row){
echo "<td style='border: 1px solid black'>".$row[$attribute]."<td>";
}
echo '</tr>';
When the code is executed it looks like
I want the table to be vertical and not horizontal like this. What do I need to change in my code?
Interchange your two loops.
echo "<table style='border: 1px solid black'><tbody>";
echo "<tr><th>country</th><th>counter</th></tr>";
for ($analysis_data as $row) {
echo '<tr>';
foreach (['country','counter'] as $attribute){
echo "<td style='border: 1px solid black'>".$row[$attribute]."<td>";
}
echo '</tr>';
}
This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 6 years ago.
I am a newbie in PHP. I am working on searching function, but it does not work well and I could not find why. The problem is; the $query has been sent and accepted well however it could not find the $query in the database even though the $query existed. I think, the $sql command might be wrong somewhere, but could not find it anyway. Thank you.
Here is my code: asset_search.php
<?php
//Search data in database
$query = $_GET['query'];
$min_length = 3;
if(strlen($query) >= $min_length)
{
//$query = htmlspecialchars($query);
//$query = mysql_real_escape_string($query);
$query = strtoupper($query);
$sql = "SELECT * FROM asset WHERE ('asset_name' LIKE '%".$query."%')";
$result = mysqli_query($conn, $sql);
$row_cnt = mysqli_num_rows($result);
$count = 0;
if($row_cnt > 0)
{
echo "<table style='padding: 5px; font-size: 15px;'>";
echo "<tr><th style='width: 30px; border: 1px solid black; align:'center''>No</th>";
echo "<th style='width: 200px; border: 1px solid black; align:'center''>Status</th>";
echo "<th style='width: 200px; border: 1px solid black; align:'center''>Asset Sub-identifier</th>";
echo "<th style='width: 200px; border: 1px solid black; align:'center''>Asset Name</th>";
echo "<th style='width: 200px; border: 1px solid black; align:'center''>Asset Type</th>";
echo "<th style='width: 200px; border: 1px solid black; align:'center''>Brand</th>";
echo "<th style='width: 200px; border: 1px solid black; align:'center''>Service Tag/ Product Tag/ Product S/N</th>";
echo "<th style='width: 200px; border: 1px solid black; align:'center''>CSM Tag</th>";
echo "<th style='width: 200px; border: 1px solid black; align:'center''>Action</th></tr>";
while($row = mysqli_fetch_assoc($result))
{
echo "<tr><td align='center'>" . ++$count . "</td>";
echo "<td align='center'>" . $row["asset_status"] . "</td>";
echo "<td align='center'><a href='asset_viewfull.php?asset_id=" . $row["asset_id"] . "'><ins>" . $row["asset_subidentifier"] . "</a></ins></td>";
echo "<td align='center'>" . $row["asset_name"] . "</td>";
echo "<td align='center'>" . $row["asset_type"] . "</td>";
echo "<td align='center'>" . $row["asset_brand"] . "</td>";
echo "<td align='center'>" . $row["asset_sertag"] . "</td>";
echo "<td align='center'>" . $row["asset_csmtag"] . "</td>";
if($row["asset_status"] == "DISPOSE")
{
echo "<td align='center'><a href='asset_delete.php?asset_id=" . $row["asset_id"] . "'>Delete</a>";
echo " ";
echo "<a href='asset_print.php?asset_id=" . $row["asset_id"] . "'>Print</a></td></tr>";
}else
{
echo "<td align='center'><a href='asset_editform.php?asset_id=" . $row["asset_id"] . "'>Edit</a>";
echo " ";
echo "<a href='asset_delete.php?asset_id=" . $row["asset_id"] . "'>Delete</a>";
echo " ";
echo "<a href='asset_disposeform.php?asset_id=" . $row["asset_id"] . "'>Dispose</a>";
echo " ";
echo "<a href='asset_print.php?asset_id=" . $row["asset_id"] . "'>Print</a></td></tr>";
}
}
}else
{
echo "<tr> There is no asset in the database </tr>";
}
echo "</table>";
}
else
{
echo "<script languange = 'Javascript'>
alert('Minimum length is' .$min_length);</script>";
}
//Close connection
mysqli_close($conn);
$count = 0;
?>
Change your query to the following:
SELECT * FROM asset WHERE (`asset_name` LIKE '%".$query."%')
Note the `` around asset_name instead of ''
you should try this without the brackets sometimes it trows out the search,
$sql = "SELECT * FROM asset WHERE `asset_name` LIKE '%{$query}%'";
this is how i preform this task and has never failed me yet!
I want to create a filter which filters my ouput from the database. I have no idea how to create this. I have searched some other topics but they were not so helpful, hopefully someone can help me out.
I have the following code which retrieves the information from my database and ouputs it into a table. (please ignore the table set-up and such, still need to clean the nesting and stuff..)
Code (retrieve.php):
<?php
// Connect to database server
mysql_connect("xx", "xx", "xx") or die (mysql_error ());
// Select database
mysql_select_db("xx") or die(mysql_error());
// SQL query
$strSQL = "SELECT * FROM informatie ORDER BY id DESC;";
// Execute the query (the recordset $rs contains the result)
$rs = mysql_query($strSQL);
//OUDE LOOP SPACE
// Write the value of the column FirstName (which is now in the array $row)
echo "<table width='100%' border='0' cellpadding='10' cellspacing='0'><tr style='background-color:#f04d44; color:#ffffff;'>";
echo "<td style='border-right:1px solid #ff9a88;'><strong>Klant</strong></td><td style='border-right:1px solid #ff9a88;'><strong>Datum</strong></td><td style='border-right:1px solid #ff9a88;'><strong>Eventviewer Nagekeken</strong></td><td style='border-right:1px solid #ff9a88;'><strong>Eventviewer Opmerkingen</strong></td><td style='border-right:1px solid #ff9a88;'><strong>Services Nagekeken</strong></td><td style='border-right:1px solid #ff9a88;'><strong>Services Opmerkingen</strong></td><td style='border-right:1px solid #ff9a88;'><strong>Backup Nagekeken</strong></td><td style='border-right:1px solid #ff9a88;'><strong>Backup Opmerkingen</strong></td><td><strong>Check Gedaan Door</strong></td>";
echo "</tr>";
// Loop the recordset $rs
// Each row will be made into an array ($row) using mysql_fetch_array
while($row = mysql_fetch_array($rs)) {
echo "<tr style='background-color:#ffffff;'>";
echo "<td style='border-right:1px solid #cccccc; border-bottom:1px solid #cccccc;'>";
echo $row['klant'];
echo "</td>";
echo "<td style='border-right:1px solid #cccccc; border-bottom:1px solid #cccccc;'>";
echo $row['datum'];
echo "</td>";
echo "<td style='border-right:1px solid #cccccc; border-bottom:1px solid #cccccc;'>";
echo $row['eventviewer_nagekeken'];
echo "</td>";
echo "<td style='border-right:1px solid #cccccc; border-bottom:1px solid #cccccc;'>";
echo $row['eventviewer_opmerkingen'];
echo "</td>";
echo "<td style='border-right:1px solid #cccccc; border-bottom:1px solid #cccccc;'>";
echo $row['services_nagekeken'];
echo "</td>";
echo "<td style='border-right:1px solid #cccccc; border-bottom:1px solid #cccccc;'>";
echo $row['services_opmerkingen'];
echo "</td>";
echo "<td style='border-right:1px solid #cccccc; border-bottom:1px solid #cccccc;'>";
echo $row['backup_nagekeken'];
echo "</td>";
echo "<td style='border-right:1px solid #cccccc; border-bottom:1px solid #cccccc;'>";
echo $row['backup_opmerkingen'];
echo "</td>";
echo "<td style='border-bottom:1px solid #cccccc;'>";
echo $row['check_door'];
echo "</td>";
}
echo "</tr></table>";
// Close the database connection
mysql_close();
?>
Thanks for helping me out!
For filtering, most of the time you can use your database.
Right now your SQL statement is like this:
$strSQL = "SELECT * FROM informatie ORDER BY id DESC;";
To filter on information from the table informatie, you should alter the SQL statment, for example:
$strSQL = "SELECT * FROM informatie WHERE `eventviewer_nagekeken` = 1 ORDER BY id DESC;";
This will return all rows where eventviewer_nagekeken equals 1. This way you will be able to create various outputs from the pool of data you have in the informatie table.
For documentation and examples, see the docs.
you can use where clause generated dynamically as bellow
$where = ' 1=1 ';
if(isset($_REQUEST['param1']))
{
$where .= " and db_field1 = ".mysql_real_escape_string($_REQUEST['param1']);
}
if(isset($_REQUEST['param2']))
{
$where .= " and db_field2 = ".mysql_real_escape_string($_REQUEST['param2']);
}
$strSQL = "SELECT * FROM informatie ".$where." ORDER BY id DESC;";
here mysql_real_escape_string will avoid mysql injection.
Hope this helps you.
Thanks to stackoverflow and its great solution, I have found a way to limit the characters in a table but it doesn't work for me. I tried a lot but with no success.
This is my table
<?php
$result = mysqli_query($conn,"SELECT * FROM library ORDER BY `CreatedTime` DESC");
echo "<table class='table-fill' border='0' cellpadding='0' cellspacing='0'>
<tr>
<th position='fixed' overflow='hidden' width='10%'>Book Name</th>
<th width='5%'></th>
</tr>";
while($row = mysqli_fetch_array($result) ) {
echo "<tr>";
echo "<td colspan='2' style='padding-bottom: 0;'><a href='library.details.php?id=". $row['id']."' target='content' class='positiontitle-link'><font style='text-shadow: none; font-weight: 800;'>" . $row['bookname']. "</td>";
echo "</tr>";
echo "<tr style='border-top-width: 0; padding-top: 0;'>";
echo '<td style="max-height: 10px;">' . $str . '</td>';
echo "<td style=' padding-top: 0; padding-left: 15px; width: 40%;'> <font color='gray'>Author :</font> " .($row['authorname'] ). "</td>";
echo "<td width='5%' style=' padding-top: 0;'> <font color='gray'>Year Published </font>" . $row['yearpublished'] . "</td>";
echo "</tr>";
if (strlen($row['bookname']) > 1) $str = substr($row['bookname'], 0, 1) . "...";
}
echo"</table>";
?>
This is how it looks like
Any help will be appreciated.
I'm doing this out of my head so forgive me any format issues and such...
Move the string length check to just under the while.
Overwrite $row['bookname'] instead of creating $str.
Remove the line with:
echo '<td style="max-height: 10px;">' . $str . '</td>';
Result:
while($row = mysqli_fetch_array($result) ) {
if (strlen($row['bookname']) > 9) $row['bookname'] = substr($row['bookname'], 0, 9) . "...";
echo "<tr>";
echo "<td colspan='2' style='padding-bottom: 0;'><a href='library.details.php?id=". $row['id']."' target='content' class='positiontitle-link'><font style='text-shadow: none; font-weight: 800;'>" . $row['bookname']. "</td>";
echo "</tr>";
echo "<tr style='border-top-width: 0; padding-top: 0;'>";
echo "<td style=' padding-top: 0; padding-left: 15px; width: 40%;'> <font color='gray'>Author :</font> " .($row['authorname'] ). "</td>";
echo "<td width='5%' style=' padding-top: 0;'> <font color='gray'>Year Published </font>" . $row['yearpublished'] . "</td>";
echo "</tr>";
}
I want to convert the valve which are inside of the DIV tag to PDF file
<div id="container2">
<?php
$con=mysql_connect('localhost','root','');
mysql_select_db('account',$con);
$query1="Select * from e_bills";
$result=mysql_query($query1);
echo "<table width='1000' border='5' cellpadding='2' id='bill'>
<tr>
<th style='border:1px solid black'><input type='checkbox'></th>
<th style='border:1px solid black'>BillNo</th>
<th style='border:1px solid black'>Amount</th>
<th style='border:1px solid black'>VendorName</th>
<th style='border:1px solid black'>Date</th>
<th style='border:1px solid black'>Paid</th>
<th style='border:1px solid black'>Pending</th>
<th style='border:1px solid black'>Action</th</tr>";
//$res=mysql_fetch_array($result);
//print_r($res);
while($res=mysql_fetch_array($result))
{
$pending=$res['bi_total']-$res['bi_paid'];
echo "<tr> <td style='border:1px solid black';>";
echo "<input type='checkbox' class='ads_Checkbox' id='checkbox.$res[bi_billnumber]' value='$res[bi_billnumber]'>";
echo " </td>";
echo "<td style='border:1px solid black';>";
echo "B_".$res['bi_billnumber'];
echo "</td>";
echo "<td style='border:1px solid black';>";
echo $res['bi_total'];
echo "</td> ";
echo "<td style='border:1px solid black';><a style='color:red;text-decoration:underline;cursor:pointer;'onclick='getdata1($res[bi_billnumber])'>";
echo $res['bi_vendername'];
echo "</a></td>";
echo "<td style='border:1px solid black';>";
echo $res['bi_date'];
echo "</td>";
echo "<td style='border:1px solid black';>";
echo $res['bi_paid'];
echo "</td>";
echo "<td style='border:1px solid black';>";
echo "$pending";
echo "</td>";
echo "<td style='border:1px solid black';>";
echo "<input type='button' name='button' value='Edit' onclick='getdata($res[bi_billnumber])'>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
</form>
</div>
I have Used
<?php
include("/MPDF53/MPDF53/mpdf.php");
$mpdf=new mPDF('c','A4','','' , 0 , 0 , 0 , 0 , 0 , 0);
$mpdf->SetDisplayMode('fullpage');
$mpdf->list_indent_first_level = 0; // 1 or 0 - whether to indent the first level of a list
$mpdf->WriteHTML(file_get_contents('billdisplay.php'));
$mpdf->Output();
?>
But useful for only from static values not for the data which is coming from the database.
You can use the dompdf for converting your html to pdf. It is so simple and it also take php code . Dompdf Example