zed420 Posted November 19, 2008 Share Posted November 19, 2008 Hi All Can anyone please tell me why doesn't my code UPDATE, it empties the field instead of updating. Any thoughts anyone. if($_POST['edit']) { foreach($_POST as $id) { $query = "SELECT * FROM news WHERE id = '$id'"; $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); if (mysql_num_rows($result) > 0) { $row = mysql_fetch_array($result); $query = "UPDATE news SET news='$news' WHERE id = '$id'"; $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); ?> <form action="<?php echo $PHP_SELF ?>" method="POST"> <center> <table width="100%" border="0"> <tr> <td> </td> <td >News</td> </tr> <tr> <td> </td> <td> <textarea name="news" cols="55" rows="15"><?php echo $row["news"]; ?></textarea> </td> </tr> <tr> <td> </td> <td ><input name="amend" type="Submit" value="Amend" onclick="return confirm ('You are about to change this record, Are you sure you want to do this?');"/></td> </tr> <tr><td> <center></center> </td> </table> </center> </form> <?php //Otherwise no rows found } // end if $rowsFound body else echo "No rows found"; } }else if($_POST['delete']) { foreach($_POST as $id) { // loop through the checked checkboxes mysql_query("DELETE FROM news WHERE id='$id'"); // deletes the record from the database if (mysql_affected_rows() > 0) { // execute query print "<font color=red size=2>No. = $id has been deleted</font><p>"; } } }else{ $query = "SELECT * FROM news ORDER BY id"; $result = mysql_query($query)or die(mysql_error()); ?> <div class="smallerText"> <form name="action" id="action" method="post" action="<?=$_SERVER['PHP_SELF']?>"> <TABLE BORDER=1 WIDTH=100% CELLSPACING=3 CELLPADDING=3 ALIGN=CENTER bgcolor="#F7956A"> <TR> <td width="1%" bgcolor="#F0C9BF"><font color=red><b>ID</b></font></TD> <td width="100%" bgcolor="#F0C9BF"><font color=red><b>News</b></font></TD> <td width="1%" bgcolor="#F0C9BF" ><font color=red></font></TD> </tr> <? while ($row = mysql_fetch_array($result)) { extract($row); echo "<tr > <td>" . $row['id'] . "</td> <td>" . $row['news'] . "</td> <td>" ?> <input type="checkbox" name="<?=$row[id]?>" id="<?=$row[id]?>" value="<?=$row[id]?>"/> <? "</TD> </tr>"; } ?></table> <br><center> <input type="submit" name="edit" id="edit" value="Edit Selected" /> <input type="submit" name="delete" id="delete" value="Delete Selected" onClick="return confirm('Are you sure, you want to Delete this record?');"/> </center> </form></div> <? } ?> Thanks Zed Link to comment https://forums.phpfreaks.com/topic/133407-solved-can-not-update/ Share on other sites More sharing options...
ratcateme Posted November 19, 2008 Share Posted November 19, 2008 should this $query = "UPDATE news SET news='$news' WHERE id = '$id'"; be $query = "UPDATE news SET news='{$row['news']}' WHERE id = '$id'"; Scott. Link to comment https://forums.phpfreaks.com/topic/133407-solved-can-not-update/#findComment-693857 Share on other sites More sharing options...
zed420 Posted November 20, 2008 Author Share Posted November 20, 2008 thanks for the reply Scott but that didn't work. ??? Zed Link to comment https://forums.phpfreaks.com/topic/133407-solved-can-not-update/#findComment-694125 Share on other sites More sharing options...
zenag Posted November 20, 2008 Share Posted November 20, 2008 just verify it..... if($_POST['amend']){ $id=$_POST["id"]; $news=$_POST["news"]; $query = "UPDATE news SET news='$news' WHERE id = '$id'"; echo $query; $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); } if($_POST['edit']) { foreach($_POST as $id) { $query = "SELECT * FROM news WHERE id = '$id'"; $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); if (mysql_num_rows($result) > 0) { $row = mysql_fetch_array($result); ?> <form action="<?php echo $PHP_SELF ?>" method="POST"> <input type="text" name="id" value="<?php echo $row["id"]?>"> <center> <table width="100%" border="0"> <tr> <td> </td> <td >News</td> </tr> <tr> <td> </td> <td> <textarea name="news" cols="55" rows="15"><?php echo $row["news"]; ?></textarea> </td> </tr> <tr> <td> </td> <td ><input name="amend" type="Submit" value="Amend" onclick="return confirm ('You are about to change this record, Are you sure you want to do this?');"/></td> </tr> <tr><td> <center></center> </td> </table> </center> </form> <?php //Otherwise no rows found } // end if $rowsFound body else echo "No rows found"; } }else if($_POST['delete']) { foreach($_POST as $id) { // loop through the checked checkboxes mysql_query("DELETE FROM news WHERE id='$id'"); // deletes the record from the database if (mysql_affected_rows() > 0) { // execute query print "<font color=red size=2>No. = $id has been deleted</font><p>"; } } }else{ $query = "SELECT * FROM news ORDER BY id"; $result = mysql_query($query)or die(mysql_error()); ?> <div class="smallerText"> <form name="action" id="action" method="post" action="<?=$_SERVER['PHP_SELF']?>"> <TABLE BORDER=1 WIDTH=100% CELLSPACING=3 CELLPADDING=3 ALIGN=CENTER bgcolor="#F7956A"> <TR> <td width="1%" bgcolor="#F0C9BF"><font color=red><b>ID</b></font></TD> <td width="100%" bgcolor="#F0C9BF"><font color=red><b>News</b></font></TD> <td width="1%" bgcolor="#F0C9BF" ><font color=red></font></TD> </tr> <? while ($row = mysql_fetch_array($result)) { extract($row); echo "<tr > <td>" . $row['id'] . "</td> <td>" . $row['news'] . "</td> <td>" ?> <input type="checkbox" name="<?=$row[id]?>" id="<?=$row[id]?>" value="<?=$row[id]?>"/> <? "</TD> </tr>"; } ?></table> <br><center> <input type="submit" name="edit" id="edit" value="Edit Selected" /> <input type="submit" name="delete" id="delete" value="Delete Selected" onClick="return confirm('Are you sure, you want to Delete this record?');"/> </center> </form></div> <? } ?> Link to comment https://forums.phpfreaks.com/topic/133407-solved-can-not-update/#findComment-694131 Share on other sites More sharing options...
zed420 Posted November 20, 2008 Author Share Posted November 20, 2008 Just as before, it empties the field instead of updating, when I'm checking in database id field is still there but news field is empty. Thanks Zed Link to comment https://forums.phpfreaks.com/topic/133407-solved-can-not-update/#findComment-694235 Share on other sites More sharing options...
zenag Posted November 20, 2008 Share Posted November 20, 2008 just copy the above code as it is and check it out...its working fine ... Link to comment https://forums.phpfreaks.com/topic/133407-solved-can-not-update/#findComment-694243 Share on other sites More sharing options...
zed420 Posted November 20, 2008 Author Share Posted November 20, 2008 zenag YOU ARE A STAR, What I fool I am not echoing out the $id. Thanks Zed Link to comment https://forums.phpfreaks.com/topic/133407-solved-can-not-update/#findComment-694286 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.