0

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.

Thomas
  • 69
  • 1
  • 7
  • this is PHP not javascript/jQuery besides that i cannot determine your error with the code you posted you have so many close braces `}` – Santiago Hernández Apr 29 '15 at 00:22
  • Do this: echo "SELECT * FROM charts WHERE newId=$newId"; Then copy and paste what is echoed it into your database server manually to see what is returned. – kojow7 Apr 29 '15 at 01:17
  • I get: #1054 - Unknown column '$newId' in 'where clause' – Thomas Apr 29 '15 at 01:43
  • but the script is trying to get the ID for the data that was updated (I posted half of the code that shows the updated data being fetched and displayed) so ho would I modify the code to make it work? – Thomas Apr 29 '15 at 01:44
  • Could you post the whole code? You´re talking about an edit-link, which is not shown in your question – Qullbrune Apr 29 '15 at 19:38
  • @BigRabbit - that newId code still brought up the error message when I tested it. The exact error when I tried it in the database server is: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"' at line 1 – Thomas Apr 30 '15 at 02:35
  • @tt_de - check my code again. I added my table code with the Edit link. – Thomas Apr 30 '15 at 02:35
  • you're using mysql_* and mysqli_* which will cause problems. You also should not be using mysql_* because its [deprecated](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). – BRBT Apr 30 '15 at 12:09

0 Answers0