2014-05-22 67 views
1

我需要更新頁面中的選定行。我有一個用戶輸入ID的網頁。在第二頁中輸入的所有行和具有相同ID的值將被顯示用於編輯。我怎樣才能做到這一點..更新php中的選定行

下面是代碼

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

唯一的ID,而不是PDO? – Cylindric

+0

首先更正你的mysql連接代碼。 – Sadikhasan

回答

0

你錯過了在你的字符串了很多報價,你可以使用\"呼應雙引號,但不關閉字符串如:

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

而且你不應該使用不帶引號的數組索引,雖然在技術上這將工作,

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

它依賴於一個事實,即PHP將回落到一個字符串,如果它不能找到常數,會產生警告:

相反串連這樣的:

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

你可能有一些SQL注入你的代碼中的漏洞,你應該閱讀。並使用mysqli_*mysql_*,因爲它是貶值

0

你爲什麼要使用舊的`mysql_ *`功能越來越現在

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