0

I need some help with PHP and SQL. Have looked for help in older questions and answers here but can't find anyting like my problem. I'm doing a webbsite where you can post notes in different subjects (Work, Home, School, and so on). After every note that being selected from my database I have a delete button that deletes the selected note.

Now I want to add a change/update button next to the delete button. When pressing the update button I want to be redirected to another page (update.php for example) where that original note is, ready to type in something new or change the note. Then pressing another update button that sends and replace the note at my database table. I have no idea how to to that and would be so grateful for all the help I can get.

This is my code that shows all the current posts for the subject work and where the update button should be next to the delete button.

<?php 

if(isset($_POST['delete'])){
   $id = $_POST['delete_rec_id'];  
   $query = "DELETE FROM notes WHERE id=$id"; 
   $result = mysql_query($query);
}

$query = "SELECT * FROM notes WHERE subject='Work' order by id desc";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) { 
        $id = $row['id'];
        $subject = $row['subject'];
        $date = $row['date'];
        $note = $row['note']; 

        print "<p><strong>$subject</strong> ($id), $date </p>"; 
        print "<p> $note </p>";

    ?>
    //delete button starts here here
    <form id="delete" method="post" action="">
    <input type="hidden" name="delete_rec_id" value="<?php print $id; ?>"/> 
    <input type="submit" name="delete" value="Delete!"/>  
    </form>
    //THIS IS WHERE THE DELETE BUTTON SHOULD BE
    <?php
}   
?>

So grateful for any help i can get :)

Miranda
  • 25
  • 1
  • 2
  • 4
  • Have a look at SQL injection, before anything else: http://en.wikipedia.org/wiki/SQL_injection – wanovak Aug 06 '12 at 17:11
  • Aside from this snippet, what do you have? Do you already have a page that handles editing/updating the notes? – Mr. Llama Aug 06 '12 at 17:11
  • 1
    You should stop using `mysql_*` functions. They're being deprecated. Instead use [PDO](http://php.net/manual/en/book.pdo.php) (supported as of PHP 5.1) or [mysqli](http://php.net/manual/en/book.mysqli.php) (supported as of PHP 4.1). If you're not sure which one to use, [read this SO article](http://stackoverflow.com/questions/13569/mysqli-or-pdo-what-are-the-pros-and-cons). – Matt Aug 06 '12 at 17:18
  • No but I will create a update.php page that you will be directed to when pressing update on the page where all the notes are. But I don't know how I will get the original note to that page so I easily change and update it. – Miranda Aug 06 '12 at 17:19
  • Aside from that, take a look at [javascript and DHTML](http://www.w3schools.com/dhtml/dhtml_javascript.asp). This is a basic intro to how you can dynamically modify a page's contents using javascript, but it's definitely not the best solution (most would consider the best solution to be jQuery, a javascript library). – Matt Aug 06 '12 at 17:20

1 Answers1

0

Basicly, the update button will just redirect the user to another page. So:

<form id="delete" method="post" action="">
<input type="hidden" name="delete_rec_id" value="<?php print $id; ?>"/> 
<input type="submit" name="delete" value="Delete!"/>  
</form>
<input type="button" name="redirect<?php print $id; ?>" value="Update" onClick="location.href( 'http://www.domain.com/update.php?id=<?php print $id; ?>');"/>

In your update.php file you just use $_GET to access the ID of the row.

$id =$_GET['id'];
echo "You want to edit the row with ID = ".$id;
Ofir Baruch
  • 10,323
  • 2
  • 26
  • 39