Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php:获取当前会话id或mysqli_insert_id()时出现问题_Php_Mysql - Fatal编程技术网

Php:获取当前会话id或mysqli_insert_id()时出现问题

Php:获取当前会话id或mysqli_insert_id()时出现问题,php,mysql,Php,Mysql,大家好,我到处都找遍了答案,没有任何东西能满足代码要求:/ 这是我的数据库表: 1 Form_ID int(11) AUTO_INCREMENT 2 identifiant varchar(10) 3 Ch varchar(6) 4 Date date 5 Unite varchar(10) 6 Catsoin varchar(30) 7 Soin varchar(30)

大家好,我到处都找遍了答案,没有任何东西能满足代码要求:/

这是我的数据库表:

1   Form_ID int(11)     AUTO_INCREMENT  
2   identifiant varchar(10) 
3   Ch  varchar(6)  
4   Date    date            
5   Unite   varchar(10)     
6   Catsoin varchar(30)         
7   Soin    varchar(30)     
8   Duree   varchar(10)     
9   Debutsoin   time        
10  Finsoin     time
我通过php表单在数据库中进行注入,我的插入工作非常好。 但是为了提高效率,我需要检索用户当前会话的id

这是我创建的表单:“$\u SESSION['id']=$\u POST['identification'];” 这种用法的问题是,当重新加载页面时,浏览器会创建一个新会话,然后释放会话id,因此这种方法对我不起作用

这就是mysqli_insert_id发挥作用的地方。 在数据库表中,我创建了AUTO_INCREMENT属性Form_ID,它是唯一的。 因此,我需要使用mysqli_insert_id来捕获在客户端进行插入时创建的唯一表单id。 这就是我尝试失败的原因: 文件:InsertionBD.php

从手册

mysqli_insert_id函数返回查询生成的id 具有“自动增量”属性的列的表。如果最后 查询不是INSERT或UPDATE语句,或者如果修改的表 没有具有自动增量属性的列,此 函数将返回零

在使用mysqli\u insert\u id之前运行的最后一个查询是select

$result = mysqli_query($connexion,"SELECT `identifiant`,`Ch`,`Date` FROM `formulaire` where 
                                    `Form_ID` = '".$id."'
                                     "); 
因此,预期回报率为0

在运行insert之后,您需要立即获取mysqli_insert_id的值

编辑

Ifisset函数对输入类型按钮作出反应,以便 用户单击它会在php服务器端创建一个反应

也许您可以考虑将id添加到input按钮提交的表单中,这样您就可以访问它发布到的页面上的值

可能使用隐藏字段,例如:

<input type="hidden" name="formulaire_id" value="<?php echo $id;?>" />
哦,我的G 我必须做出一个简单的选择

真正的问题是,当用户在Form.php中插入所有数据时,他有一个选择:
session_start();
include('include/config.php');
include('include/connexion.php');
$id=mysqli_insert_id($connexion);

$result = mysqli_query($connexion,"SELECT `identifiant`,`Ch`,`Date` FROM `formulaire` where 
                                    `Form_ID` = '".$id."'
                                     "); 

If(isset($_POST['SelectID']))

    {

        //$_SESSION['id'] = mysqli_insert_id($connexion); -> not working since Session reload
        echo "processing...<br/>";
        echo "ID of last inserted record is: ".mysqli_insert_id($connexion);
        while($row = mysqli_fetch_array($result)) 
        {
                 $Recupid = $row['identifiant'] ;
                $RecupCh = $row['Ch'] ;
                $RecupDate = $row['Date'];
            echo "<br><b>Votre identifiant :</b>". $Recupid. 
                 "<br><b> Le code horaire choisi :</b> ".$RecupCh.
                 "<br><b> La date de la prestation : </b>".$RecupDate. 
                 "<br><b> L'horaire de la prestation : </b>".$debut." <b>à</b> ".$fin ;
            echo "<br>";

        }
    }
 else
     {
        //I used superglobal $_Sessions before this code on this page but I did not show the code to simplify the view but session works fine 
        echo "<b>Your ID :</b>".$_SESSION['id']."<br />";
        echo "<b>Your Code horaire  :</b>".$_SESSION['Code']."<br />";
        echo "<b>Your Date  : </b>".$_SESSION['Date']."<br />";
        echo "<b>Your First Plage horaire :</b>".$_SESSION['Debut']."<br />";
        echo "<b>Your Second Plage horaire :</b>".$_SESSION['Fin']."<br />";
     }
session_start();

include('include/config.php');
include('include/connexion.php');

$_SESSION['id']=$_POST['Identifiant'];
$_SESSION['Code']=$_POST['CodeHoraire'];
$_SESSION['Date']=$_POST['the_date'];
....
?>
选择重做另一个表单插入->我把他送回Form.php
session_start();
include('include/config.php');
include('include/connexion.php');
$id=mysqli_insert_id($connexion);

$result = mysqli_query($connexion,"SELECT `identifiant`,`Ch`,`Date` FROM `formulaire` where 
                                    `Form_ID` = '".$id."'
                                     "); 

If(isset($_POST['SelectID']))

    {

        //$_SESSION['id'] = mysqli_insert_id($connexion); -> not working since Session reload
        echo "processing...<br/>";
        echo "ID of last inserted record is: ".mysqli_insert_id($connexion);
        while($row = mysqli_fetch_array($result)) 
        {
                 $Recupid = $row['identifiant'] ;
                $RecupCh = $row['Ch'] ;
                $RecupDate = $row['Date'];
            echo "<br><b>Votre identifiant :</b>". $Recupid. 
                 "<br><b> Le code horaire choisi :</b> ".$RecupCh.
                 "<br><b> La date de la prestation : </b>".$RecupDate. 
                 "<br><b> L'horaire de la prestation : </b>".$debut." <b>à</b> ".$fin ;
            echo "<br>";

        }
    }
 else
     {
        //I used superglobal $_Sessions before this code on this page but I did not show the code to simplify the view but session works fine 
        echo "<b>Your ID :</b>".$_SESSION['id']."<br />";
        echo "<b>Your Code horaire  :</b>".$_SESSION['Code']."<br />";
        echo "<b>Your Date  : </b>".$_SESSION['Date']."<br />";
        echo "<b>Your First Plage horaire :</b>".$_SESSION['Debut']."<br />";
        echo "<b>Your Second Plage horaire :</b>".$_SESSION['Fin']."<br />";
     }
session_start();

include('include/config.php');
include('include/connexion.php');

$_SESSION['id']=$_POST['Identifiant'];
$_SESSION['Code']=$_POST['CodeHoraire'];
$_SESSION['Date']=$_POST['the_date'];
....
?>
为了结束这个过程,因为他不需要插入任何其他内容->我将他发送到主页

问题是,当您使用会话时,如果您将用户重定向到同一页面,在我的例子Form.php中,将创建一个新的sesison,然后所有数据都将消失。
session_start();
include('include/config.php');
include('include/connexion.php');
$id=mysqli_insert_id($connexion);

$result = mysqli_query($connexion,"SELECT `identifiant`,`Ch`,`Date` FROM `formulaire` where 
                                    `Form_ID` = '".$id."'
                                     "); 

If(isset($_POST['SelectID']))

    {

        //$_SESSION['id'] = mysqli_insert_id($connexion); -> not working since Session reload
        echo "processing...<br/>";
        echo "ID of last inserted record is: ".mysqli_insert_id($connexion);
        while($row = mysqli_fetch_array($result)) 
        {
                 $Recupid = $row['identifiant'] ;
                $RecupCh = $row['Ch'] ;
                $RecupDate = $row['Date'];
            echo "<br><b>Votre identifiant :</b>". $Recupid. 
                 "<br><b> Le code horaire choisi :</b> ".$RecupCh.
                 "<br><b> La date de la prestation : </b>".$RecupDate. 
                 "<br><b> L'horaire de la prestation : </b>".$debut." <b>à</b> ".$fin ;
            echo "<br>";

        }
    }
 else
     {
        //I used superglobal $_Sessions before this code on this page but I did not show the code to simplify the view but session works fine 
        echo "<b>Your ID :</b>".$_SESSION['id']."<br />";
        echo "<b>Your Code horaire  :</b>".$_SESSION['Code']."<br />";
        echo "<b>Your Date  : </b>".$_SESSION['Date']."<br />";
        echo "<b>Your First Plage horaire :</b>".$_SESSION['Debut']."<br />";
        echo "<b>Your Second Plage horaire :</b>".$_SESSION['Fin']."<br />";
     }
session_start();

include('include/config.php');
include('include/connexion.php');

$_SESSION['id']=$_POST['Identifiant'];
$_SESSION['Code']=$_POST['CodeHoraire'];
$_SESSION['Date']=$_POST['the_date'];
....
?>
因此,解决方案是:我创建了第二个Form2.php,下面是注册所需数据的代码: Form.php
session_start();
include('include/config.php');
include('include/connexion.php');
$id=mysqli_insert_id($connexion);

$result = mysqli_query($connexion,"SELECT `identifiant`,`Ch`,`Date` FROM `formulaire` where 
                                    `Form_ID` = '".$id."'
                                     "); 

If(isset($_POST['SelectID']))

    {

        //$_SESSION['id'] = mysqli_insert_id($connexion); -> not working since Session reload
        echo "processing...<br/>";
        echo "ID of last inserted record is: ".mysqli_insert_id($connexion);
        while($row = mysqli_fetch_array($result)) 
        {
                 $Recupid = $row['identifiant'] ;
                $RecupCh = $row['Ch'] ;
                $RecupDate = $row['Date'];
            echo "<br><b>Votre identifiant :</b>". $Recupid. 
                 "<br><b> Le code horaire choisi :</b> ".$RecupCh.
                 "<br><b> La date de la prestation : </b>".$RecupDate. 
                 "<br><b> L'horaire de la prestation : </b>".$debut." <b>à</b> ".$fin ;
            echo "<br>";

        }
    }
 else
     {
        //I used superglobal $_Sessions before this code on this page but I did not show the code to simplify the view but session works fine 
        echo "<b>Your ID :</b>".$_SESSION['id']."<br />";
        echo "<b>Your Code horaire  :</b>".$_SESSION['Code']."<br />";
        echo "<b>Your Date  : </b>".$_SESSION['Date']."<br />";
        echo "<b>Your First Plage horaire :</b>".$_SESSION['Debut']."<br />";
        echo "<b>Your Second Plage horaire :</b>".$_SESSION['Fin']."<br />";
     }
session_start();

include('include/config.php');
include('include/connexion.php');

$_SESSION['id']=$_POST['Identifiant'];
$_SESSION['Code']=$_POST['CodeHoraire'];
$_SESSION['Date']=$_POST['the_date'];
....
?>
然后文件InsertionBD.php将基本插入…,在这个页面中,我用一个Header.Location重定向到一个重定向页面,在该页面中,用户选择:返回主页或进行另一次插入。 如果他选择另一个插入: ->用户重定向到Form2.php:

<?php

session_start();



include('include/config.php');
include('include/connexion.php');

echo "Your id: ".$_SESSION['id'];
echo "<br>Your Code: " .$_SESSION['Code'];
echo "<br>Your Date: ".$_SESSION['Date'];
?>

这是一个基本的愚蠢的故事,我不得不说的会议,我的问题确实是在概念,而不是在技术。我希望它能帮助处于这种情况下的其他人


我感谢伊恩·肯尼和安德鲁的关心:

此时此刻,我正在使用session\u id;但是id总是返回相同的号码,即使在我关闭/打开浏览器时…也值得你阅读好的,我已经阅读了,我必须使用PDO,以使其更安全,谢谢这一点;您可以在回答中使用带有mysqli第二选项的准备语句是的,我将使用这个选项2。使用mysqli:$stmt=$dbConnection->prepare'SELECT*FROM employees WHERE name=$stmt->bind_参数',$name$stmt->execute;我理解这一点,所以我在Select之前删除了mysqli_insert_id,并且只让函数在我插入之后进入。现在的问题是,代码在最后插入的记录的ID中甚至没有返回0:它只返回零,当我尝试使用mysqli_insert_ID时出现问题,我遗漏了一些东西