无法更新数据库

时间:2016-09-15 10:55:59

标签: php jquery

这是我的userprofile.php。我在此页面中有更新按钮。当我点击此按钮时,表格将隐藏,表格将显示。但是当我点击更新个人资料按钮时它会重定向我的ajax.php到此页面而不在ajax.php页面中运行更新查询。

 <?php
      session_start();
     if(isset($_SESSION['email']))
       {
         include('config.php');
          include('userheader.php');
          include('usersiderbar.php');;
    ?> 
  <div id="page-wrapper">
   <div class="col-md-12 graphs">
   <div id="form4" class="xs">
 <h3><strong>Your Profile</strong></h3>
<div class="bs-example4" data-example-id="simple-responsive-table">

<div class="table-responsive">
  <table class="table table-bordered">
    <thead>
      <tr>
        <th><strong>Name</strong></th>
        <th><strong>Email</strong></th>
        <th><strong>Phone Number</strong></th>

      </tr>
    </thead>
    <tbody>
     <?php

     $query1=mysql_query("select * from userregister where email='".$_SESSION['email']."'");

  while($query2=mysql_fetch_array($query1))
{
 ?>
      <tr>
        <th scope="row"><?php echo $query2['name'];?></th>
        <td><?php echo $query2['email'];?></td>
        <td><?php echo $query2['phone'];?></td>  
      </tr>     
  <?php
  }
   ?>
    </tbody>
    </table>
   </div><!-- /.table-responsive -->

    <div style="text-align:center;"><input type="button" name="upd4" id="upd4"  class="btn-success btn" value="Edit Profile" /></div>

    </div>

     </div>

     <div id='updateprofile'>

     </div>

    </div>
    </div>
  <!-- /#page-wrapper -->
    <?php
    include('adminfooter.php');
    }
    else {
    $_SESSION['invalid']=='set';
    header('location:login.php');
    }
    ?>
  <script>
 $(document).ready(function(){
  $("#upd4").click(function(){

 $.ajax({
                type: "POST",
                url: "ajax.php",
                data: $("form").serialize(),
                success: function(data) {
                  $("#form4").hide();
                  $("#updateprofile").show();
                  $("#updateprofile").append(data);
                }
 });

 });

 });

  </script>

我正在更新数据库,但它没有显示任何更新。首先我有用户详细信息和编辑按钮。通过单击编辑按钮,将使用ajax打开一个表单。

    <?php 
     session_start();
     include('config.php');
     $query2 = "SELECT * FROM userregister where email='".$_SESSION['email']."'";
     $result2 = mysql_query($query2);
     $row2 = mysql_fetch_array($result2);

    if(isset($_POST['updatenew']))
    {

    $name=$_REQUEST['name'];
    $email=$_REQUEST['email'];
    $phone=$_REQUEST['phone'];

    $sql="UPDATE userregister SET name='$name',email='$email',phone='$phone' where email='".$_SESSION['email']."'";
    $res=mysql_query($sql);

    echo "<script>alert('Your Record Sucessfully Updated.');</script>";
     } 
    ?>
           <h3>Update Your Profile</h3>
             <div class="tab-content">
                        <div class="tab-pane active" id="horizontal-form">

                            <form class="form-horizontal" action="" method="post">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">Name :- </label>
                                    <div class="col-sm-8">
                                        <input type="text" name="name" id="name" class="form-control1" value="<?php echo $row2['name']; ?>" tabindex="1" />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">Email :- </label>
                                    <div class="col-sm-8">
                                        <input type="text" name="email" id="email" class="form-control1" value="<?php echo $row2['email']; ?>" tabindex="1" />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">Phone Number :- </label>
                                    <div class="col-sm-8">
                                        <input type="text" name="phone" id="phone" class="form-control1" value="<?php echo $row2['phone']; ?>" tabindex="1" />
                                    </div>
                                </div>

                              <div style="text-align:center;"><button class="btn-success btn" name="updatenew" id="updatenew">Update Profile</button></div> 

                            </form>
                         </div>
             </div>

我在数据库中有5列,但我通过选择特定的电子邮件ID仅更新3列。它在输入框中显示值,但是当我更改输入框中的值时,它没有显示任何消息。 这是我的userregister.php,我有按钮。点击该按钮会显示ajax.php。

2 个答案:

答案 0 :(得分:1)

请在表单中添加新行

    <div class="tab-content">
                    <div class="tab-pane active" id="horizontal-form">

                        <form class="form-horizontal" action="" method="post">
                           <input type="hidden" name="updatenew" value="Update Profile" />
                            <div class="form-group">
                                <label class="col-sm-2 control-label">Name :- </label>
                                <div class="col-sm-8">
                                    <input type="text" name="name" id="name" class="form-control1" value="<?php echo $row2['name']; ?>" tabindex="1" />
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">Email :- </label>
                                <div class="col-sm-8">
                                    <input type="text" name="email" id="email" class="form-control1" value="<?php echo $row2['email']; ?>" tabindex="1" />
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">Phone Number :- </label>
                                <div class="col-sm-8">
                                    <input type="text" name="phone" id="phone" class="form-control1" value="<?php echo $row2['phone']; ?>" tabindex="1" />
                                </div>
                            </div>

                          <div style="text-align:center;"><button class="btn-success btn" name="updatenew" id="updatenew">Update Profile</button></div> 

                        </form>
                     </div>
         </div>

  <script>
     $(document).on("click", "#updatenew", function(){

       $.ajax({
            type: "POST",
            url: "ajax.php",
            data: $("form").serialize(),
            success: function(data) {
              alert(data);
              $("#updateprofile").hide();
            }
  });

  });
 </script>

然后更改按钮名称 希望它对你有用:)。

答案 1 :(得分:0)

永远不会发布按钮。所以if(isset($_POST['updatenew'])) { }永远不会成真

将其更改为表单的必填字段的名称。例如:if(isset($_POST['name'])) { }

相关问题