gpt4 book ai didi

php - 无法从其他 php 文件获取变量

转载 作者:行者123 更新时间:2023-11-29 12:01:35 25 4
gpt4 key购买 nike

我在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;
}
}
?>

Main.PHP

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

还有

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

但是它不起作用。在我将操作放入表单而不是标题中之前,它起作用了,但它没有在数据库中插入用户,现在它插入了,但它没有显示变量。有办法解决这个问题吗?

最佳答案

1) 不要使用 mysql_ 函数,它已被弃用,并将在 PHP 7 稳定版本中删除,在 mysqli_ 之间选择或PDO .

2) 不要在没有明显原因的情况下多次打开和关闭 php 解释器。如果您的代码是纯 PHP,则标准是永远不要关闭它。

3) 使用 header 后,PHP 或 HTML 不应该有任何其他内容需要处理/显示。 ("位置:...") 函数。这是使用脚本时在脚本中执行的最后一件事。

关于php - 无法从其他 php 文件获取变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32289360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com