PHP不需要的自动提交

时间:2015-09-02 09:15:02

标签: php jquery mysql post

我在HTML中有这个DB条目列表,我希望每行旁边都有一个编辑按钮,以便能够更新用户信息。 POST变量到达更新数据库行的.php文件,问题是我只能编辑列表的最后一行,好像它会自动通过POST提交数据。我只想在按下编辑按钮后才提交数据。

显示当前会员信息并点击重定向到执行查询本身的.php文件

<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("users") or die(mysql_error());

$id = $_POST['id'];

$query = "SELECT * FROM userinfo WHERE id LIKE $id";

$result = mysql_query($query);

$row = mysql_fetch_array($result);

?>

<!DOCTYPE html>
<html>
<head>

	<meta charset="utf-8">
	<title></title>
	<meta name="viewport" content="width=device-width, initial-scale=1.0">

	<link rel="stylesheet" href="css/bootstrap.css">
	<link rel="stylesheet" href="css/style.css">
	<link rel="stylesheet" href="css/font-awesome.css">
	<script src="js/jquery-2.1.4.js"></script>
        <script src="js/bootstrap.js"></script>

</head>

<body>

    <div class="container">
        <div class="well">
            <form action="edit.php" class="form-horizontal well" method="post">
                <fieldset>
                  <legend>Edit a member</legend>
                        <div class="row">
                            <div class="col-xs-8">
                                <div class="form-group">
                                    <div class="rows">
                                        <div class="col-md-8">
                                            <div class="col-lg-6">
                                                <input class="form-control input-lg" id="name" name="name" value="<?php echo $row['name'] ?>" type="text">
                                            </div>

                                            <div class="col-lg-6">
                                                <input class="form-control input-lg" id="surname" name="surname" value="<?php echo $row['surname'] ?>" type="text">
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                    <div class="form-group">
                                        <div class="rows">
                                            <div class="col-md-8">
                                                <div class="col-lg-12">
                                                    <input class="form-control input-lg" id="address" name="address" value="<?php echo $row['address'] ?>" type="text">
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="form-group">
                                        <div class="rows">
                                            <div class="col-md-8">
                                                <div class="col-lg-12">
                                                    <input class="form-control input-lg" id="number" name="number" value="<?php echo $row['number'] ?>" type="text">
                                                    <input type='hidden' name='id' value="<?php echo $row['id'] ?>">
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="form-group">
                                        <div class="rows">
                                            <div class="col-md-8">
                                                <div class="col-lg-12">
                                                        <button class="btn btn-success btn-lg" type="submit">Update member details</button>
                                                        <div class="btn btn-info"><a href="index.php">View member DB</a></div>        

                                                </div>
                                            </div>
                                        </div>
                                    </div>
 
                            </div>
 
			</div>
                </fieldset>
            </form>
	</div>
    </div><!-- /container -->

</body>

</html>

列出当前用户(数据库行)

<!DOCTYPE html>

<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("users") or die(mysql_error());
?>

<html>
<head>

	<meta charset="utf-8">
	<title></title>
	<meta name="viewport" content="width=device-width, initial-scale=1.0">

	<link rel="stylesheet" href="css/bootstrap.css">
	<link rel="stylesheet" href="css/style.css">
	<link rel="stylesheet" href="css/font-awesome.css">
	<script src="js/jquery-2.1.4.js"></script>
        <script src="js/bootstrap.js"></script>

</head>

<body>
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="well">
                    <h2 class="text-center">List of members</h2>
                    <hr width="75%">
                    <table class="table table-striped">
                        <thead>
                            <th width="6%" align="left">Member ID</th>
                            <th width="7%" align="left">First Name</th>
                            <th width="7%" align="left">Last Name</th>
                            <th width="7%" align="center">Address</th>
                            <th width="7%" align="center">Number</th>
                            <th width="7%" align="center">Edit</th>
                        </thead>
                        
                        <tbody>
                            <?php
                                $query = 'SELECT * FROM userinfo';
                                $result = mysql_query($query);
                                
                                
                                while ($row = mysql_fetch_array($result)){
                                   
                                    echo ' <tr> ';
                                    echo ' <td> ';
                                    echo $row['id'];
                                    echo ' <td> ';
                                    echo $row['name'];
                                    echo ' <td> ';
                                    echo $row['surname'];
                                    echo ' <td> ';
                                    echo $row['address'];
                                    echo ' <td> ';
                                    echo $row['number'];
                                    echo "<td><form action='info.php' method='POST'><input type='hidden' name='id' value='".$row['id']."'/><input type='submit' name='submit-btn' value='View/Update Details' /><form></td></tr>";
                                }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        
        <div class="btn btn-info"><a href="insertform.html">Insert a new member</a></div>        
        <div class="btn btn-danger"><a href="clear.php">Clear table</a></div>

        
    </div>
</body>

</html>

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

替换您的编辑按钮代码。

echo "<td><form action='info.php' method='POST'><input type='hidden' name='id' value='".$row['id']."'/><input type='submit' name='submit-btn' value='View/Update Details' /><form></td></tr>";

使用此代码

echo '<a href="update.php/'. $row["id"].'"';

答案 1 :(得分:0)

替换

<form>

</form>

正确地结束表单,否则它将被视为只有一个表单,并且将使用最后一行的值,因为它们是重复的(一个表单包含所有字段)