简体   繁体   中英

Store session array in DB

<?php               
include 'connection.php';
session_start();
$noteinfo=array();
$noteinfo['note']=$_POST['note'];
$_SESSION['noteinfo']=$noteinfo;

    if (isset($_POST['submit'])) {
        if (empty($_POST['note'])) {
            echo "Dobavete Komentar";
        }if (!empty($_SESSION['noteinfo'])) {
            $check=mysqli_escape_string($conn,$_SESSION['userinfo']['fname']);

        $sql = "INSERT INTO users (user_fname,user_mname,user_lname,user_login,user_email,user_phone) VALUES ('$_SESSION['userinfo'][0]}','{$_SESSION['userinfo'][1]}','{$_SESSION['userinfo'][2]}','{$_SESSION['userinfo'][3]}','{$_SESSION['userinfo'][4]}','{$_SESSION['userinfo'][5]}')";
        $sql1= "INSERT INTO addresses (address_line_1,address_line_2,address_zip,address_city,address_province,address_country) VALUES ('$_SESSION[adr1]','$_SESSION[adr2]','$_SESSION[zip]','$_SESSION[city]','$_SESSION[provinciq]','$_SESSION[durjava]')";    
        $sql2="INSERT INTO notes (note_text) VALUES ('$_SESSION[note]')";
        if (mysqli_query($conn,$sql)) {
            echo "Added";
        if (mysqli_query($conn,$sql1)) {
            echo "Added";
            if (mysqli_query($conn,$sql2)) {
                echo "Added";
                header("refresh:3 ; url=profile.php");
            }
        } 
        }else{
            echo "Error";
        }
    }else{
        header("refresh:1 ; url=zapiski.php");
    }

?>

When i complete my 3 forms for users info,i store them in sessions array and after the third step i want to add the whole information from the arrays in my Database.But when i click the submit button,my db is filling only with ID's and no data.

session_start();
$userinfo=array();
$userinfo['fname']=$_POST['Fname'];
$userinfo['mname']=$_POST['Mname'];
$userinfo['lname']=$_POST['Lname'];
$userinfo['login']=$_POST['login'];
$userinfo['email']=$_POST['email'];
$userinfo['phone']=$_POST['phone'];
$_SESSION['userinfo']=$userinfo;

This is the first array,just like the other 2 .

$_SESSION This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. There is no need to do global $variable; to access it within functions or methods.

<?php
include 'connection.php';
session_start();
$noteinfo=array();
$noteinfo['note']=$_POST['note'];
$_SESSION['noteinfo']=$noteinfo;

    if (isset($_POST['submit'])) {
        if (empty($_POST['note'])) {
            echo "Dobavete Komentar";
        }if (!empty($_SESSION['noteinfo'])) {
            $check=mysqli_escape_string($conn,$_SESSION['userinfo']['fname']);

$sql = "INSERT INTO users (user_fname,user_mname,user_lname,user_login,user_email,user_phone) VALUES ('{$_SESSION['userinfo']['fname']}','{$_SESSION['userinfo']['mname']}','{$_SESSION['userinfo']['lname']}','{$_SESSION['userinfo']['login']}','{$_SESSION['userinfo']['email']}','{$_SESSION['userinfo']['phone']}')";
$sql1 = "INSERT INTO addresses (address_line_1,address_line_2,address_zip,address_city,address_province,address_country) VALUES ('$_SESSION[adr1]','$_SESSION[adr2]','$_SESSION[zip]','$_SESSION[city]','$_SESSION[provinciq]','$_SESSION[durjava]')";
$sql2="INSERT INTO notes (note_text) VALUES ('$_SESSION[note]')";
if (mysqli_query($conn,$sql)) {
echo "Added"; if (mysqli_query($conn,$sql1)) {
echo "Added";
if (mysqli_query($conn,$sql2)) {
echo "Added";
header("refresh:3 ; url=profile.php");
}

}
}else{
echo "Error"; }

} else{

    header("refresh:1 ; url=zapiski.php");
}
        }  

?>
PHP $_SESSION visit this link

You are using indexes(0,1,2,....) instead of (fname, mname, lname,....) . So you should update line following

$sql = "INSERT INTO users (user_fname,user_mname,user_lname,user_login,user_email,user_phone) VALUES ('{$_SESSION['userinfo'][0]}','{$_SESSION['userinfo'][1]}','{$_SESSION['userinfo'][2]}','{$_SESSION['userinfo'][3]}','{$_SESSION['userinfo'][4]}','{$_SESSION['userinfo'][5]}')";

with

$sql = "INSERT INTO users (user_fname,user_mname,user_lname,user_login,user_email,user_phone) VALUES ('{$_SESSION['userinfo']['fname']}','{$_SESSION['userinfo']['mname']}','{$_SESSION['userinfo']['lname']}','{$_SESSION['userinfo']['login']}','{$_SESSION['userinfo']['email']}','{$_SESSION['userinfo']['phone']}')";

You have a syntax error inside your first query, you're missing { This '$_SESSION['userinfo'][0]}', should be '{$_SESSION['userinfo'][0]}'.

Also, consider removing the indexes from the variables; this '{$_SESSION['userinfo'][0]}'. to '{$_SESSION['userinfo']}' .

You are sending wrong information as values. You should write for example VALUES ('{$_SESSION['userinfo']['fname']}',... inted of

VALUES ('$_SESSION['userinfo'][0]}','{$_SESSION['userinfo'][1]}','{$_SESSION['userinfo'][2]}','{$_SESSION['userinfo'][3]}','{$_SESSION['userinfo'][4]}','{$_SESSION['userinfo'][5]}')"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM