How to remove table rows from database?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fikaqqq
    New Member
    • Sep 2012
    • 4

    #1

    How to remove table rows from database?

    I need to be able to delete table rows from database via link which is shown down below, but i don't know how to form javascript function which will enable that.

    So, this is how my index.php looks like:

    Code:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" media="screen" />
    <title>Ajax</title>
    
    
    
    <script>
    
    
    
    function dodajNovogAutora()
    {
    alert("Dodali ste novog autora!");
    	 var ime = document.getElementById("inputName").value;
    	  var prezime = document.getElementById("inputPrezime").value;
    	  var adresa = document.getElementById("inputAdresa").value;
    	 alert("add.php?ime=" + ime + "&prezime=" + prezime + "&adresa=" + adresa);
    var xmlhttp;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
    
    
    	  var rowCount = document.getElementById("tablica").getElementsByTagName("TR").length;
    	 
    	  var table=document.getElementById("tablica");
    	  var row=table.insertRow(rowCount);
    	  var cell1=row.insertCell(0);
    	  var cell2=row.insertCell(1);
    	  var cell3=row.insertCell(2);
    	  var cell4=row.insertCell(3);
    	  var cell5=row.insertCell(4);
    	  var cell6=row.insertCell(5);
    	  var sifra= xmlhttp.responseText;
    	  cell1.innerHTML=sifra;
    	  cell2.innerHTML=ime;
    	  cell3.innerHTML=prezime;
    	  cell4.innerHTML=adresa;
    	  cell5.innerHTML="<a href=\"#\" onclick=\"updateauthor()(" +sifra + ");return false;\">Edit</a>";
    	  cell6.innerHTML="<a href=\"#\" onclick=\"deleteauthor()(" +sifra + ");return false;\">Briši</a>";
    	   
        }
      }
    
      
    xmlhttp.open("GET","add.php?ime=" + ime + "&prezime=" + prezime + "&adresa=" + adresa,true);
    xmlhttp.send();
    }
    //potrebno je napraviti funkcije update i obrisi koje rade kao i dodajNovogAutora
    	</script>
    
    	
    		
    			
    
    </head>
    
    <body>
    <h3>Popis autora</h3>
    <table border="1" id="tablica">
    
    <thead>
    	<tr>
    		<td>Sifra</td>
    		<td>Ime</td>
    		<td>Prezime</td>
    		<td>Adresa</td>
    		<td></td>
    		<td></td>
    	</tr>
    </thead>
    <tbody>
    
    <?php	
    	include 'connect-db.php';
    	//dohvacanje rezultata pretrazivanja
    	$rezultati = mysql_query("select * from Autor");
    	//Iteracija kroz rezultate 
    	while($red = mysql_fetch_array($rezultati))
    	{
    		//ispis rezultata pretrazivanja
    		echo "<tr>";
    		echo " <td>" . $red['sifra'] . "</td>";
    		echo " <td>" . $red['ime'] . "</td>";
    		echo " <td>" . $red['prezime'] . "</td>";
    		echo " <td>" . $red['adresa'] . "</td>";
    		
    
    		echo " <td><a href=\"#\" onclick=\"updateauthor()(". $red['sifra'] . ");return false;\">Edit</a></td>";
    		echo " <td><a href=\"#\" onclick=\"deleteauthor()(". $red['sifra'] . ");return false;\">Delete</a></td>";
    	
    		echo "</tr>";
    	}
    	mysql_close($veza);
    ?>
    
    	
    
    
    	
    </tbody>
    </table>
    
    
    <h3>Dodaj novog autora</h3>
    <form action="add.php" method="GET" id="forma">
    	Ime: <input type="text"  name="inputName" id="inputName" value="" /> <br />
    	Prezime: <input type="text" name="inputPrezime" id="inputPrezime" value="" /><br />
    	Adresa: <input type="text" name="inputAdresa" id="inputAdresa" value="" />
    	
    	<br />
    	<input type="button" onclick="dodajNovogAutora(); return false;" value="Dodaj" />
    </form>
    
    </body>
    </html>
    
    
    And this is how my delete.php looks like:
    
    <?php
    	//Uspostava veze prema bazi
    	$veza = mysql_connect("localhost","root","");
    	//odabir baze na kojoj zelimo raditi
    	mysql_select_db("seminar", $veza);
    	
    	mysql_query("DELETE FROM Autor WHERE sifra = $_GET[sifra]") or die(mysql_error());
    
    
    ?>
    So could anyone help me, and form a javascript function which will enable deleting rows by clicking 'Delete' link?
    Know it's a few lines of code, but I just can't do it.
    Last edited by zmbd; Sep 24 '12, 08:07 PM. Reason: When posting code, SQL, HTML, etc... please use the <CODE/> formating button.
  • iam_clint
    Recognized Expert Top Contributor
    • Jul 2006
    • 1207

    #2
    well you would do the same thing you have done using "ajax" but sending a different query string and parsing it with your php to delete rows in the database?

    Comment

    • fikaqqq
      New Member
      • Sep 2012
      • 4

      #3
      Don't understand what you're trying to say cause its a bit new to me.
      All i need is to make 2 more functions(for delete table row in database, and edit), cause i've got add new function already (don't know why i pasted it cause it's actually not needed).

      Comment

      • fikaqqq
        New Member
        • Sep 2012
        • 4

        #4
        Delete function should refer to this row

        Code:
        echo " <td><a href=\"#\" onclick=\"deleteauthor()(". $red['sifra'] . ");return false;\">Delete</a></td>";
        Last edited by zmbd; Sep 24 '12, 10:07 PM. Reason: When posting any type of code, please use the <CODE/> format button.

        Comment

        • iam_clint
          Recognized Expert Top Contributor
          • Jul 2006
          • 1207

          #5
          I can't give you the exact code i don't have enough information but you need to make another ajax request to your server in the function

          From previous post
          Code:
           xmlhttp.open("GET","add.php?ime=" + ime + "&prezime=" + prezime + "&adresa=" + adresa,true);
          xmlhttp.send();
          in add.php or even make a dele.php if you feel the need to and make it remove the records based on the criteria passed.

          Comment

          • fikaqqq
            New Member
            • Sep 2012
            • 4

            #6
            Since i'm actually new to this, don't wonder if i'm asking 'well-known or stupid questions', but does that mean that i should make a new function and copy these two lines that u quoted and add delete.php instead of add.php in new function?

            I've already got delete.php that contains criteria which should allow deleting records from database, just don't know how to form that function and write the code in index.php :[

            Comment

            • Anivia
              New Member
              • Sep 2012
              • 1

              #7
              Well hello mate... I think you should be able to do this with your knowledge, as you are very intelligent and all that. It is of utmost importance that you do that. Thanks!

              Comment

              Working...