Advertisement
Guest User

Untitled

a guest
Feb 21st, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. function Add_Remove_Topics(){
  2. //CONECT TO MYSQL
  3. $username = "root";
  4. $password = "";
  5. $database = "user_test";
  6. $id = ($_SESSION["user_id"]);
  7.  
  8. $connect = mysql_connect('localhost',$username,$password);
  9. @ mysql_select_db($database) or die( "Unable to select database");
  10.  
  11. //GET VALUES
  12. $sql = "SELECT * FROM topics";
  13. $result = mysql_query($sql);
  14.  
  15. //echo "<div id='add-remove-topics'>";
  16. //Table headlines - NOT A PHP
  17.  
  18. echo "<center>Simply type into the fields to change your current topic information - click update once done editing. If you no longer want one of your topics, click the delete button.</center>";
  19.  
  20. echo "<form action='' method='POST'><table id='artopics'>
  21. <col width='30%'>
  22. <col width='70%'>
  23. <col width='70px'>
  24. <col width='70px'>
  25. <tr height='40px'>
  26. <td style='background-color: #BD4040; color: white; padding-left: 5px;'>Topic Name</td>
  27. <td style='background-color: #BD4040; color: white; padding-left: 5px;'>Topic Address</td>
  28. <td style='background-color: #BD4040;'></td>
  29. <td style='background-color: #BD4040;'></td>
  30. </tr>";
  31.  
  32. // output data of each row
  33.  
  34. while($row = mysql_fetch_assoc($result))
  35. {
  36.  
  37.  
  38. echo
  39. "
  40. <tr height='40px'>
  41. <td align='center' style='font-weight: normal;background-color: white;'><input type='text' name='up_topic_name[{$row['topic_id']}]' value='" . $row['topic_name'] . "'></td>
  42. <td align='center' style='font-weight: normal;background-color: white;'><input type='text' name='up_topic_address[{$row['topic_id']}]' value='" . $row['topic_address'] . "'></td>
  43. <td style='background-color: white;' align='center'><button type='submit' name='updateTopic' value='".$row['topic_id']."'>Update</button></td>
  44. <td style='background-color: white;' align='center'><button type='submit' name='deleteTopic' value='".$row['topic_id']."'>Delete</button></td>
  45. </tr>
  46.  
  47. ";
  48.  
  49.  
  50.  
  51. }
  52.  
  53. if(isset($_POST['updateTopic'])){
  54. $updateID = $_POST['updateTopic'];
  55. $updateTopicName = $_POST['up_topic_name'][$updateID];
  56. $updateTopicAddress = $_POST['up_topic_address'][$updateID];
  57. $updateTopic = mysql_query("UPDATE topics SET topic_name='$updateTopicName', topic_address='$updateTopicAddress' WHERE topic_id='$updateID'") or die(mysql_error());
  58. echo "<meta http-equiv='refresh' content='0'>";
  59. }
  60.  
  61.  
  62. if(isset($_POST['deleteTopic'])){
  63. $deleteID = $_POST['deleteTopic'];
  64. $deleteFromTopics = mysql_query("DELETE FROM topics WHERE topic_id = $deleteID ") or die(mysql_error());
  65. $deleteFromUserTopic = mysql_query("DELETE FROM user_topic WHERE topic_id = $deleteID ") or die(mysql_error());
  66. echo "<meta http-equiv='refresh' content='0'>";
  67. }
  68.  
  69. echo "</table></form>";
  70.  
  71.  
  72. echo "<br><center>To add a new topic, simply fill in the fields below, clicking insert once completed.</center>";
  73.  
  74. echo "<form action='' method='POST'><table id='artopics'>
  75. <col width='30%'>
  76. <col width='70%'>
  77. <col width='140px'>
  78. <tr height='40px'>
  79. <td style='background-color: #BD4040; color: white; padding-left: 5px;'>Topic Name</td>
  80. <td style='background-color: #BD4040; color: white; padding-left: 5px;'>Topic Address</td>
  81. <td style='background-color: #BD4040;'></td>
  82. </tr>";
  83.  
  84. // output data of each row
  85.  
  86.  
  87.  
  88. echo
  89. "
  90. <tr height='40px'>
  91. <td align='center' style='font-weight: normal;background-color: white;'><input type='text' name='new_topic_name'></td>
  92. <td align='center' style='font-weight: normal;background-color: white;'><input type='text' name='new_topic_address'></td>
  93. <td align='center' style='background-color: white;'><button style='width:130px;' type='submit' name='InsertTopic'>Insert</button></td>
  94. </tr>
  95.  
  96. ";
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103. if(isset($_POST['InsertTopic'])){
  104. $topicName = $_POST['new_topic_name'];
  105. $topicAddress = $_POST['new_topic_address'];
  106. $insertIntoTopics = mysql_query("INSERT INTO topics (topic_name, topic_address) VALUES ('$topicName', '$topicAddress') ") or die(mysql_error());
  107. $grabNewTopicId = mysql_query("SELECT MAX(topic_id) AS topic_id FROM topics") or die(mysql_error());
  108. while($row2 = mysql_fetch_array($grabNewTopicId)) {
  109. $NewTopicId = $row2['topic_id'];
  110. $insertIntoUserTopic = mysql_query("INSERT INTO user_topic (user_id, topic_id) VALUES ('$id', '$NewTopicId') ") or die(mysql_error());
  111. }
  112. echo "<meta http-equiv='refresh' content='0'>";
  113.  
  114. }
  115.  
  116. echo "</table></form>";
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement