Guest User

Untitled

a guest
Nov 22nd, 2018
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. if( isset($_POST['submit']))
  2. {
  3. //user data
  4. $name = $_REQUEST['fullname'];
  5. $numbers = $_REQUEST['number'];
  6. $bedrooms = $_REQUEST['bedrooms'];
  7. $date = $_REQUEST['date'];
  8. $movingFrom = $_REQUEST['from-postcode'];
  9. $movingTo = $_REQUEST['to-postcode'];
  10. $typeOfJob = $_REQUEST['typeOfJob'];
  11. $additionalInfo= $_REQUEST['message'];
  12. $ip = $_SERVER['REMOTE_ADDR'];
  13. $id= NULL;
  14. //connection variables
  15. $host='localhost';
  16. $user='root';
  17. $pass='';
  18. $db='lookup';
  19. $chset='utf8mb4';
  20. $dns = "mysql:dbname=$db;host=$host;charset=$chset";
  21. $options = [
  22. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  23. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  24. PDO::ATTR_EMULATE_PREPARES => false,
  25. ];
  26.  
  27. try {
  28. //Try to create a new PDO object and pass the vars
  29. $connection = new PDO($dns, $user, $pass, $options);
  30.  
  31. //prepare the database for data and exectue
  32. $stmt = $connection->prepare("SELECT id, numbers, ip FROM numbers");
  33. $stmt->execute();
  34.  
  35. //Loop through table and check for numbers and ips
  36. //If present set var to true and break from loop
  37. $present = false;
  38. foreach ($stmt->fetchAll() as $k=>$v)
  39. {
  40. if($v['ip'] == $ip or $v['numbers'] == $numbers)
  41. $present = true;
  42. break;
  43. }
  44. //If data present I will be redirecting and informing user
  45. if($present)
  46. {
  47. //TODO: send to different pages
  48. echo "present";
  49. }
  50. //Else insert the data into the table
  51. else
  52. {
  53. $sql = "INSERT INTO numbers (id, numbers, ip) VALUES (NULL, '$numbers', '$ip')" ;
  54. $stmt = $connection->prepare($sql);
  55.  
  56. $stmt->bindParam(':id', $id);
  57. $stmt->bindParam(':numbers', $numbers);
  58. $stmt->bindParam(':ip', $ip);
  59. $stmt->execute();
  60. echo "Woohoo";
  61. }
  62. } catch (PDOException $e) {
  63. throw new PDOException($e->getMessage(), (int)$e->getCode());
  64. }
Add Comment
Please, Sign In to add comment