PHP/MySQL : After inserting the value into a table the value of the variable gets lost and cannot print it out at the end of the file?

标签 php mysql

<分区>

我想在同一个文件中进行多个数据库查询:

创建一个用户,选择新创建用户的 UID,并为同一用户分配特定角色。

从新创建的用户处获取 UID 后,我将该值保存到 $userID 变量中,但在文件末尾,变量值丢失了。

为什么? (PS:我暂时不考虑安全性)。

//Create User
$email = strip_tags($_POST['email']);
$conectar = mysqli_connect(HOST, USER, PASS, DATABASE);
$query = "INSERT INTO usuarios 
            (userEmail) 
          VALUES 
            ('$email')";
$insertarBase = mysqli_query($conectar,$query);
mysqli_close($conectar);

//look for the UID of the newly created user
$conectar2 = mysqli_connect(HOST, USER, PASS, DATABASE);
$buscarUsuario = "SELECT userID, userEmail 
                  FROM usuarios 
                  WHERE userEmail='$email'                  
                    ";
$resultadoBusqueda = mysqli_query($conectar2,$buscarUsuario);   
    $row = mysqli_fetch_array($resultadoBusqueda);
    $userID = $row['userID'];
mysqli_close($conectar2);

//assign a role to the newly created user
$conectar3 = mysqli_connect(HOST, USER, PASS, DATABASE);
$asignarRol = "INSERT INTO rolesUsuarios 
            (userID, nombreRol) 
          VALUES 
            ('$userID', 'registered')
          ";
$asignarRolenBase = mysqli_query($conectar3,$asignarRol);
mysqli_close($conectar3);

echo $userID; //Here the content of $userID is gone, nothing gets printed out

编辑:

由于某些奇怪的原因,$userID = mysqli_insert_id($conectar); 返回零。 usuarios表语句的创建是这样的:

CREATE TABLE usuarios(
    userID int unsigned not null auto_increment primary key,
    userEmail char(50) not null);

此外,echo $asignarRol; 返回:

INSERT INTO rolesUsuarios (userID, nombreRol) VALUES ('0', 'noAutorizado') 

最佳答案

我尝试整理您的代码并删除多余的代码。

//Create User
$email = $_POST['email']; // you have to verify if this is an email or html etc.
$conectar = new mysqli(HOST, USER, PASS, DATABASE);

$query = "INSERT INTO usuarios 
        (userEmail) 
      VALUES 
        (?)";
$stmt = $conectar->prepare($query);
$stmt->bind_param('s',$email);
$stmt->execute();
$userID = $stmt->insert_id;

$stmt->close();//close statement

//assign a role to the newly created user

$query = "INSERT INTO rolesUsuarios 
        (userID, nombreRol) 
      VALUES 
        (?, 'registered')";
$stmt = $conectar->prepare($query);
$stmt->bind_param('i',$userID);
$stmt->execute();
$stmt->close();
$conectar->close();
echo $userID; //Here the content of $userID

首先,您不必为每个语句创建一个新的数据库连接。 第二:为了安全起见,请准备好您的声明。

如果$userID 为空,生成一个error_log($userID);在你 $userID 得到它的值后,如果它是空的,可能有其他错误。

关于PHP/MySQL : After inserting the value into a table the value of the variable gets lost and cannot print it out at the end of the file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38577687/

相关文章:

php - 如何附加 MYSQL JSON

php - 苹果推送通知 PHP 服务器带宽

php - 如何使用 mysql 数据库数据删除 php 数组上的项目

mysql - 如何引用列的查询值与存储值?

php - 使用 GET 参数在 Symfony 5 中生成 URL

XAMPP 中缺少 php 错误日志

php - 提交按钮内有多个 ID 循环?(购物车)

php - fatal error : Using $this when not in object context with pdo wrapper class

mysql - 在静态库中链接 Mysqllib (MAC OS X.8.4, Xcode 4.6.3)

php - 使用 Codeigniter 在数据库中搜索确切的单词