Jump to content

[SOLVED] Can not update


zed420

Recommended Posts

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


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>            
<?
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.