如何使用会话在文本框中显示自动增量值

时间:2014-04-16 04:30:04

标签: php

  

的login.php

     session_start();
     <?php

     $username = "root";
     $password = "tiger";
     $hostname = "localhost"; 
     //connection to the database
      $dbhandle = mysqli_connect($hostname, $username, $password) 
     or die("Unable to connect to MySQL");

     //select a database to work with
     /* @var $selected type */
     $selected = mysqli_select_db($dbhandle,"sample") 
     or die("Could not select sample");
     $name=(\filter_input(\INPUT_POST,'name'));
     $phone=(\filter_input(\INPUT_POST,'phone'));
     $email=(\filter_input(\INPUT_POST,'email'));
     //$custno=(\filter_input(\INPUT_POST,'custno'));
     if(!empty(\filter_input(\INPUT_POST,'continue')))
       {
         echo "<script type='text/javascript'>\n";
         'check()';
         echo "</script>";
           $sql="insert into customersignin(name,phone,email)values('$name','$phone','$email')";
         $result=mysqli_query($dbhandle,$sql) or die(\mysqli_error($dbhandle));


}
  else
   {
   $sql1="insert into customersignin(custno)values(NULL)";
    $result1=mysqli_query($dbhandle,$sql1) or die(\mysqli_error($dbhandle)); 

 }
 $sql2="select custno from customersignin";
 $result2=mysqli_query($dbhandle,$sql2) or die (mysqli_error($dbhandle));
  $row=  mysqli_fetch_array($result2);
  if(mysqli_num_rows($result2)>0)
  {

   echo  "$_SESSION['custno']";
   unset($_SESSION['custno'];
    header('Location:customersvsoup.php');
  }

mysqli_close($dbhandle);

$_SESSION[name]=(\filter_input(INPUT_POST,'name'));
  

customer.php

   <body>
   <?php session_start(); ?>
   <input type="text" style="position: absolute;top:200px;" value="<?php echo $_SESSION["custno"]?>">
</body>
  

在php文件中,客户登录完成,custno是自动生成字段,我有2个按钮,名为continue和skip,因为自动生成工作正常,任何按钮操作完成后,我需要使用session在下一页的文本框中显示custno。但问题是当我运行此代码时文本框为空。但会话[&#39; name&#39;]正在运行..请帮助。

2 个答案:

答案 0 :(得分:1)

您的session_start();应位于login.php中文件的开头。我看到你在被叫之前使用$_SESSION[custno]。这就是您的文本框为空的原因。

也应该是:

$_SESSION['custno'] $_SESSION['name'] 请注意单引号

关于您的逻辑问题(在评论中),请尝试:

$_SESSION['name'] = (filter_input(INPUT_POST, 'name'));

if (!empty(filter_input(INPUT_POST, 'continue')))
    {
    echo "<script type='text/javascript'>\n";
    'check()';
    echo "</script>";
    $sql = "insert into customersignin(name,phone,email)values('$name','$phone','$email')";
    $result = mysqli_query($dbhandle, $sql) or die(mysqli_error($dbhandle));
    $sql2 = "select max(custno) as last_custno from customersignin";
    $result2 = mysqli_query($dbhandle, $sql2) or die(mysqli_error($dbhandle));
    if (mysqli_num_rows($result2) > 0)
        {
        $row = mysqli_fetch_assoc($result2);
        $_SESSION['custno'] = $row['last_custno'];
        header('Location:customersvsoup.php');
        }
    }
  else
    {
    $sql1 = "insert into customersignin(custno)values(NULL)";
    $result1 = mysqli_query($dbhandle, $sql1) or die(mysqli_error($dbhandle));
    //since this bit of code is repeating,
    //you could even use a function to shorten it
    $sql2 = "select max(custno) as last_custno from customersignin";
    $result2 = mysqli_query($dbhandle, $sql2) or die(mysqli_error($dbhandle));
    if (mysqli_num_rows($result2) > 0)
        {
        $row = mysqli_fetch_assoc($result2);
        $_SESSION['custno'] = $row['last_custno'];
        header('Location:customersvsoup.php');
        }
    }

请在session_start();之后将<?php 置于之内。所有的PHP代码都应该在PHP标签内。

答案 1 :(得分:0)

插入查询中有错误:

$sql="insertintocustomersignin(name,phone,email)values('$name','$phone','$email')";

应该是:

$sql="insert into customersignin(name,phone,email) values ('$name','$phone','$email')";

你应该在数组索引中使用引号: $_SESSION[custno], $_SESSION[name]应为$_SESSION['custno'], $_SESSION['name']