mysql - Not able to delete or update a row in PHP? -
i working on first php app simple notes taking application. using different urls notes -
echo "<p><a href='/see_note.php?id={$row["note_id"]}'>{$row['note']}</a></p>";
so, primary key acts parameter url of note. link redirects me note without problem, when try delete or update note throws error -
undefined index: id in /home/user/phpstormprojects/notes/see_note.php on line 17
line 17 extracting id url -
$note_id = $_get['id'];
here rest of code page displaying note-
//get id url $note_id = $_get['id']; //fetch note database $query = "select note notes note_id = '$note_id'"; $query_result = mysqli_query($dbconn, $query) or die(mysqli_error($dbconn)); $row = mysqli_fetch_assoc($query_result); $note = $row['note']; //update note if(isset($_post['save_note_btn'])) { $note = $_post['note']; $query = "update notes set note = '$note' note_id ='$note_id'"; $query_result = mysqli_query($dbconn, $query) or die(mysqli_error($dbconn)); header("location: home.php"); } //delete note if(isset($_post['del_note_btn'])) { $query = "delete notes note_id = '$note_id'"; $query_result = mysqli_query($dbconn, $query) or die(mysqli_error($dbconn)); header("location: home.php"); } mysqli_close($dbconn); ?> //this note displayed , edited <form method="post" action="see_note.php"> <textarea name="note" placeholder="your note" rows="20" cols="140"> <?php echo $note; ?> </textarea> <button type="submit" name="save_note_btn">save</button> </form> //button delete note <form method="post" action="see_note.php"> <button type="submit" name="del_note_btn">delete</button> </form>
the code seems fine me, have no idea causing error. maybe have made silly mistake. please help.
the problem because of following 2 lines,
//this note displayed , edited <form method="post" action="see_note.php"> ^^^^^^^^^^^^ ...
and
//button delete note <form method="post" action="see_note.php"> ^^^^^^^^^^^^ ...
you need append note's id along action
attribute, this:
//this note displayed , edited <form method="post" action="see_note.php?id=<?php echo $note_id; ?>"> ...
and
//button delete note <form method="post" action="see_note.php?id=<?php echo $note_id; ?>"> ...
sidenote:
keep line is,
//get id url $note_id = $_get['id'];
Comments
Post a Comment