<?
$sql="select * from table";

while($row=mysql_fetch_array($sql))
{
        $name=$row['name'];
	$view=$row['view'];
	$view1=$row['view1'];
        $id2=$row['id'];
?>
<form name="myform" action="list.php?id=<?=$id2;?>&view=<?=$view;?>" method="post">
 <td align="center" width="5%"><?=$name;?></td>
 <td align="center" width="5%"><?=$view;?></td>
<td align="center" width="3%"><input type="text" value="<?=$view1; ?>" size="5" id="newva" name="newva"> </td>
</form>
 <td align="center" width="5%">
<a href="/show/list?id=<?=$id2;?>&view=<?=$view;?>" onClick="document.myform.submit();" >Submit</a>
</td>

<? } ?>

It shows out put like
name view view1 link
xxx 4 4 (in text box) Submit
yyy 5 5(in text box) Submit
zzz 6 6(in text box) Submit

I want to edit value for view1 and when click on link submit it saves
edited value in table(mysql).

How to do that??

Recommended Answers

All 5 Replies

<?
$rs = mysql_query("select * from test where id = '".$_GET['id']."'");///////////////////you have to send this $_GET['id'] using any GET method for this page
while($row=mysql_fetch_array($rs))
{
        $name=$row['name'];
        $id2=$row['id'];
?>
<form name="myform" action="list.php" method="post">
 <td align="center" width="5%"><input type="text" value="<? echo $name; ?>" name="name" /></td>
 <input type="hidden" name="up_id" value="<? echo $id2;?>"/>
 <td align="center" width="5%"><input type="submit" value="update"/></td>
</form>
<? }
if(isset($_POST[up_id))
{
	$query = mysql_query("update table set name = '".$_POST['name']."' where id = '".$_POST['up_id']."'")
	echo "Record has been Updated Successfully...........";
	exit;
}


?>
Member Avatar for diafol

You're already using js for submitting, why not use ajax?

IN THE FORM PAGE HEAD SECTION:

<script src="/path/to/prototype.js" type="text/javascript"></script>
<script src="/path/to/myAjax.js" type="text/javascript"></script>

YOUR FORM:

<input id="names" name="names" type="text" /> <a href="#" onclick="update_db('names');return false;">Update</a> 
<input id="view" name="view" type="text" /> <a href="#" onclick="update_db('view');return false;">Update</a> 
<input id="view1" name="view1" type="text" /> <a href="#" onclick="update_db('view1');return false;">Update</a> 
<hidden id="myId" name="myId" value="<?=$id2;?>" />

IN YOUR myAjax.js FORM:

function update_db(updateField){
  var sField = updateField;
  var sVal = $F(updateField);
  var sId = $F('myId');
  var pars = "uf=" + sField + "&id=" + sId + "&value=" + sVal;
  var phpupdater = "includes/formupdater.php"; 
  var oAjax = new Ajax.Request(phpupdater,{method: 'post',parameters: pars,onSuccess: function(transport){
      alert("Success!"); 
    }});
}

In YOUR formupdater.php FILE:

$id = $_POST['id'];$f = $_POST['uf'];$val = $_POST['value'];

$sql ="UPDATE yourtable SET {$f} = '{$val}' WHERE user_id = '{$id}'"; 

(then connect and update your usual way)

Normally I'd escape and validate all data (client side and server side), but moitted for brevity. Prototype library can be downloaded with Scriptaculous. Note this code has not been run - off top of my head. You'll probably need to debug.

thats more good, will you suggest me some useful links to learn AJAX with PHP?

Member Avatar for diafol

For myAjax.js - that's the code you need to put in it:


IN YOUR myAjax.js FORM (sorry FORM should be FILE):

function update_db(updateField){
  var sField = updateField;
  var sVal = $F(updateField);
  var sId = $F('myId');
  var pars = "uf=" + sField + "&id=" + sId + "&value=" + sVal;
  var phpupdater = "includes/formupdater.php"; 
  var oAjax = new Ajax.Request(phpupdater,{method: 'post',parameters: pars,onSuccess: function(transport){
      alert("Success!"); 
    }});
}

The prototype.js file can be downloaded from the scriptaculous site or from http://www.prototypejs.org.

As to where you can learn Ajax:
I bought a few books on the subject (Beginning AJAX and Professional AJAX from WROX). If you Google Ajax tutorials, I'm sure you'll find something useful. However, if (like me) your JS is a bit dodgy, use prototype or another framework to take the sting out of creating ajax objects.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.