简体   繁体   English

PHP中的表单数据未更新到mysql表

[英]form data in php not being update to mysql table

I have a login website, user can register using email and password, which are stored in the database, after user registers, he can login and go to the profile page and fill up the extra details if needed. 我有一个登录网站,用户可以使用存储在数据库中的电子邮件和密码进行注册,用户注册后,他可以登录并进入个人资料页面并在需要时填写其他详细信息。 the profile page will look like below: 个人资料页面如下所示:

 <?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> 

now when the user adds any data to the form, its no being updated and the following error shows up: 现在,当用户将任何数据添加到表单时,它不会被更新,并且会显示以下错误:

Notice: Undefined index: phone in C:\\xampp\\htdocs\\vendor\\profile.php on line 174 注意:未定义的索引:第174行的C:\\ xampp \\ htdocs \\ vendor \\ profile.php中的电话

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 警告:无法修改标头信息-第179行的C:\\ xampp \\ htdocs \\ vendor \\ profile.php中已经发送过的标头(输出从C:\\ xampp \\ htdocs \\ vendor \\ profile.php:10开始)

now the problem is not the error, the problem is why is my data not being added to the database, can anyone please tell me what's wrong with my code? 现在的问题不是错误,问题是为什么我的数据没有被添加到数据库中,谁能告诉我我的代码有什么问题吗?

(NB: I know this code is prone to SQL injections, am doing this for testing purpose) (注意:我知道此代码易于进行SQL注入,正在出于测试目的进行此操作)

update data is not working because you put input name value as cc-payment name="cc-payment" for all. 更新数据不起作用,因为您将所有输入名称值都设置为cc-payment name="cc-payment" it should be unique of each input. 每个输入应该唯一。 as per your post request, it should be like this : 根据您的职位要求,应该是这样的:

exam : 考试:

<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>

in php : you can get value 在php中:您可以获得价值

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

Hope you understand. 希望你能理解。 : ) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM