简体   繁体   中英

PHP redirect to another URL after submitting a form

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"; 
   }
     ?>

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) .

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/');
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM