Advertisement
Guest User

new.php issue

a guest
Nov 29th, 2013
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. <?php
  2. error_reporting(-1);
  3. ini_set('display_errors', 'On');
  4. ?>
  5.  
  6. <?php
  7. session_start();
  8. if(empty($_SESSION['loggedin']))
  9. {
  10. header('Location: http://' . $_SERVER['HTTP_HOST'] . '/sites/sgr/admin/login.php');
  11. exit;
  12. }
  13.  
  14. echo 'You will only see this if you are logged in.';
  15.  
  16. ?>
  17.  
  18.  
  19. <?php
  20. /*
  21. NEW.PHP
  22. Allows user to create a new entry in the database
  23. */
  24.  
  25. // creates the new record form
  26. // since this form is used multiple times in this file, I have made it a function that is easily reusable
  27. function renderForm($id, $ref, $role, $division, $location, $salary, $description, $addedby,
  28. $active, $error)
  29. {
  30. ?>
  31.  
  32. <?php
  33.  
  34. include ( 'includes/header.php' );
  35.  
  36. ?>
  37. <title>Admin Add Job Page</title>
  38. </head>
  39. <body>
  40. <div id="container">
  41. <?php
  42. // if there are any errors, display them
  43. if ($error != '')
  44. {
  45. echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
  46. }
  47. ?>
  48.  
  49. <form action="" method="post">
  50. <div>
  51. <strong>Ref: *</strong> <input type="text" name="ref" value="<?php echo $ref; ?>" />
  52. <br/><br>
  53. <strong>Role: *</strong> <input type="text" name="role" value="<?php echo $role; ?>" />
  54. <br/><br>
  55. <strong>Division: *</strong> <input type="text" name="division" value="<?php echo $division; ?>" />
  56. <br /><br>
  57. <strong>Location: *</strong> <input type="text" name="location" value="<?php echo $location; ?>" />
  58. <br /><br>
  59. <strong>Salary: *</strong> <input type="text" name="salary" value="<?php echo $salary; ?>" />
  60. <br /><br>
  61. <strong>Description: *</strong> <textarea name="description" value="<?php echo $description; ?>" cols="30" rows="6" /></textarea>
  62. <br /><br>
  63. <strong>Added By: *</strong> <input type="text" name="addedby" value="<?php echo $addedby; ?>" />
  64. <br>
  65. <label style="color: #FFFFFF;"><input type="radio" name="active" value="1" <?php if($active == 1) echo 'checked="checked"'; ?> > Active</label>
  66. <br>
  67. <label style="color: #FFFFFF;"><input type="radio" name="active" value="0" <?php if($active == 0) echo 'checked="checked"'; ?> > Inactive</label>
  68. <br>
  69. <input type="submit" name="submit" value="Submit">
  70. </div>
  71. </form>
  72. </div>
  73. </body>
  74. </html>
  75. <?php
  76. }
  77.  
  78. // connect to the database
  79. //include('connect-db.php');
  80. $con = mysqli_connect("","","","");
  81.  
  82. // check if the form has been submitted. If it has, start to process the form and save it to the database
  83. if (isset($_POST['submit']))
  84. {
  85. // get form data, making sure it is valid
  86. $id = $_POST['id'];
  87. $ref = mysql_real_escape_string(htmlspecialchars($_POST['ref']));
  88. $role = mysql_real_escape_string(htmlspecialchars($_POST['role']));
  89. $division = mysql_real_escape_string(htmlspecialchars($_POST['division']));
  90. $location = mysql_real_escape_string(htmlspecialchars($_POST['location']));
  91. $salary = mysql_real_escape_string(htmlspecialchars($_POST['salary']));
  92. $description = mysql_real_escape_string(htmlspecialchars($_POST['description']));
  93. $addedby = mysql_real_escape_string(htmlspecialchars($_POST['addedby']));
  94. $active = (int)$_POST['active'];
  95.  
  96. // check to make sure all fields are entered
  97. if ($ref == '' || $role == '' || $division = '' || $location = '' || $salary = '' ||
  98. $description = '' || $addedby = '' || $active = '' )
  99. {
  100. // generate error message
  101. $error = 'ERROR: Please fill in all required fields!';
  102.  
  103. // if either field is blank, display the form again
  104. renderForm($id, $ref, $role, $division, $location, $salary, $description, $addedby, $active, $error);
  105. }
  106. else
  107. {
  108. // save the data to the database
  109. mysqli_query($con, "INSERT INTO jobs (ref,role,division,location,salary,description,addedby,active) VALUES('$ref','$role','$division','$location','$salary','$description','$addedby','$active')")
  110. or die(mysql_error());
  111.  
  112. // once saved, redirect back to the view page
  113. header("Location: view.php");
  114. }
  115. }
  116. else
  117. // if the form hasn't been submitted, display the form
  118. {
  119. renderForm('','','','','','','','','','');
  120. }
  121. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement