将当前用户发布到SQL数据库和表中

时间:2013-05-11 04:17:04

标签: php html sql database

我有一个登录系统工作。我还有一个表单,当您选择将数据或值发送到SQL数据库并在不同页面上显示表中的所有已发送值时。

如何显示在桌面上发送该数据的用户?

这是我用于表单发布到数据库的代码。

<form action="add_order.php" method="post">
<input type="hidden" name="s_table" value="$username"/>
<p><input type="checkbox" name="s_food" value="Pepsi" /><font color="gray">PEPSI® </font>
 <font color="white">$2.39</font>       

<p><input type="checkbox" name="s_food" value="Diet Pepsi" /><font color="gray">DIET PEPSI®</font>
 <font color="white">$2.39 </font>      

<p><input type="checkbox" name="s_food" value="Pepsi Max" /><font color="gray">PEPSI MAX®</font>
 <font color="white">$2.39  </font> 

<p><input type="checkbox" name="s_food" value="Mountain Dew" /><font color="gray">MOUNTAIN DEW®</font>
 <font color="white">$2.39 </font>      

<p><input type="checkbox" name="s_food" value="Sierra Mist" /><font color="gray">SIERRA MIST®</font>
 <font color="white">$2.39 </font>      

<p><input type="checkbox" name="s_food" value="Mug Root Beer" /><font color="gray">MUG ROOT BEER®</font>
 <font color="white">$2.39 </font>      

<p><input type="checkbox" name="s_food" value="Spring Water" /><font color="gray">SPRING WATER</font>
 <font color="white">$2.79  </font> 

<p><input type="checkbox" name="s_food" value="Perrier" /><font color="gray">PERRIER®</font>
<font color="white"> $2.79</font>

这是我登录系统的代码。

$_SESSION['username']=$_POST['username'];

 //Checks if there is a login cookie

 if(isset($_COOKIE['ID_my_site']))


 //if there is, it logs you in and directes you to the members page

 { 
    $username = $_COOKIE['ID_my_site']; 

    $pass = $_COOKIE['Key_my_site'];

        $check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());

    while($info = mysql_fetch_array( $check ))  

        {

        if ($pass != $info['password']) 

            {

                        }

        else

            {

            header("Location: menu.php");



            }

        }

 }


 //if the login form is submitted 

 if (isset($_POST['submit'])) { // if form has been submitted



 // makes sure they filled it in

    if(!$_POST['username'] | !$_POST['pass']) {

        die('You did not fill in a required field.');

    }

    // checks it against the database



    if (!get_magic_quotes_gpc()) {

        $_POST['email'] = addslashes($_POST['email']);

    }

    $check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error());



 //Gives error if user dosen't exist

 $check2 = mysql_num_rows($check);

 if ($check2 == 0) {

        die('That user does not exist in our database. <a href=add.php>Click Here to Register</a>');

                }

 while($info = mysql_fetch_array( $check ))     

 {

 $_POST['pass'] = stripslashes($_POST['pass']);

    $info['password'] = stripslashes($info['password']);

    $_POST['pass'] = md5($_POST['pass']);



 //gives error if the password is wrong

    if ($_POST['pass'] != $info['password']) {

        die('Incorrect password, please try again.');

    }
 else 

 { 


 // if login is ok then we add a cookie 

     $_POST['username'] = stripslashes($_POST['username']); 

     $hour = time() + 3600; 

 setcookie(ID_my_site, $_POST['username'], $hour); 

 setcookie(Key_my_site, $_POST['pass'], $hour);  



 //then redirect them to the members area 

 header("Location: menu.php"); 

 } 

 } 

 } 

 else 

{    



 // if they are not logged in 

 ?> 

 <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> 

 <table border="0"> 

 <tr><td colspan=2><h1>Login</h1></td></tr> 

 <tr><td>Username:</td><td> 


 <input type="text" name="username" maxlength="40"> 

 </td></tr> 

 <tr><td>Password:</td><td> 

 <input type="password" name="pass" maxlength="50"> 

 </td></tr> 

 <tr><td colspan="2" align="right"> 

 <input type="submit" name="submit" value="Login"> 

 </td></tr> 

 </table> 

 </form> 

 <?php 

 } 



 ?> 

 </body>
</html>

我希望能够接收我登录的用户并将该用户添加到我订购的饮料中并将其发送到SQL表并显示他们订购的用户和饮料。

1 个答案:

答案 0 :(得分:0)

你可能想采取这种方法,

当您对用户进行身份验证时,会在会话中存储用户的公共信息。然后,当您获得饮料的信息时,您可以使用会话中的数据。

或者,如果您有一张订购饮料的表格,那么我将假设您为每个饮品订单输入一条新记录。您可以使用列来存储用户的username,甚至更好地存储他们的user_id(如果您在users表中有一个)。

然后当您运行查询以获取饮料订单时,您可以JOIN这样的表格

SELECT u.username, u.first_name, o.drink_name FROM users u JOIN orders o ON u.username = o.username

因此,这将提取订单记录,并为您提供订单所属用户的信息。

您的登录脚本很简单,需要一些工作才能使其更安全,但这是一个很好的开始。您遇到的问题是,您需要扩展计划如何保存用户信息和饮料订单信息,然后如何利用某种类型的JOIN来使所有数据一起流动。