显示和更新用户信息

时间:2016-07-25 08:04:04

标签: php html mysql

我是PHP的新手并且学习更新mysql中的用户信息。这里的问题是我无法在表单上显示数据,也无法更新用户的信息。我的表单似乎是空的,而不是有任何用户信息

<?php
session_start();
require('config.php');
error_reporting(0);
if(isset($_POST["submit"]))
{
    $sqlupdate = mysql_query("UPDATE user
                  SET
                    first_Name = '{$_POST['sname']}',
                    password = '{$_POST['spassword']}',
                   WHERE first_Name = '{$_POST['sname']}'
                ");         
}


$sql=mysql_query("SELECT * FROM student WHERE email='$_SESSION['SESS_USEREMAIL']' and password ='$_SESSION['SESS_USERPASS']' ");
while($record = mysql_fetch_array($sql)){
    $first = $record['first_Name'];
    $second = $record['first_Name'];
}
?>

<form method="post" name="studentform" id="student-form" action="#" enctype="multipart/form-data">
  <div class="form-group">
    <div class="inputGroupContainer">
      <div class="input-group">
        <span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span>
        <input name="sname" id="studentname" placeholder="Name" class="form-control" type="text" value="<?php echo $first; ?>">
      </div>
    </div>
  </div>
  <div class="form-group">
    <div class="inputGroupContainer">
      <div class="input-group">
        <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
        <input name="spassword" id="studentpassword" placeholder="Password" class="form-control" type="password" value="<?php echo $second; ?>">
      </div>
    </div>
  </div>
  <div class="form-group">
  <button type="submit" id="ssubmitbtn" class="btn btn-primary btn-md" value="submit" name="submit">Submit<span class="glyphicon glyphicon-pencil "></button>                                                               
  </div>

</form>

2 个答案:

答案 0 :(得分:0)

假设您的SQL是正确的,您可以使用PDO's,如下所示;

<?php 

    $sFirst = '';
    $sLast  = '';

    $conn   = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $sql    = "SELECT * FROM user WHERE email= :email and password = :password";
    $st     = $conn->prepare( $sql );
    $st->bindValue(":email", $name, PDO::PARAM_STR);
    $st->bindValue(":password", $password, PDO::PARAM_STR);
    $st->execute();

    if ( $row = $st->fetch() ){
        $sFirst   = $row['first_col_name'];
        $sLast    = $row['seccond_col_name'];
    }
?>

您提到在会话中设置查询中的值,使用以下行可以分配要绑定到查询的var;

$st->bindValue(":email", $_SESSION['SESS_USEREMAIL'], PDO::PARAM_STR);
$st->bindValue(":password", $_SESSION['SESS_USERPASS'], PDO::PARAM_STR);

对于您的更新,您可以使用类似的内容

if(isset($_POST["submit"]))
{   
    $sError    = '';
    $sName     = '';
    $sPassword = '';


    // basic error checking

    if(isset($_POST['sname']) && $_POST['sname'] != ''){
        $sName = $_POST['sname'];
    }
    if($sName == '') $sError .= "[MissingName]";

    if(isset($_POST['spassword']) && $_POST['spassword'] != ''){
        $sPassword = $_POST['spassword'];
    }
    if($sPassword == '') $sError .= "[MissingPassword]";

    // if no errors, proceed to update
    if($sError == ''){

        $conn   = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
        $sql    = "UPDATE user first_name = :firstName, password = :password WHERE first_name :first_name";
        $st     = $conn->prepare( $sql );
        $st->bindValue(":firstName", $_POST['sname'], PDO::PARAM_STR);
        $st->bindValue(":password", $_POST['spassword'], PDO::PARAM_STR);
        $st->execute();
    }
}

在更新之前,我会检查/验证预期输入的信息。

我还会在更新时使用唯一标识符,以确保更新正确的用户。为此,我通常在表格上创建一个触发器,生成一个简短的唯一代码。

答案 1 :(得分:-1)

$sql=mysqli_query("SELECT * FROM user WHERE email='$useremail' and password ='$userpassword' "); 

$ record = mysqli_fetch_array($ sql);

而是写<?php echo $first...使用<?php echo $redord['firstname];?&gt; 这样可以解决你的问题。

P.S

$useremail$userpassword必须存储在会话中(在登录页面中) 使用:

$_SESSION['usermail'] = $usermail;
$_SESSION['userpassword'] = $userpassword;

IN QUERY

$sql=mysql_query("SELECT * FROM user WHERE email='$useremail' and password ='$userpassword' "); 

$usermail替换为$_SESSION['username'];,将$userpassword替换为$_SESSION['userpassword'];