-1

I'm trying to save data from a multi-page form into a database. I followed this tutorial but the connection always fails. I had to change to function from mysql_connect to mysqli_connect as I am running PHP7, so this could be part of the issue. Here is the code:

<?php
  session_start();
  if (isset($_POST['state'])) {
  if (!empty($_SESSION['post'])){
  if (empty($_POST['address1'])
  || empty($_POST['city'])
  || empty($_POST['pin'])
  || empty($_POST['state'])){
  // Setting error for page 3.
  $_SESSION['error_page3'] = "Mandatory field(s) are missing, Please fill it again";
  header("location: finder-step-3.php"); // Redirecting to third page.
  } else {
  foreach ($_POST as $key => $value) {
  $_SESSION['post'][$key] = $value;
  }
  extract($_SESSION['post']); // Function to extract array.
  $connection = mysqli_connect("localhost", "root", "root");
  $db = mysqli_select_db($connection, "finder_form"); // Storing values in database.
  $query = mysqli_query($db, "insert into detail (name,email,contact,password,religion,nationality,gender,qualification,experience,address1,address2,city,pin,state) values('$name','$email','$contact','$password','$religion','$nationality','$gender','$qualification','$experience','$address1','$address2','$city','$pin','$state')", $connection);
  if ($query) {
  echo '<p><span id="success">Form Submitted successfully..!!</span></p>';
  } else {
  echo '<p><span>Form Submission Failed..!!</span></p>';
  }
  unset($_SESSION['post']); // Destroying session.
  }
  } else {
  header("location: finder-step-1.php"); // Redirecting to first page.
  }
  } else {
  header("location: finder-step-1.php"); // Redirecting to first page.
  }
  ?>

Can anyone spot where I am going wrong? Thanks in advance!

Update 1:

@Damon Swayn, I have changed it to the below but still receive the form submission failed message:

$connection = mysqli_connect("localhost", "root", "root", "finder_form");
  $query = mysqli_query($connection, "insert into detail (name,email,contact,password,religion,nationality,gender,qualification,experience,address1,address2,city,pin,state) values('$name','$email','$contact','$password','$religion','$nationality','$gender','$qualification','$experience','$address1','$address2','$city','$pin','$state')", $connection);
  if ($query) {
  echo '<p><span id="success">Form Submitted successfully..!!</span></p>';
  } else {
  echo '<p><span>Form Submission Failed..!!</span></p>';
  }

@lps, I setup the following on a test.php page in the same directory and it connected successfully:

<?php
$con = mysqli_connect('localhost', 'root', 'root') or die('Could not connect the database : Username or password incorrect');
mysqli_select_db($con, 'finder_form') or die ('No database found');
echo 'Database Connected successfully';
?>

Update 2: Solved

The changes suggested by Damon Swayn worked, I just had to remove the $connection at the end of the query. Here is the working code:

$connection = mysqli_connect("localhost", "root", "root", "finder_form");
  $query = mysqli_query($connection, "insert into detail (name,email,contact,password,religion,nationality,gender,qualification,experience,address1,address2,city,pin,state) values('$name','$email','$contact','$password','$religion','$nationality','$gender','$qualification','$experience','$address1','$address2','$city','$pin','$state')");
  if ($query) {
  echo '<p><span id="success">Form Submitted successfully..!!</span></p>';
  } else {
  echo '<p><span>Form Submission Failed..!!</span></p>';
  }
cmccarra
  • 189
  • 5
  • 13
  • well first thing to check is if you can connect to the database directly with those credentials. besides that some info on the errors you're seeing would help troubleshoot this. – lps Mar 03 '16 at 00:04
  • Hi lps, thanks for your help. I have updated my answer to show the database connects successfully on a test file within the same directory. – cmccarra Mar 03 '16 at 11:19

1 Answers1

0

mysqli_connect() can take 4 parameters, the fourth being the database name.

you are using the return value of mysqli_select_db() as the connection param for every following call, mysqli_select_db() returns a boolean true/false value, try replacing the $db param in the following calls after mysqli_select_db() with the $connection variable.

Damon Swayn
  • 1,326
  • 2
  • 16
  • 36
  • Hi Damon, thanks for your help. I have updated my question showing the above changes but I still get a failed message. – cmccarra Mar 03 '16 at 11:18