How to delete in database using php and javascript?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shinypenny
    New Member
    • Oct 2011
    • 4

    How to delete in database using php and javascript?

    Hello all,

    I am trying to use PHP to pull information from a mySQL database, as well as offer an "Add More" button through the use of JavaScript. I'm able to put the new information into the database and I am able to pull the information out of the database to show. However, I am unable to figure out how to delete the information that's pulled from the database when I hit the delete button beside it (line 138 below). Any thoughts or ideas?

    Thank you in advance!

    Code:
    <?php
    include ("dbinfo.php");
    include ("add.php");
    ?>
    <HTML>
    <head>
    <title>Add test</title>
    <php
    $rnumber="0";
    ?>
    <script language="javascript">
    rnumber=0;
    function addRow(tbl,row){
    
    <?php
    //row count
    $rnumber++;
     
    if ($rnumber<20){
     ?>
     
    var textbox = '<center><input type="file" size="10" name=upload[]></center>';
    
    var textbox2 ='<center><select name=toy[]><option 
    
    value="select">Select</option><option value="doll">Doll</option><option 
    
    value="truck">Truck</option><option value="train">Train</option><option 
    
    value="ball">Ball</option></select></center>';
    
    var textbox3 = '<center><select name=color[]><option 
    
    value="select">Select</option><option value="blue">Blue</option><option 
    
    value="red">Red</option><option value="yellow">Yellow</option><option 
    
    value="Green">Green</option></select></center>';
    
    var textbox4 = '<center><input type="text" size = "5" maxlength= "10" name=weight[]
    
    ></center>';
    var textbox5 = '<center><input type="text" size = "5" maxlength= "10" name=price[]
    
    ></center>';
    var textbox6 = '<center><input type="checkbox" name=accept[]></center>';
    
    var stop = '<input type="button" value="delete" onclick="deleteRow(this)" >';
     
    var tbl = document.getElementById(tbl);
    var rowIndex = document.getElementById(row).value;
    var newRow = tbl.insertRow(rnumber);
    var newCell = newRow.insertCell(0);
    newCell.innerHTML = textbox;
    var newCell = newRow.insertCell(1);
    newCell.innerHTML = textbox2;
    var newCell = newRow.insertCell(2);
    newCell.innerHTML = textbox3;
    var newCell = newRow.insertCell(3);
    newCell.innerHTML = textbox4;
    var newCell = newRow.insertCell(4);
    newCell.innerHTML = textbox5;
    var newCell = newRow.insertCell(5);
    newCell.innerHTML = textbox6;
    var newCell = newRow.insertCell(6);
    newCell.innerHTML = stop;
     <?php
    }
    ?>
     
    }
        function deleteRow(r)
        {
        var i=r.parentNode.parentNode.rowIndex;
        document.getElementById('TableMain').deleteRow(i);
         
    <?php
        $rnumber--;
    ?>
        }
     
    </script>
    </head>
    <body>
    
    <table width="75%" >
    <tr>
    <td width = 100%>
    
    <form action="<?php echo $_SERVER['PHP_SELF'];?>"name="toyentry" method="post"> 
    <table width="100%" border="0" cellspacing="0" cellpadding="2" id="TableMain">
    <th>Upload Picture</th>
    <th><center>Toy</th>
    <th>Color</th>
    <th>Weight</th>
    <th>Price</th>
    <th>Accept</th>
    
    $id=$_SESSION['id'];
    $id=htmlspecialchars($id);
    $id_sq=mysql_real_escape_string($id);
    
    <?php
    $sql="SELECT * FROM table WHERE id='$id_sq'";
    $result = mysql_query($sql);
    
    while ($row = mysql_fetch_assoc($result))
    {
    $toydb=$row["toy"];
    $colordb=$row["color"];
    $weightdb=$row["Weight"];
    $picdb=$row["ImagePath"];
    $pricedb=$row["Price"];
    
    echo "<tr><td>";
    echo "<center><img src=\"$picdb_sq\" height=\"75\" width=\"100\"></center>";
    echo "</td><td>";
    echo "<center>";
    echo $toydb_sq;
    echo "</center>";
    echo "</td><td>";
    echo "<center>";
    echo $colordb_sq;
    echo "</center>";
    echo "</td><td>";
    echo "<center>";
    echo $weightdb_sq;
    echo "</center>";
    echo "</td><td>";
    echo "<center>$";
    echo $pricedb_sq;
    echo "</center>";
    echo "</td><td>";
    echo "<center><input type=\"checkbox\"></center>";
    echo "</td><td>";
    echo "<center><input type=\"checkbox\"></center>";
    echo "</td><td>";
    echo "<input type=\"button\" value=\"Delete\" onclick=\"deleteRow(this)\" >";
    echo "</td></tr>";
    }
    ?>
    
    <tr id="row1">
    </tr>
    </table>
    </td>
    <td valign="top" width = 20%>
    <input type="button" name="Button" value="Add more" onClick="addRow
    
    ('TableMain','row1')"></td>
    </tr>
    </table>
    <table>
    <tbody>
    <tr><td>
    <input type="submit" class="button" name="submit" value="Submit">
    </td></tr>
    </tbody>
    </table>
    </form>
    </body>
    </html>
    Here is add.php:
    Code:
    <?php
    
    if(isset($_POST['submit']))
    {
    
    include ("dbinfo.php");
    
    $weight = $_POST["weight"];
    
    $cnt=count($weight);
    
    for ($counter=0; $counter < $cnt; $counter++)
    {
    $weight = $_POST["weight"][$counter];
    $file = $_POST["upload"][$counter];
    $price = $_POST["price"][$counter];
    $toy = $_POST["toy"][$counter];
    $color = $_POST["color"][$counter];
    
    mysql_query("INSERT INTO table (Weight, File, Price, Toy, Color) VALUES('$weight', '$file', '$price', '$toy', '$color')") or die(mysql_error());  
    }
    
    }
    ?>
  • zorgi
    Recognized Expert Contributor
    • Mar 2008
    • 431

    #2
    I didn't look through your code but to delete rows from the database you should use MySql DELETE statement.



    I am not sure why do you want to use javascript and php to do this but if you really have to... than use ajax.

    Comment

    • shinypenny
      New Member
      • Oct 2011
      • 4

      #3
      Well, I have to use JavaScript to dynamically add the rows and php to talk to the database. Is Ajax similar to jQuery? Thanks for your response :)

      Comment

      • zorgi
        Recognized Expert Contributor
        • Mar 2008
        • 431

        #4
        • jQuery is javascript library
        • Ajax is a way of programming


        With jQuery you can preforme AJAX request.

        Comment

        • shinypenny
          New Member
          • Oct 2011
          • 4

          #5
          I see. Thanks so much :)

          Comment

          Working...