Kolibrok
Kolibrok

Reputation: 23

Can't get variables from other php file

I have this code in index.php

<?php
include "ch.php";
?>

ch.php
<?php
if (isset($_POST['Murad'])) {
    header("Location: Main.php");
    $firstname=$_POST['firstname'];
    $lastname=$_POST['lastname'];
    $userName=$_POST['username'];
    $password=$_POST['pwd1'];
    $userName = stripslashes($userName);
    $password = stripslashes($password);
    $userName = mysql_real_escape_string($userName);
    $password = mysql_real_escape_string($password);
    $email=$_POST['email'];
    $mysql_hostname = "localhost";
    $mysql_user = "root";
    $mysql_password = "123";
    $mysql_databse = "websiteusers";
    $prefix = "";
    $bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
    $sql = "
        INSERT INTO websiteusers 
        (fullname,lastname,userName,email,pass) 
        VALUES ( '$firstname', '$lastname','$userName', '$email','$password')
    ";
    mysql_select_db('websiteusers');
    $retval = mysql_query( $sql );
    if (! $retval ) {
        die('Could not enter data: ' . mysql_error());
        return false;
    } else {
        echo "Entered data successfully\n";
    }
    $usernamecheck=mysql_query("
        SELECT `userName` FROM `websiteusers` 
        WHERE userName='$userName'
    ");
    if (mysql_num_rows($usernamecheck)>=1) {
        echo $userName." is already taken";
        return false;
    }
}
?>

And

Main.PHP

<?php
include 'ch.php';
?>

And

<?php  
  echo $firstname=$_POST['firstname'];
?>

But it is not working. It worked before I put action in form instead of header but it didn't insert user in database now it inserts but it is not showing variables. Is there anyway to fix this?

Upvotes: 0

Views: 134

Answers (1)

al&#39;ein
al&#39;ein

Reputation: 1729

1) Do not use mysql_ functions, it's deprecated and will be removed at PHP 7 stable release, choose between mysqli_ or PDO.

2) Don't open and close your php interpreter multiple times with no apparent reason. If your code is pure PHP, a standard is to never close it.

3) There should be nothing else for PHP or HTML to be processed/displayed after using header("Location: ...") function. It's the last thing you do at the script when you use it.

Upvotes: 1

Related Questions