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;'><a href=" . $row["charts_URL"]. "><img src=". $row["charts_URL"]. " width='200px'></a></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.