我尝试过使用:
$name = $_SESSION['userid'];
但它不起作用。我也尝试了其他答案,但无济于事。
编辑:是的我正在使用session_start();
S_SESSION
用户名工作正常,但用户ID不返回任何内容。
这是整个代码,抱歉不包括它:
<?php
session_start();
if (isset($_SESSION['username'])){
$name = $_SESSION['userid'];
}
$conn = new mysqli('localhost', 'root', '', 'phvatsim') or die ('Cannot connect to db');
if(isset($_POST['confirm']))
{
$name = $_SESSION['userid'];
$address = $_POST ['adr'];
$product = $_POST ['product'];
$contact = $_POST ['con'];
$email = $_POST ['ema'];
$prod_qty = $_POST['prod_qty'];
$cash=$_POST['cs'];
$username = $_SESSION['username'];
$result = mysqli_query($conn,"SELECT `stocks` FROM `products` WHERE productname = '$product'");
$message = "<script>alert('Order Placed to our Database! We will contact you on the e-mail you provided for your order sumamry, Thank You!');</script>";
while($row = mysqli_fetch_array($result))
{
$_SESSION['stocks'] = $row['stocks'];
}
$newstocks = $_SESSION['stocks'] - $prod_qty;
$update = mysqli_query($conn, "UPDATE products SET `stocks` = '$newstocks' WHERE productname = '$product'");
$sql = ("INSERT INTO `transactions`(`username`, `user_id`, `customer_address`, `customer_contact`,`customer_email`, `prod_name`, `prod_qty`) VALUES ('$username','$name','$address','$contact', '$email','$product', '$prod_qty')");
$result = $conn->query($sql);
if($result === TRUE){
echo $message;
}
}
?>
<meta http-equiv="refresh" content="3;URL=../index.php"/>
Redirecting to home page in 3 seconds. Or redirect <a href="../index.php">now.</a>
&#13;
当我在name = $ _ SESSION [&#39; userid&#39;]中使用userid时,它不会放置值,但是当我使用用户名时,它会显示用户名。
答案 0 :(得分:0)
首先使用
开始您的会话在session_start();
存储会话变量
$名称= $ _ SESSION [ '用户ID'];
$ _ SESSION [ '用户ID'] = $名称;
现在执行您的查询
答案 1 :(得分:0)
<?php
... <?php
session_start();
$_SESSION['my_variable'] = $myvariable;
?>
....
我将提供一个示例:
<?php
session_start();
$myvariable = $_SESSION['my_variable'];
echo "<table><tr><td>$myvariable</td></tr><table>";
?>
将此文件命名为'index.php'。
现在在另一个档案中:
{{1}}
答案 2 :(得分:0)
我认为你正在寻找这样的东西:
<?php
session_start();
if (isset($_SESSION['userid'])) {
$userid = $_SESSION['userid'];
$db_host = "localhost";
$db_name = "test";
$db_user = "root";
$db_pass = "toor";
$db = new PDO("mysql:host=$db_host;dbname=$db_name;charset=utf8","$db_user","$db_pass");
$stmt = $db->prepare(
"INSERT INTO `tblusers`(`id`, `userid`) VALUES (:id, :userid)"
);
$stmt->execute(
array(
'id' => NULL,
'userid' => $userid
)
);
}