更新php中的选定行

时间:2014-05-22 10:43:43

标签: php mysql

我需要更新页面中的选定行。我有一个网页,用户输入id。在第二页中,输入具有相同Id的所有行和值,以便进行编辑。我怎么能这样做..

这是代码

    <form method="post" action="edituser.php">
    <label type="text" name="name" maxlength="50" size="30" class="label">Enter the Membership Number</label><br />
    <input type="text" name='id' placeholder="enter Membership Number" class="input" size="40"/><br />
    <span class="field">(* Required field)</span><br /><br />

    <input type="submit" name="submit" value="SUBMIT" class="button"><br /><br /><br /><br />
    </form>
    </body>
    </html>

    <?php
    mysql_connect("localhost","root","");
    mysql_select_db("anthonys");
    if(isset($_POST['submit']))
    {
    $id= $_POST['id'];

    if( ! ctype_alnum($id) )
      die('invalid id');


    $query = "SELECT id FROM `member` WHERE `id` =$id";


    $run = mysql_query($query);


    if(mysql_num_rows($run)>0){
    echo "<script>window.open('edit.php?id=".$id."','_self')</script>";
    }

    else {

        echo "<script>alert('Membership No is Invalid!')</script>";
        }
    }
    ?>





    <

h2>Application for the Membership</h2><br /><br />
<table border="0px" style="border-collapse:collapse; width:810px;" align="center">
<tr>
<td>
<form name="XIForm" id="XIForm" method="POST" action="pdf/pdf2.php">
<label type="text" name="uid" maxlength="50" size="30" class="label">Membership No</label><br />
<input type="text" name="id" id="id" value="<?php if(isset($_GET['id'])) { echo $_GET['id']; } ?>" readonly> <br /><br />
<label type="text" name="fathername"  maxlength="50" size="30" class="label">Father`s Name</label><br />
<input  name="fathername"name="fathername" placeholder="" class="input" size="40"value="<?php if(isset($_GET['fathername'])) { echo $_GET['fathername']; } ?>"> <br /><br />









    <input type="hidden" name="formType" id="formType" value="reg"/>
        <input type="button" name="XISubmit" id="XISubmit" value="ADD" class="button" />        


<br /><br /><br /><br />
</form></td>


</tr>
</table>



</div>


</div>
</section>
</body>
</html>

    <?php
    mysql_connect("localhost","root","");
    mysql_select_db("anthonys");

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



    $UpdateQuery = "UPDATE member SET fathername='$_POST[fathername]' WHERE id='$_POST[hidden]'";               
    $run=mysql_query($UpdateQuery);
    };


    echo "<table border=1>
    <tr>
    <th>Fathers_Name</th>
    "</tr>";
    while($record = mysql_fetch_array($myData)){
    echo "<form action=edit.php method=post>";
    echo "<tr>";

    echo "<td>" . "<input type=text name=Father_name value=" . $record['fathername'] . " </td>";


    echo "<td>" . "<input type=hidden name=hidden value=" . $record['id'] . " </td>"; 
    echo "<td>" . "<input type=submit name=update value=update" . " </td>";

    echo "</tr>";
    echo "</form>";
    }
    echo "<form action=edit.php method=post>";
    echo "<tr>";

    echo "<td><input type=text name=fathername></td>";


    echo "</form>";
    echo "</table>";

    ?>

    </body>
    </html> 

2 个答案:

答案 0 :(得分:0)

您在字符串中遗漏了很多引号,您可以使用\"来回显双引号而不关闭字符串,例如:

echo "<form action=\"modify.php\" method=\"post\">";

此外,您不应该使用不带引号的数组索引,但从技术上讲,这将起作用,

"SET fathername='$_POST[fathername]'" 

它依赖于如果php无法找到常量并将产生警告,那么php将回退到字符串:

而是将它连接起来:

"SET fathername=".$_POST['fathername']."...."

您的代码中可能存在一些SQL注入漏洞,您应该阅读它。并使用mysqli_*而不是mysql_*,因为它已被折旧

答案 1 :(得分:0)

您现在只获得身份证

$query = "SELECT * FROM `member` WHERE `id` =$id"; // will give you all the rows