By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,369 Members | 966 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,369 IT Pros & Developers. It's quick & easy.

How to delete in database using php and javascript?

P: 4
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!

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include ("dbinfo.php");
  3. include ("add.php");
  4. ?>
  5. <HTML>
  6. <head>
  7. <title>Add test</title>
  8. <php
  9. $rnumber="0";
  10. ?>
  11. <script language="javascript">
  12. rnumber=0;
  13. function addRow(tbl,row){
  14.  
  15. <?php
  16. //row count
  17. $rnumber++;
  18.  
  19. if ($rnumber<20){
  20.  ?>
  21.  
  22. var textbox = '<center><input type="file" size="10" name=upload[]></center>';
  23.  
  24. var textbox2 ='<center><select name=toy[]><option 
  25.  
  26. value="select">Select</option><option value="doll">Doll</option><option 
  27.  
  28. value="truck">Truck</option><option value="train">Train</option><option 
  29.  
  30. value="ball">Ball</option></select></center>';
  31.  
  32. var textbox3 = '<center><select name=color[]><option 
  33.  
  34. value="select">Select</option><option value="blue">Blue</option><option 
  35.  
  36. value="red">Red</option><option value="yellow">Yellow</option><option 
  37.  
  38. value="Green">Green</option></select></center>';
  39.  
  40. var textbox4 = '<center><input type="text" size = "5" maxlength= "10" name=weight[]
  41.  
  42. ></center>';
  43. var textbox5 = '<center><input type="text" size = "5" maxlength= "10" name=price[]
  44.  
  45. ></center>';
  46. var textbox6 = '<center><input type="checkbox" name=accept[]></center>';
  47.  
  48. var stop = '<input type="button" value="delete" onclick="deleteRow(this)" >';
  49.  
  50. var tbl = document.getElementById(tbl);
  51. var rowIndex = document.getElementById(row).value;
  52. var newRow = tbl.insertRow(rnumber);
  53. var newCell = newRow.insertCell(0);
  54. newCell.innerHTML = textbox;
  55. var newCell = newRow.insertCell(1);
  56. newCell.innerHTML = textbox2;
  57. var newCell = newRow.insertCell(2);
  58. newCell.innerHTML = textbox3;
  59. var newCell = newRow.insertCell(3);
  60. newCell.innerHTML = textbox4;
  61. var newCell = newRow.insertCell(4);
  62. newCell.innerHTML = textbox5;
  63. var newCell = newRow.insertCell(5);
  64. newCell.innerHTML = textbox6;
  65. var newCell = newRow.insertCell(6);
  66. newCell.innerHTML = stop;
  67.  <?php
  68. }
  69. ?>
  70.  
  71. }
  72.     function deleteRow(r)
  73.     {
  74.     var i=r.parentNode.parentNode.rowIndex;
  75.     document.getElementById('TableMain').deleteRow(i);
  76.  
  77. <?php
  78.     $rnumber--;
  79. ?>
  80.     }
  81.  
  82. </script>
  83. </head>
  84. <body>
  85.  
  86. <table width="75%" >
  87. <tr>
  88. <td width = 100%>
  89.  
  90. <form action="<?php echo $_SERVER['PHP_SELF'];?>"name="toyentry" method="post"> 
  91. <table width="100%" border="0" cellspacing="0" cellpadding="2" id="TableMain">
  92. <th>Upload Picture</th>
  93. <th><center>Toy</th>
  94. <th>Color</th>
  95. <th>Weight</th>
  96. <th>Price</th>
  97. <th>Accept</th>
  98.  
  99. $id=$_SESSION['id'];
  100. $id=htmlspecialchars($id);
  101. $id_sq=mysql_real_escape_string($id);
  102.  
  103. <?php
  104. $sql="SELECT * FROM table WHERE id='$id_sq'";
  105. $result = mysql_query($sql);
  106.  
  107. while ($row = mysql_fetch_assoc($result))
  108. {
  109. $toydb=$row["toy"];
  110. $colordb=$row["color"];
  111. $weightdb=$row["Weight"];
  112. $picdb=$row["ImagePath"];
  113. $pricedb=$row["Price"];
  114.  
  115. echo "<tr><td>";
  116. echo "<center><img src=\"$picdb_sq\" height=\"75\" width=\"100\"></center>";
  117. echo "</td><td>";
  118. echo "<center>";
  119. echo $toydb_sq;
  120. echo "</center>";
  121. echo "</td><td>";
  122. echo "<center>";
  123. echo $colordb_sq;
  124. echo "</center>";
  125. echo "</td><td>";
  126. echo "<center>";
  127. echo $weightdb_sq;
  128. echo "</center>";
  129. echo "</td><td>";
  130. echo "<center>$";
  131. echo $pricedb_sq;
  132. echo "</center>";
  133. echo "</td><td>";
  134. echo "<center><input type=\"checkbox\"></center>";
  135. echo "</td><td>";
  136. echo "<center><input type=\"checkbox\"></center>";
  137. echo "</td><td>";
  138. echo "<input type=\"button\" value=\"Delete\" onclick=\"deleteRow(this)\" >";
  139. echo "</td></tr>";
  140. }
  141. ?>
  142.  
  143. <tr id="row1">
  144. </tr>
  145. </table>
  146. </td>
  147. <td valign="top" width = 20%>
  148. <input type="button" name="Button" value="Add more" onClick="addRow
  149.  
  150. ('TableMain','row1')"></td>
  151. </tr>
  152. </table>
  153. <table>
  154. <tbody>
  155. <tr><td>
  156. <input type="submit" class="button" name="submit" value="Submit">
  157. </td></tr>
  158. </tbody>
  159. </table>
  160. </form>
  161. </body>
  162. </html>
  163.  
Here is add.php:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. if(isset($_POST['submit']))
  4. {
  5.  
  6. include ("dbinfo.php");
  7.  
  8. $weight = $_POST["weight"];
  9.  
  10. $cnt=count($weight);
  11.  
  12. for ($counter=0; $counter < $cnt; $counter++)
  13. {
  14. $weight = $_POST["weight"][$counter];
  15. $file = $_POST["upload"][$counter];
  16. $price = $_POST["price"][$counter];
  17. $toy = $_POST["toy"][$counter];
  18. $color = $_POST["color"][$counter];
  19.  
  20. mysql_query("INSERT INTO table (Weight, File, Price, Toy, Color) VALUES('$weight', '$file', '$price', '$toy', '$color')") or die(mysql_error());  
  21. }
  22.  
  23. }
  24. ?>
  25.  
Oct 28 '11 #1

✓ answered by zorgi

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

http://dev.mysql.com/doc/refman/5.0/en/delete.html

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.

Share this Question
Share on Google+
4 Replies


zorgi
Expert 100+
P: 431
I didn't look through your code but to delete rows from the database you should use MySql DELETE statement.

http://dev.mysql.com/doc/refman/5.0/en/delete.html

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.
Oct 29 '11 #2

P: 4
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 :)
Oct 29 '11 #3

zorgi
Expert 100+
P: 431
  • jQuery is javascript library
  • Ajax is a way of programming

With jQuery you can preforme AJAX request.

http://api.jquery.com/jQuery.ajax/
Oct 29 '11 #4

P: 4
I see. Thanks so much :)
Oct 29 '11 #5

Post your reply

Sign in to post your reply or Sign up for a free account.