我有一个代码,显示存储在数据库中的数据。我想创建一个允许我编辑数据的编辑按钮或链接(我有一个表格,用于显示列和行中的数据)。
我的编辑代码片段
// 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';
}
}
?>
当我点击我的编辑按钮时,我得到错误!345我知道它正在查询数据库,但我不知道为什么它告诉我我的ID有错误。
根据要求,这是我的表格代码,显示编辑链接:
$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";
}
编辑代码位于charts_date单元格下面。