如何将$ _SESSION用户名插入数据库

时间:2015-11-17 10:22:06

标签: php mysql sql session

我有一个脚本,我可以制作并将news_items添加到mysql数据库中,但我想插入autor名称......然后才能创建一个必须登录的项目,并将名称存储到会话中。但我该如何使用?

这里有一些代码: $ rn是一个测试...但它不会工作:(

<?php
error_reporting(1);
include_once 'dbcon.php';

if(isset($_POST['save_mul']))
{       
    $total = $_POST['total'];

    for($i=1; $i<=$total; $i++)
    {
        $fn = $_POST["fname$i"];
        $ln = $_POST["lname$i"];   
        $rn = $_POST [$_SESSION]'user'];
        $sql = mysql_query("INSERT INTO nieuws_bericht (title,berichten,aangemaakt) VALUES('".$fn."','".$ln."','".$rn."' ");
        $sql = $MySQLiconn->query($sql);        
    }

    if($sql)
    {
        ?>
        <script>
        alert('<?php echo $total." bericht(en) zijn aangemaakt !!!"; ?>');
        window.location.href='index.php';
        </script>
        <?php
    }
    else
    {
        ?>
        <script>
        alert('error while inserting , TRY AGAIN');
        window.location.href='index.php';
        </script>
        <?php
    }
}
?>

4 个答案:

答案 0 :(得分:1)

首先,要访问会话,您需要在输出任何代码之前宣布session_start()(理想情况下,在文档的顶部)。将PHP文件的前几行更改为:

<?php
session_start();
error_reporting(1);
include_once 'dbcon.php';

其次,这一行是错误的:

$rn = $_POST [$_SESSION]'user'];

你想在这做什么?

要访问会话的user变量,只需使用$_SESSION["user"],就像这样:

$rn = $_SESSION['user'];

第三,再看看这一行:

$sql = mysql_query("INSERT INTO nieuws_bericht 
        (title,berichten,aangemaakt)
        VALUES('".$fn."','".$ln."','".$rn."' ");

您错过了字符串中的括号。将其更改为:

$sql = mysql_query("INSERT INTO nieuws_bericht 
        (title,berichten,aangemaakt)
        VALUES('".$fn."','".$ln."','".$rn."')");

答案 1 :(得分:0)

要使用会话变量,只需使用

#!/bin/bash

var1=$1;
var2=$2;
var3=$3;

echo "$var1";
echo "$var2";
echo "$var3";

echo $var1 $var2 $var3 > ./test.txt

你可以在for循环之前设置这个变量。无需在for循环中使用它

答案 2 :(得分:0)

要使用会话,首先需要启动它:

<?php
session_start();

那么$ rn应该成为这个:

  $rn = $_SESSION['user']; 

并且第一个$ sql应该成为:

  $sql = "INSERT INTO nieuws_bericht (title,berichten,aangemaakt) VALUES('$fn','$ln','$rn')"; 

始终准备您的查询

选项1($ MySQLiconn)

<?php
session_start();
error_reporting(1);
include_once 'dbcon.php';

if(isset($_POST['save_mul']))
{       
    $total = $_POST['total'];

    for($i=1; $i<=$total; $i++)
    {
        $fn = $_POST["fname$i"];
        $ln = $_POST["lname$i"];   
        $rn = $_SESSION]['user'];
        $sql = "INSERT INTO nieuws_bericht (title,berichten,aangemaakt) VALUES('$fn','$ln','$rn')";
        $sql = $MySQLiconn->query($sql)or die($MySQLiconn->error);        
    }

    if($sql)
    {
        ?>
        <script>
        alert("<?=$total?> bericht(en) zijn aangemaakt !!!");
        window.location.href='index.php';
        </script>
        <?php
    }
    else
    {
        ?>
        <script>
        alert('error while inserting , TRY AGAIN');
        window.location.href='index.php';
        </script>
        <?php
    }
}
?> 

选项2(mysql _)

<?php
session_start();
error_reporting(1);
include_once 'dbcon.php';

if(isset($_POST['save_mul']))
{       
    $total = $_POST['total'];

    for($i=1; $i<=$total; $i++)
    {
        $fn = mysql_real_escape_string(stripslashes($_POST["fname$i"]));
        $ln = mysql_real_escape_string(stripslashes($_POST["lname$i"]));   
        $rn = mysql_real_escape_string(stripslashes($_SESSION]['user']));
        $sql = "INSERT INTO nieuws_bericht (title,berichten,aangemaakt) VALUES('$fn','$ln','$rn')";
        $sql = mysql_query($sql)or die(mysql_error());        
    }

    if($sql)
    {
        ?>
        <script>
        alert("<?=$total;?> bericht(en) zijn aangemaakt !!!");
        window.location.href='index.php';
        </script>
        <?php
    }
    else
    {
        ?>
        <script>
        alert('error while inserting , TRY AGAIN');
        window.location.href='index.php';
        </script>
        <?php
    }
}
?> 

答案 3 :(得分:0)

1)这一行出错。在行尾没有关闭')'

$sql = mysql_query("INSERT INTO nieuws_bericht (title,berichten,aangemaakt) VALUES('".$fn."','".$ln."','".$rn."' ");

2)改变这个

$rn = $_POST [$_SESSION]'user'];

$rn = $_SESSION['user'];

3)无需使用$sql=mysql_query(""); As,在下一行中执行它。

$sql = "INSERT INTO nieuws_bericht (title,berichten,aangemaakt) VALUES('$fn','$ln','$rn')";
$sql = $MySQLiconn->query($sql);    

更新代码

.
.
for($i=1; $i<=$total; $i++)
{
    $fn = $_POST["fname$i"];
    $ln = $_POST["lname$i"];   
    $rn = $_SESSION['user'];
    $sql = "INSERT INTO nieuws_bericht (title,berichten,aangemaakt) VALUES('$fn','$ln','$rn')";
    $sql = $MySQLiconn->query($sql);          
}
.
.