Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Add_Remove_Topics(){
- //CONECT TO MYSQL
- $username = "root";
- $password = "";
- $database = "user_test";
- $id = ($_SESSION["user_id"]);
- $connect = mysql_connect('localhost',$username,$password);
- @ mysql_select_db($database) or die( "Unable to select database");
- //GET VALUES
- $sql = "SELECT * FROM topics";
- $result = mysql_query($sql);
- //echo "<div id='add-remove-topics'>";
- //Table headlines - NOT A PHP
- 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>";
- echo "<form action='' method='POST'><table id='artopics'>
- <col width='30%'>
- <col width='70%'>
- <col width='70px'>
- <col width='70px'>
- <tr height='40px'>
- <td style='background-color: #BD4040; color: white; padding-left: 5px;'>Topic Name</td>
- <td style='background-color: #BD4040; color: white; padding-left: 5px;'>Topic Address</td>
- <td style='background-color: #BD4040;'></td>
- <td style='background-color: #BD4040;'></td>
- </tr>";
- // output data of each row
- while($row = mysql_fetch_assoc($result))
- {
- echo
- "
- <tr height='40px'>
- <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>
- <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>
- <td style='background-color: white;' align='center'><button type='submit' name='updateTopic' value='".$row['topic_id']."'>Update</button></td>
- <td style='background-color: white;' align='center'><button type='submit' name='deleteTopic' value='".$row['topic_id']."'>Delete</button></td>
- </tr>
- ";
- }
- if(isset($_POST['updateTopic'])){
- $updateID = $_POST['updateTopic'];
- $updateTopicName = $_POST['up_topic_name'][$updateID];
- $updateTopicAddress = $_POST['up_topic_address'][$updateID];
- $updateTopic = mysql_query("UPDATE topics SET topic_name='$updateTopicName', topic_address='$updateTopicAddress' WHERE topic_id='$updateID'") or die(mysql_error());
- echo "<meta http-equiv='refresh' content='0'>";
- }
- if(isset($_POST['deleteTopic'])){
- $deleteID = $_POST['deleteTopic'];
- $deleteFromTopics = mysql_query("DELETE FROM topics WHERE topic_id = $deleteID ") or die(mysql_error());
- $deleteFromUserTopic = mysql_query("DELETE FROM user_topic WHERE topic_id = $deleteID ") or die(mysql_error());
- echo "<meta http-equiv='refresh' content='0'>";
- }
- echo "</table></form>";
- echo "<br><center>To add a new topic, simply fill in the fields below, clicking insert once completed.</center>";
- echo "<form action='' method='POST'><table id='artopics'>
- <col width='30%'>
- <col width='70%'>
- <col width='140px'>
- <tr height='40px'>
- <td style='background-color: #BD4040; color: white; padding-left: 5px;'>Topic Name</td>
- <td style='background-color: #BD4040; color: white; padding-left: 5px;'>Topic Address</td>
- <td style='background-color: #BD4040;'></td>
- </tr>";
- // output data of each row
- echo
- "
- <tr height='40px'>
- <td align='center' style='font-weight: normal;background-color: white;'><input type='text' name='new_topic_name'></td>
- <td align='center' style='font-weight: normal;background-color: white;'><input type='text' name='new_topic_address'></td>
- <td align='center' style='background-color: white;'><button style='width:130px;' type='submit' name='InsertTopic'>Insert</button></td>
- </tr>
- ";
- if(isset($_POST['InsertTopic'])){
- $topicName = $_POST['new_topic_name'];
- $topicAddress = $_POST['new_topic_address'];
- $insertIntoTopics = mysql_query("INSERT INTO topics (topic_name, topic_address) VALUES ('$topicName', '$topicAddress') ") or die(mysql_error());
- $grabNewTopicId = mysql_query("SELECT MAX(topic_id) AS topic_id FROM topics") or die(mysql_error());
- while($row2 = mysql_fetch_array($grabNewTopicId)) {
- $NewTopicId = $row2['topic_id'];
- $insertIntoUserTopic = mysql_query("INSERT INTO user_topic (user_id, topic_id) VALUES ('$id', '$NewTopicId') ") or die(mysql_error());
- }
- echo "<meta http-equiv='refresh' content='0'>";
- }
- echo "</table></form>";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement