Guest User

Untitled

a guest
Aug 13th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. Advanced writing to file
  2. <?php
  3. $myFile = "questions.htm";
  4. $fh = fopen($myFile, 'a') or die("can't open file");
  5. $stringData = $_POST ['title'];
  6. $stringData = $_POST ['description'];
  7.  
  8. fwrite($fh, $stringData);
  9. fclose($fh);
  10. ?>
  11.  
  12. <?php
  13. $hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company, if you are with webune leave as localhost
  14. $db_user = "root"; // change to your database password
  15. $db_password = ""; // change to your database password
  16. $database = "search"; // provide your database name
  17. $db_table = "searchengine"; // leave this as is
  18.  
  19. error_reporting (E_ALL ^ E_NOTICE);
  20.  
  21. # STOP HERE
  22. ####################################################################
  23. # THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE
  24. $db = mysql_connect($hostname, $db_user, $db_password);
  25. mysql_select_db($database,$db);
  26. ?>
  27.  
  28. <html>
  29. <head>
  30. <title>Add a Question and Answer to the Database</title>
  31. <style type="text/css">
  32. .style1 {
  33. font-family: Calibri;
  34. font-weight: normal;
  35. font-size: medium;
  36. }
  37. .style2 {
  38. color: #808080;
  39. }
  40. .style3 {
  41. text-align: center;
  42. }
  43. .style4 {
  44. border-width: 0px;
  45. }
  46. </style>
  47. </head>
  48. <body>
  49.  
  50. <?php
  51. if (isset($_REQUEST['Submit'])) {
  52. # THIS CODE TELL MYSQL TO INSERT THE DATA FROM THE FORM INTO YOUR MYSQL TABLE
  53. $sql = "INSERT INTO $db_table(title,description,url,keywords) values ('".mysql_real_escape_string(stripslashes($_REQUEST['title']))."','".mysql_real_escape_string(stripslashes($_REQUEST['description']))."','".mysql_real_escape_string(stripslashes($_REQUEST['url']))."','".mysql_real_escape_string(stripslashes($_REQUEST['keywords']))."')";
  54. if($result = mysql_query($sql ,$db)) {
  55. echo '<h1>Thank you</h1>Your information has been entered into our database<br><br>';
  56. } else {
  57. echo "ERROR: ".mysql_error();
  58. }
  59. } else {
  60. ?>
  61.  
  62. <h1 class="style3"><a href="index.php">
  63. <img src='ChamberLogo.png' class="style4"></a> </h1>
  64. <h1 class="style1">Add A Question to the FAQ
  65. Database</h1>
  66. <hr>
  67.  
  68. <form method="post" action="">
  69. Question:<br>
  70. &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input type="text" name="title" style="width: 486px">
  71. <br>
  72. &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp Answer: <br>
  73. &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input type="text" name="description" style="height: 24px; width: 352px">
  74. <br>
  75. Keywords <span class="style2">(Type Question Again):</span><br>
  76. &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input type="text" name="keywords" style="height: 24px; width: 486px">
  77. <br><br>
  78. &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input type="submit" name="Submit" value="Submit">
  79. </form>
  80.  
  81. <?php
  82. }
  83. ?>
  84. </body>
  85. </html>
  86.  
  87. <?php
  88. $myFile = "questions.htm";
  89. $fh = fopen($myFile, 'a') or die("can't open file");
  90. $stringData = $_POST ['title'];
  91. fwrite($fh, $stringData);
  92. fclose($fh);
  93. ?>
  94.  
  95. $myFile = "questions.htm";
  96. $fh = fopen($myFile, 'a') or die("can't open file");
  97. $stringData = $_POST .'<br><p class="style1"><a id="Question33">"title"</p></a>'.
  98. $stringData = $_POST .'<br><p class="style2">"description"</p>';
  99. fwrite($fh, $stringData);
  100. fclose($fh);
  101.  
  102. //Read the entire file into the $file_contents variable
  103. $file_contents = file_get_contents("questions.htm");
  104.  
  105. //Remove the closing body and html tags
  106. $file_contents = substr($file_contents, 0, strlen($file_contents) - strlen("</body></html>"));
  107.  
  108. //Add in whatever other data you want
  109. $file_contents .= "whatever you want to append goes here";
  110.  
  111. //Add your closing body and html tags back again
  112. $file_contents .= "</body></html>";
  113.  
  114. //Overwrite the original questions.htm file with the new contents
  115. $fh = fopen("questions.htm", "w");
  116. fwrite($fh, $file_contents);
  117. fclose($fh);
Add Comment
Please, Sign In to add comment