Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

The code of update multiple page is given below

    <title>Update Multiple Data Form</title>
<?php
include ('config.php');
$tbl_name="user";
$id=$_POST['id'];
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$mobile=$_POST['mobile'];
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<table width="300" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr> 
<td>
<table width="300" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>First Name</strong></td>
<td align="center"><strong>Last Name</strong></td>
<td align="center"><strong>Email-Id</strong></td>
<td align="center"><strong>Mobile No</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center">
<input name="id[]" type="text" id="id" value="<?php echo $rows['id']; ?>">
</td>
<td align="center">
<input name="firstname[]" type="text" id="firstname" value="<?php echo $rows['firstname']; ?>">
</td>
<td align="center">
<input name="lastname[]" type="text" id="lastname" value="<?php echo $rows['lastname']; ?>">
</td>
<td align="center">
<input name="email[]" type="text" id="email" value="<?php echo $rows['email']; ?>">
</td>
<td align="center">
<input name="mobile[]" type="text" id="mobile" value="<?php echo $rows['mobile']; ?>">
</td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
$id=$_POST['id'];
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$mobile=$_POST['mobile'];
if(isset($_POST['submit'])){
for($i=0;$i<$count;$i++){
$sql1="UPDATE $tbl_name SET firstname='$firstname', lastname='$lastname', email='$email', mobile='$mobile' WHERE id='$id'";
$result1=mysql_query($sql1);
}
}
if($result1){
}
mysql_close();
?>
<a href="list.php">Click here to List Data</a>

The values i am giving is not updating,Once i submit with the values the page is just reloading not doing anything than that. I don't know whether where i made the actual mistake, The error was not in giving action to the page i guess.

I have crossed through this page PHP MySQL Update Set query with Multiple columns and few pages. It didn't solve my problem.

share|improve this question
    
echo the value of $sql1. Are they valid queries? –  Andy Mar 12 at 5:01
    
What is the error? –  user2486495 Mar 12 at 5:02
    
the query is valid if we gave the alternate values of the $value in phpmyadmin –  user3408979 Mar 12 at 5:02
    
@User2486495 The page just reloads and does nothing –  user3408979 Mar 12 at 5:03
    
how about your config.php, is it containing the correct DB information? –  Yohanes Khosiawan 许先汉 Mar 12 at 5:04

4 Answers 4

up vote 0 down vote accepted

You have made a small mistake in while passing the values [$i] value in all the parameter.

$sql1="UPDATE $tbl_name SET firstname='$firstname[$i]', lastname='$lastname[$i]', email='$email[$i]', mobile='$mobile[$i]' WHERE id='$id[$i]'";
share|improve this answer
    
thanks, now it is working –  user3408979 Mar 12 at 5:16

Your $_POST['id'],$_POST['firstname'], etc.. are array not a single value, Please print POST request you will get clear picture. Use following sql:

$sql_update ="UPDATE $tbl_name SET firstname='$firstname[$i]', lastname='$lastname[$i]',email='$email[$i]', mobile='$mobile[$i]' WHERE id='$id[$i]'";

Also from where you are getting count value? Its has to posted via form using any hidden field.

share|improve this answer

Try this. You sql string is wrongly using single quotes.

for($i=0;$i<$count;$i++){
$sql1 =<<<QRY
UPDATE {$tbl_name} SET firstname="{$firstname[$i]}", lastname="{$lastname[$i]}", email="{$email[$i]}", mobile="{$mobile[$i]}" WHERE id="{$id[$i]}"
QRY;

$result1=mysql_query($sql1);
}
share|improve this answer

First of all you should not have the same id for different input fields. (e.g)

<input name="id[]" type="text" id="id" value="<?php echo $rows['id']; ?>">

should be changed this id.

Try like this:

if(isset($_POST['submit'])){
  for($i=0;$i<$count;$i++){
    $id=$_POST['id'][$i];
    $firstname=$_POST['firstname'][$i];
    $lastname=$_POST['lastname'][$i];
    $email=$_POST['email'][$i];
    $mobile=$_POST['mobile'][$i];   
    $sql1="UPDATE $tbl_name SET firstname='$firstname', lastname='$lastname', email='$email', mobile='$mobile' WHERE id='$id'";
    $result1=mysql_query($sql1);
      if(!$result1){
          die("can not updated: ".mysql_error());
      }
    }
}

mysql_close();
?>

You also should not use mysql_* since all mysql_* functions are deprecated.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.