php - php中的表单数据没有更新到mysql表

标签 php html mysql

我有一个登录网站,用户可以使用存储在数据库中的电子邮件和密码进行注册,用户注册后,他可以登录并转到个人资料页面,并根据需要填写额外的详细信息。个人资料页面如下所示:

<?php
  require_once "server.php";

  if (!isset($_SESSION['username'])) {
  	$_SESSION['msg'] = "You must log in first";
  	header('location: login.php');
  }

?>

<!doctype html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang=""> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8" lang=""> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9" lang=""> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js" lang=""> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>TEIA Admin Panel</title>
<meta name="description" content="Ela Admin - HTML5 Admin Template">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="apple-touch-icon" href="images\Icon.png">
<link rel="shortcut icon" href="images\Icon.png">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/normalize.css@8.0.0/normalize.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.1.3/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/lykmapipo/themify-icons@0.1.2/css/themify-icons.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/pixeden-stroke-7-icon@1.2.3/pe-icon-7-stroke/dist/pe-icon-7-stroke.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flag-icon-css/3.2.0/css/flag-icon.min.css">
<link rel="stylesheet" href="assets/css/cs-skin-elastic.css">
<link rel="stylesheet" href="assets/css/style.css">
    <!-- <script type="text/javascript" src="https://cdn.jsdelivr.net/html5shiv/3.7.3/html5shiv.min.js"></script> -->
<link href="https://cdn.jsdelivr.net/npm/chartist@0.11.0/dist/chartist.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/jqvmap@1.5.1/dist/jqvmap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/weathericons@2.1.0/css/weather-icons.css" rel="stylesheet" />
<link href="https://cdn.jsdelivr.net/npm/fullcalendar@3.9.0/dist/fullcalendar.min.css" rel="stylesheet" />
<style>
#weatherWidget .currentDesc {
color: #ffffff!important;
}
.traffic-chart {
min-height: 335px;
}
#flotPie1  {
height: 150px;
}
#flotPie1 td {
padding:3px;
}
#flotPie1 table {
top: 20px!important;
right: -10px!important;
}
.chart-container {
display: table;
min-width: 270px ;
text-align: left;
padding-top: 10px;
padding-bottom: 10px;
}
#flotLine5  {
height: 105px;
}
#flotBarChart {
height: 150px;
}
#cellPaiChart{
height: 160px;
}
</style>
</head>
<body>
<aside id="left-panel" class="left-panel">
<nav class="navbar navbar-expand-sm navbar-default">
<div id="main-menu" class="main-menu collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active">
<a href="index.php"><i class="menu-icon fa fa-laptop"></i>Dashboard </a>
</li>
<li class="menu-title">Menu</li><!-- /.menu-title -->
<li class="menu-item-has-children dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> <i class="menu-icon fa fa-cogs"></i>Demo</a>
<ul class="sub-menu children dropdown-menu">
<li><i class="fa fa-book"></i><a href="ui-switches.html">Switches</a></li>
<li><i class="fa fa-th"></i><a href="ui-grids.html">Grids</a></li>
<li><i class="fa fa-file-word-o"></i><a href="ui-typgraphy.html">Typography</a></li>
</ul>
</li>
<li class="menu-item-has-children dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> <i class="menu-icon fa fa-table"></i>Demo</a>
<ul class="sub-menu children dropdown-menu">
<li><i class="fa fa-table"></i><a href="tables-basic.html">Basic Table</a></li>
<li><i class="fa fa-table"></i><a href="tables-data.html">Data Table</a></li>
</ul>
</li>
<li class="menu-item-has-children dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> <i class="menu-icon fa fa-th"></i>Demo</a>
<ul class="sub-menu children dropdown-menu">
<li><i class="menu-icon fa fa-th"></i><a href="forms-basic.html">Basic Form</a></li>
<li><i class="menu-icon fa fa-th"></i><a href="forms-advanced.html">Advanced Form</a></li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</nav>
</aside>
  <!-- /#left-panel -->
  <!-- Right Panel -->
<div id="right-panel" class="right-panel">
      <!-- Header-->
<header id="header" class="header">
<div class="top-left">
<div class="navbar-header">
<a id="menuToggle" class="menutoggle"><i class="fa fa-bars"></i></a>
</div>
</div>
<div class="top-right">
<div class="header-menu">
<div class="user-area dropdown float-right">
<a href="#" class="dropdown-toggle active" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<img class="user-avatar rounded-circle" src="images/admin.png" alt="User Avatar">
</a>
<div class="user-menu dropdown-menu">
<a class="nav-link" href="profile.php"><i class="fa fa- user"></i>My Profile</a>
                          <!-- <a class="nav-link" href="#"><i class="fa fa -cog"></i>Settings</a> -->
<a class="nav-link" href="logout.php"><i class="fa fa-power -off"></i>Logout</a>
</div>
</div>
</div>
</div>
</header>
        <!-- Header-->
<div class="breadcrumbs">
<div class="breadcrumbs-inner">
<div class="row m-0">
<div class="col-sm-4">
<div class="page-header float-left">
<div class="page-title">
<h1>Profile</h1>
</div>
</div>
</div>
<div class="col-sm-8">
<div class="page-header float-right">
<div class="page-title">
</div>
</div>
</div>
</div>
</div>
</div>

<?php
if(isset($_SESSION['username']) && $_SESSION['username'] == true)
{
$username=$_SESSION['username'];
$check_user="select * from users WHERE username='$username'";
$run=mysqli_query($db,$check_user);
while($row = $run->fetch_assoc())
{
$_SESSION['id']=$row['id'];
}
}
$id=$_SESSION['id'];
$SelSql = "SELECT * FROM `users` WHERE id=$id";
$res = mysqli_query($db, $SelSql);
$r = mysqli_fetch_assoc($res);
if(isset($_POST) & !empty($_POST)){
$phone = $_POST['phone'];
$company = $_POST['company'];
$designation = $_POST['designation'];
$fullname = $_POST['full_name'];
$image = $_POST['image'];
$UpdateSql = "UPDATE `users` SET full_name='$fullname', phone='$phone',
designation='$designation', company='$company', image='$image' WHERE id=$id";
$res = mysqli_query($db, $UpdateSql);
if($res){
header('location: login.php');
}else{
$fmsg = "Failed to update data.";
}
}?>
<?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?>

<div class="content">
<div class="animated fadeIn">
<div class="row">
<div class="col-lg-6">
<div class="card">
<div class="card-header">
<strong class="card-title"></strong>
</div>
<div class="card-body">
                                <!-- Credit Card -->
<div id="pay-invoice">
<div class="card-body">
<div class="card-title">
<h3 class="text-center"></h3>
</div>
<hr>
<form method="post">
<div class="form-group">
<label for="cc-number" class="control-label mb-1">Username</label>
<input id="cc-number" name="cc-number" type="tel" class="form-control cc-number identified visa" value="<?php echo $username; ?>" data-val="true" data-val-required="Please enter the card number" data-val-cc-number="Please enter a valid card number" readonly>
<span class="help-block" data-valmsg-for="cc-number" data-valmsg-replace="true"></span>
</div>
<div class="form-group">
<label for="cc-payment" class="control-label mb-1">Full Name</label>
<input id="cc-payment" name="cc-payment" type="text" class="form-control" aria-required="true" aria-invalid="false" >
</div>
<div class="form-group has-success">
<label for="cc-name" class="control-label mb-1">Email</label>
<input id="cc-payment" name="cc-payment" type="text" class="form-control" aria-required="true" aria-invalid="false" value="<?php echo $r['email'];?>" readonly>
<span class="help-block field-validation-valid" data-valmsg-for="cc-name" data-valmsg-replace="true"></span>
</div>
<div class="form-group">
<label for="cc-payment" class="control-label mb-1">Phone</label>
<input id="cc-payment" name="cc-payment" type="text" class="form-control" aria-required="true" aria-invalid="false" >
</div>
<div class="form-group">
<label for="cc-payment" class="control-label mb-1">Company</label>
<input id="cc-payment" name="cc-payment" type="text" class="form-control" aria-required="true" aria-invalid="false" >
</div>
<div class="form-group">
<label for="cc-payment" class="control-label mb-1">Designation</label>
<input id="cc-payment" name="cc-payment" type="text" class="form-control" aria-required="true" aria-invalid="false" >
</div>
<div class="form-group">
<label for="cc-payment" class="control-label mb-1">Image</label>
<input id="cc-payment" name="cc-payment" type="file" class="form-control" aria-required="true" aria-invalid="false" >
</div>
<div>
<input type="submit" class="btn btn-primary" value="Update">
</div>
</form>
</div>
</div>
</div>
</div> <!-- .card -->
</div><!--/.col-->
</div>
</div><!-- .animated -->
</div><!-- .content -->
<div class="clearfix"></div>
<footer class="site-footer">
<div class="footer-inner bg-white">
<div class="row">
<div class="col-sm-6">
Copyright &copy; 2019 TEIA
</div>
<div class="col-sm-6 text-right">
Designed by <a href="https://booktheparty.in">BookTheParty</a>
</div>
</div>
</div>
</footer>
</div><!-- /#right-panel -->
<!-- Right Panel -->
<!-- Scripts -->
<script src="https://cdn.jsdelivr.net/npm/jquery@2.2.4/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.14.4/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.1.3/dist/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery-match-height@0.7.2/dist/jquery.matchHeight.min.js"></script>
<script src="assets/js/main.js"></script>
</body>
</html>

现在,当用户向表单添加任何数据时,它不会被更新,并且会显示以下错误:

Notice: Undefined index: phone in C:\xampp\htdocs\vendor\profile.php on line 174

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\vendor\profile.php:10) in C:\xampp\htdocs\vendor\profile.php on line 179

现在的问题不是错误,问题是为什么我的数据没有添加到数据库中,任何人都可以告诉我我的代码有什么问题吗?

(注意:我知道这段代码容易受到 SQL 注入(inject),这样做是为了测试目的)

最佳答案

更新数据不起作用,因为您将所有输入名称值设置为 cc- payment name="cc- payment"。每个输入的它应该是唯一的。根据您的帖子请求,它应该是这样的:

考试:

<div class="form-group">
<label for="full_name_label" class="control-label mb-1">Full Name</label>
<input id="full_name_label" name="full_name" type="text" class="form-control" aria-required="true" aria-invalid="false" >
</div>

<div class="form-group has-success">
<label for="phone_label" class="control-label mb-1">Phone</label>
<input id="phone_label" name="phone" type="text" class="form-control" aria-required="true" aria-invalid="false" value="">
</div>

在 php 中:你可以获得值

$fullname = $_POST['full_name'];
$email = $_POST['phone'];

希望你能理解。 :)

关于php - php中的表单数据没有更新到mysql表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57159164/

相关文章:

mysql - 如何使用 Node.JS/Express 显示 JSON 文件

php - 如何使用 google-analytics 和 php 跟踪下载页面?

html - 按钮的 ID 和类选择器 - 以前工作

javascript - django/javascript/jquery/html/css : summing the contents of a form for display

php - fatal error : Out of memory Zend Error/PhpMyAdmin

mysql - 根据单元格值更新 MySQL 中的列

php - load local infile 中 @var 的上下文是什么

php - 如何在 Symfony2 Doctrine2 中获取不同的 DBAL 连接

php - 值不保存在数组中

html - 更改 HTML 密码字段中显示的符号