0

From the beginning. I'm working on a small web application, using PHP. It consists in some kind of notepad, you can write a note and it'll save it for you. My problem is in deleting those notes, I can't figure out a way to delete the correct note. I query the database and show everything with a simple loop:

while($rows=mysqli_fetch_assoc($query_for_content)){
    echo '
        <div class="col-md-4">
        <h2>' . $rows['title_text']. '</h2>
        <p style="overflow-y:scroll; max-height:150px">' . $rows['txt_mensagem'].  '</p>
        <p style="color:#A0A0A0; font-size:90%;">' . $rows['date_creation'].  '</p>
        <p><a class="btn btn-secondary" id="row_table" data-toggle="modal" data-target="#open_modal_edit"  role="button">Detail &raquo;</a></p>
        </div>'; 
}

As you can see I gave it some simple style, and put it inside a div.

How note are being shown

I've been thinking about my problem for some time, googled, asked friends, no luck.

The problem is that I can't figure out the correct query to delete the message. As shown in the picture I have some fields as "Title", "Message" and "Date of creation". I thought about using those field to create the query, but I can't access the fields.

I hope I didn't confuse you, and in case I did, please ask me so I can help you help me :D

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • 1
    create a button that onclick has a function that you can pass a parameter, this parameter should be the id of the note. The function makes an ajax call and deletes the record from the database – Rafael Shkembi Jan 05 '17 at 16:26
  • First you need to figure the solution on your DB, then move the solution to PHP. If your DB have an ID collumn, use it to delete the row. – Danielzt Jan 05 '17 at 16:43
  • @Danielzt But how do I identify the message? For example, imagine I have a message that is in a div and I wanna delete it, how do I build the query using information from my div? Is that possible? – Pavlo Zakharuk Jan 05 '17 at 16:48
  • Yes it is possible. When you are generating the HTML, put the ID you have on the table, inside the HTML. Like Gautam solution, put the ID dinamically. Delete – Danielzt Jan 05 '17 at 20:18
  • If you have the ID only inside the HTML, set the field ID of the div and get it with jquery. But I think you are looking for first solution. – Danielzt Jan 05 '17 at 20:21
  • @Danielzt Well, I think that is the solution that I need, I'll try to implement it. Thank you. – Pavlo Zakharuk Jan 05 '17 at 20:43

1 Answers1

0

You can simply create a new php script. Lets say delete.php

The content of the script may be:

session_start()
// Do your session checks and terminate script if user is not logged in....

// UID = lets say that uid is the primary key in your table
$delete_uid=$_GET['uid'];
$con = mysqli_connect(server,username,pass);
$con->query("delete from table_name where uid=$$delete_uid")

Just add a link to the script in your notepad passig uid of the note as Get parameter, for example:

<a href="delete.php?uid=212">Delete</a>

Alternatively you can also make an ajax call to the script. See https://www.tutorialspoint.com/ajax/what_is_xmlhttprequest.htm

You can read more about this here:

Community
  • 1
  • 1
Gautam Krishna R
  • 2,388
  • 19
  • 26