0

I have a webform with basic info (Name, Last_Name, Date of Birth, Telephone, Email)

After submitting the form, and inserting the data of those fields into a MySQL database, I want to be able to be redirected to a different URL. I tried using the header function for php but for some reason it will not redirect me to the URL I specified.

<?php
if ($_POST) {
            $host="23.229.187.201"; // Host name 
            $username="inturuser"; // Mysql username 
            $password="admin123"; // Mysql password 
            $db_name="intur"; // Database name 
            $tbl_name="test_mysql"; // Table name 

// Connect to server and select database.
            $link = mysql_connect("localhost", "inturuser", "hola123")or die("Error"); 
            mysql_select_db("intur")or die("cannot select DB");
// Get values from form the form 

            $name=$_POST['name'];
            $lastname=$_POST['lastname'];
            $email=$_POST['email'];
            $bday = date('Y-m-d', strtotime($_POST['bday']));
            $phone = $_POST['phone'];
            $datetime = date('Y-m-d H:i:s');
            $ip = ""; 
            if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
    $ip = $_SERVER['REMOTE_ADDR'];
}


// Insert data into mysql 
$sql = 'INSERT INTO users (nombre, apellido, correo, fecha_nacimiento, telefono, ip_restaurante, timestamp) 
VALUES("'.$name.'", "'.$lastname.'", "'.$email.'", "'.$bday.'", "'.$phone.'", "'.$ip.'", "'.$datetime.'")';

mysql_query($sql);

mysql_close();

// if successfully insert data into database, displays message "Successful and redirect to a URL ". 
if($result)  

{
        header('Location: http://www.example.com/');;
}

else {
echo "ERROR";
     }

//close connection 

   } 

   else {
echo "NO POST"; 
   }
     ?>
3
  • 1
    Is there any white space before the first <?php tag on page? or a blank line? or any html?. There cant be any output to browser before header is called Commented Jun 11, 2014 at 3:12
  • Check if you have any text before headers. Debug the page in browser and see what the headers are. Commented Jun 11, 2014 at 3:13
  • displays message "Successful and redirect to a URL: Did you remove that display part before posting your code here? You cant be displaying anything before you want to redirect using header Commented Jun 11, 2014 at 3:16

2 Answers 2

3

You have not assigned the $result variable so the if($result) will always be false.

You should turn on your error_reporting to show E_NOTICE, and then you need to replace your mysql_query line with $result = mysql_query($sql).

Sign up to request clarification or add additional context in comments.

Comments

0

Your code:

$sql = 'INSERT INTO users (nombre, apellido, correo, fecha_nacimiento, telefono, ip_restaurante, timestamp) 
    VALUES("'.$name.'", "'.$lastname.'", "'.$email.'", "'.$bday.'", "'.$phone.'", "'.$ip.'", "'.$datetime.'")';

    mysql_query($sql);

    mysql_close();

    // if successfully insert data into database, displays message "Successful and redirect to a URL ". 
    if($result)  

    {
            header('Location: http://www.example.com/');;
    }

please remove extra semicolon from header. and assign $result=mysql_query($sql)

corrected solution:

 $sql = 'INSERT INTO users (nombre, apellido, correo, fecha_nacimiento, telefono, ip_restaurante, timestamp) 
    VALUES("'.$name.'", "'.$lastname.'", "'.$email.'", "'.$bday.'", "'.$phone.'", "'.$ip.'", "'.$datetime.'")';

    $result=mysql_query($sql);

    mysql_close();

    // if successfully insert data into database, displays message "Successful and redirect to a URL ". 
    if($result)  

    {
            header('Location: http://www.example.com/');
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.