Nikita

Nikita

  • NA
  • 56
  • 9.3k

inserting multiple values to database

Jan 2 2020 5:00 AM
Warning: mysqli_real_escape_string() expects parameter 2 to be string, array given in D:\xampp\htdocs\punegurukulfinal\registration.php on line 16
 
HTML CODE
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta name="viewport" content="width=device-width, initial-scale=1" />  
  5. <style>  
  6. body {  
  7. font-family: Arial, Helvetica, sans-serif;  
  8. background-color: black;  
  9. }  
  10. * {  
  11. box-sizing: border-box;  
  12. }  
  13. /* Add padding to containers */  
  14. .container {  
  15. padding: 16px;  
  16. background-color: white;  
  17. }  
  18. /* Full-width input fields */  
  19. input[type="text"],  
  20. input[type="text"] {  
  21. width: 100%;  
  22. padding: 15px;  
  23. margin: 5px 0 22px 0;  
  24. display: inline-block;  
  25. border: none;  
  26. background: #f1f1f1;  
  27. }  
  28. input[type="text"]:focus,  
  29. input[type="text"]:focus {  
  30. background-color: #ddd;  
  31. outline: none;  
  32. }  
  33. /* Overwrite default styles of hr */  
  34. hr {  
  35. border: 1px solid #f1f1f1;  
  36. margin-bottom: 25px;  
  37. }  
  38.   
  39. /* Set a style for the submit button */  
  40. .registerbtn {  
  41. background-color: #4caf50;  
  42. color: white;  
  43. padding: 16px 20px;  
  44. margin: 8px 0;  
  45. border: none;  
  46. cursor: pointer;  
  47. width: 100%;  
  48. opacity: 0.9;  
  49. }  
  50. .registerbtn:hover {  
  51. opacity: 1;  
  52. }  
  53. /* Add a blue text color to links */  
  54. a {  
  55. color: dodgerblue;  
  56. }  
  57. /* Set a grey background color and center the text of the "sign in" section */  
  58. .signin {  
  59. background-color: #f1f1f1;  
  60. text-align: center;  
  61. }  
  62. </style>  
  63. </head>  
  64. <body>  
  65. <form action="registration.php">  
  66. <div class="container">  
  67. <div class="row">  
  68. <h1>Register</h1>  
  69. <p>Please fill in this form to create an account.</p>  
  70. <hr />  
  71. <label for="fname"><b>First Name</b></label>  
  72. <input  
  73. type="text"  
  74. placeholder="Enter First Name"  
  75. name="First_Name"  
  76. required/>  
  77. <label for="lname"><b>Last Name</b></label>  
  78. <input  
  79. type="text"  
  80. placeholder="Enter Password"  
  81. name="Last_Name"  
  82. required/>  
  83. <label for="Mobile"><b>Mobile</b></label>  
  84. <input  
  85. type="text"  
  86. placeholder="Enter Mobile"  
  87. name="mobile"  
  88. required/>  
  89. <label for="email"><b>Email</b></label>  
  90. <input type="text" placeholder="Enter Email" name="email" required/>  
  91. <label for="training"><b>Training</b></label><br />  
  92. <input type="checkbox" name="Training[]" value="it" /> IT and Yoga  
  93. Training<br />  
  94. <input  
  95. type="checkbox"  
  96. name="Training[]"  
  97. value="technology"  
  98. />Technology Training<br />  
  99. <input  
  100. type="checkbox"  
  101. name="Training[]"  
  102. value="Meditation"  
  103. checked/>Meditation  
  104. <p>  
  105. By creating an account you agree to our  
  106. <a href="#">Terms & Privacy</a>.  
  107. </p>  
  108. <button type="submit" class="registerbtn" name="submit1">  
  109. Register  
  110. </button>  
  111. </div>  
  112. <div class="container signin">  
  113. <p>Already have an account? <a href="#">Sign in</a>.</p>  
  114. </div>  
  115. </div>  
  116. </form>  
  117. </body>  
  118. </html>  
PHP CODE
  1. <?php  
  2. /* Attempt MySQL server connection. Assuming you are running MySQL 
  3. server with default setting (user 'root' with no password) */  
  4. $link = mysqli_connect("localhost""root""""punegurukul");  
  5. // Check connection  
  6. if($link === false){  
  7. die("ERROR: Could not connect. " . mysqli_connect_error());  
  8. }  
  9. // Escape user inputs for security  
  10. $First_Name = mysqli_real_escape_string($link$_REQUEST['First_Name']);  
  11. $Last_Name = mysqli_real_escape_string($link$_REQUEST['Last_Name']);  
  12. $mobile = mysqli_real_escape_string($link$_REQUEST['mobile']);  
  13. $email = mysqli_real_escape_string($link$_REQUEST['email']);  
  14. $Training = mysqli_real_escape_string($link$_REQUEST['Training']);  
  15. //code for checkbox values  
  16. if(isset($_POST["submit1"]))  
  17. {  
  18. if(!emptyempty($_POST["Training"]))  
  19. {  
  20. echo '<h3>Please select atlest one training';  
  21. foreach($_POST["Training"as $Training)  
  22. {  
  23. echo '<p>' .$Training'</p>';  
  24. }  
  25. }  
  26. else  
  27. {  
  28. echo "please select atleast one training";  
  29. }  
  30. }  
  31. // Attempt insert query execution  
  32. $sql = "INSERT INTO studentreg (First_Name, Last_Name, Mobile, Email, Training) VALUES ('$First_Name', '$Last_Name', '$mobile','$email','$Training')";  
  33. if(mysqli_query($link$sql)){  
  34. echo "Records added successfully.";  
  35. else{  
  36. echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);  
  37. }  
  38. // Close connection  
  39. mysqli_close($link);  
  40. ?>  
PLEASE solve my problem.
 
I want to insert checkbox multiple values in database.

Answers (1)