Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.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 具有';更新';按钮问题_Php_Mysql - Fatal编程技术网

Php 具有';更新';按钮问题

Php 具有';更新';按钮问题,php,mysql,Php,Mysql,我有一个奇怪的问题,我真的不明白。我已经创建了一个注册/登录页面,在注册页面上它会询问您的全名、电子邮件和性别。一旦你登录,就会有一个“我的详细信息”按钮,一旦你点击,你就可以更改你的详细信息。这就是我的问题,当我点击“我的详细信息”时,我可以看到全名、电子邮件和性别。但当我尝试更新它时,它只是清除,即使它声明“已成功更新”,它将被完全擦除,甚至旧的细节也将被删除。我怀疑是以下代码 <?php //this authenticates user session_start(); if(!i

我有一个奇怪的问题,我真的不明白。我已经创建了一个注册/登录页面,在注册页面上它会询问您的全名、电子邮件和性别。一旦你登录,就会有一个“我的详细信息”按钮,一旦你点击,你就可以更改你的详细信息。这就是我的问题,当我点击“我的详细信息”时,我可以看到全名、电子邮件和性别。但当我尝试更新它时,它只是清除,即使它声明“已成功更新”,它将被完全擦除,甚至旧的细节也将被删除。我怀疑是以下代码

<?php
//this authenticates user
session_start();
if(!isset($_SESSION['username'])){
header("location:index.php");
}
$username = $_SESSION['username'];
?>

<?php include ('header.php'); ?> 

<?php
$update = (isset($_GET['update']) ? $_GET['update'] : null);
$full_name = (isset($_GET['full_name']) ? $_GET['full_name'] : null);
$full_name = strip_tags($full_name);
$location = (isset($_GET['location']) ? $_GET['location'] : null);
$location = strip_tags($location);
$gender = (isset($_GET['gender']) ? $_GET['gender'] : null);

if($update == 1 && !empty($_POST)) // Checks if the form is submitted or not
{
$success_update = mysql_query("UPDATE users SET fullname='$full_name', location='$location', gender='$gender' WHERE username='$username' ");
if($success_update) { 
echo '
<div class="alert alert-success">
Account Successfully updated!
</div>
';
} 

else {
echo '
<div class="alert">
  <button type="button" class="close" data-dismiss="alert">&times;</button>
Failed to update
</div>
';


}

}

$document_get = mysql_query("SELECT * FROM users WHERE username='$username'");
$match_value = mysql_fetch_array($document_get);
$fullname = $match_value['fullname'];
$location = $match_value['location'];
$gender = $match_value['gender'];

?>
<br/>

 <div style="float:right"> <a class="btn btn-info" href="dashboard.php" > Account </a>  <a class="btn" href="home.php"> <i class="icon-home icon-black"></i>Home</a> 
 <a class="btn btn-danger logout" href="logout.php" > Logout</a> 

 </div>

 <fieldset>
    <legend>Welcome <?php echo $username; ?>, </legend>

    <br/>
    <br/>
<form action="settings.php?update=1" method="post" name="myForm" onsubmit="return(validate());">
  <fieldset>
    <legend>Settings</legend>

    <label>Full Name *</label>
    <input name="full_name" type="text" placeholder="Type something…" value="<?php echo $fullname; ?>" >
    <br/>
    <label>Location </label>
    <input name="location" type="text" placeholder="Type something…" value="<?php echo $location; ?>">
    <br/>
    <label>Gender </label>
    <select name="gender">
  <option <?php if($gender == 'Male') echo 'selected'; ?> >Male</option>
  <option <?php if($gender == 'Female') echo 'selected'; ?> >Female</option>
</select>

    <br/>
    <button type="submit" class="btn">Update</button>
  </fieldset>
</form>
 </fieldset>





 <script>

 function validate()
{


   if( document.myForm.full_name.value == "" )
   {
     alert( "Please provide your full name!" );
     document.myForm.full_name.focus() ;
     return false;
   }

   return( true );
}


 $('.logout').click(function(){
    return confirm("Are you sure you want to Logout?");
})
</script>
<?php include ('footer.php'); ?> 


  • 您确定它是一个
    $\u GET
    而不是一个
    $\u POST
    ,导致变量为null,从而将所有字段更新为null吗
  • $username
    未被声明
  • 您很容易受到SQL注入的攻击
从代码中检查
$\u POST
,因此我假设您的意思是检查文件顶部的
$\u POST
,而不是
$\u GET

将文件的顶部替换为

$update = (isset($_POST['update']) ? $_POST['update'] : null);
$username = (isset($_POST['username']) ? $_POST['username'] : null);
$full_name = (isset($_POST['full_name']) ? $_POST['full_name'] : null);
$full_name = strip_tags($full_name);
$location = (isset($_POST['location']) ? $_POST['location'] : null);
$location = strip_tags($location);
$gender = (isset($_POST['gender']) ? $_POST['gender'] : null);
此外,请就@esqew评论采取行动

请不要在新代码中使用mysql_*函数。它们不再被维护,并被正式弃用。看到红色的盒子了吗?学习准备好的语句,并使用PDO或MySQLi——本文将帮助您决定使用哪种语句


你确定这是一个
$\u GET
而不是
$\u POST
,导致变量为
null
,从而将所有字段更新为
null
?嗨,我的名字是
bobby';删除表用户--,但当我尝试注册你的网站时,每个人都会崩溃和崩溃。您是否考虑过转义输入,以便人们无法通过输入SQL注入攻击来删除您的表?。它们不再得到维护。看到了吗?相反,学习,并使用or-将帮助您决定哪一个。如果你选择PDO,.@scragar我不打算让我的网站上线。@Ellie不要在安全方面偷懒,即使它不是在线的。谢谢你的回答,但这似乎不起作用。我刚刚更新它,声明
$username
。明智的做法是将查询放入一个变量中,然后
echo
将其输出,这样我们可以进一步调试。(另外,
print\r(mysql\u error($successful\u update))
)我已经更新,因为您可以看到上面编辑过的文章,但仍然无法工作:(