-1

it display the record but when I save it. it doesnt work. please have a check on my array query.

<form action="saveupdaterecord.php" class="form-horizontal" role="form" method="post">   
    <table>
                  <tr>
                     <?php
            $result = $db->prepare("SELECT * FROM famcomp WHERE app_id='".     mysql_real_escape_string($app_id) ."'");
            $result->execute();
            for($i=0; $row = $result->fetch(); $i++){
          ?>
                    <td><input type="hidden" name="app_id[]" value="<?php echo $row['app_id']; ?>" /></td>
                    <td><input type="text" name="fullname[]" value="<?php echo $row['fullname']; ?>" class="input" /></td>
                    <td><input type="text" name="fage[]" value="<?php echo $row['fage']; ?>" class="input" /></td>
                    <td><input type="text" name="frel[]" value="<?php echo $row['frel']; ?>" class="input" /></td>
                    <td><input type="text" name="fcivil[]" value="<?php echo $row['fcivil']; ?>" class="input" /></td>
                    <td><input type="text" name="fedu[]" value="<?php echo $row['fedu']; ?>" class="input" /></td>
                    <td><input type="text" name="foccup[]" value="<?php echo $row['foccup']; ?>" class="input" /></td>
                    <td><input type="text" name="finco[]" value="<?php echo $row['finco']; ?>" class="input" /></td>
                </tr>    
                <?php
                    }
                ?>
    <br></table></form>

saveupdaterecord.php

$fullname=$_POST['fullname'];
$N = count($fullname);
for($i=0; $i < $N; $i++)
mysql_query("UPDATE 'famcomp' SET 'fullname' = '".$_POST["fullname[$i]"]."', fage = '".$_POST["fage[$i]"]."', frel = '".$_POST["frel[$i]"]."', fcivil = '".$_POST["fcivil[$i]"]."', fedu = '".$_POST["fedu[$i]"]."', foccup = '".$_POST["foccup[$i]"]."', finco = '".$_POST["finco[$i]"]."' WHERE `app_id` = '".$_POST["app_id"]."'"); // Run the Mysql update query inside for loop
$message = 'Success Updating the record!!';
echo "<SCRIPT>alert('$message');</SCRIPT>";
echo "<script>windows: location='editrecord.php?name=$a'</script>";
kim de castro
  • 299
  • 6
  • 19

3 Answers3

0

Dont use 'famcomp' use famcomp

Also as raveenanigam said you havent used $_POST["app_id[$i]"]. If still gets the problem, try using

     mysql_query("UPDATE famcomp SET 
     fullname = '".$_POST["fullname[$i]"]."', fage = '".$_POST["fage[$i]"]."',
     frel = '".$_POST["frel[$i]"]."', fcivil = '".$_POST["fcivil[$i]"]."', 
fedu = '".$_POST["fedu[$i]"]."', foccup = '".$_POST["foccup[$i]"]."',
     finco = '".$_POST["finco[$i]"]."'
     WHERE `app_id` = '".$_POST["app_id[$i]"]."'") 
or die(mysql_error()); // Run the Mysql update query inside for loop

Edit : Try this

if(isset($_POST['fullname']))
{
    $fullname = $_POST['fullname'];
    echo "Got the Fullname";
    foreach( $fullname as $key => $n ) {
        mysql_query("UPDATE famcomp SET 'fullname' = '".$_POST[fullname[$key]]."', fage = '".$_POST["fage[$key]"]."', frel = '".$_POST["frel[$key]"]."', fcivil = '".$_POST["fcivil[$key]"]."', fedu = '".$_POST["fedu[$key]"]."', foccup = '".$_POST["foccup[$key]"]."', finco = '".$_POST["finco[$key]"]."' WHERE app_id= '".$_POST["app_id[$key]"]."'") or die(mysql_error());

        $message = 'Success Updating the record for ' . $n;
    }
}
else
    echo "Cannot get Fullname";
Harshit
  • 5,147
  • 9
  • 46
  • 93
0

Remove quotes to table name and Use foreach for looping the data.

foreach($fullname as $key){ mysql_query("UPDATE famcomp SET 'fullname' = '".$_POST[fullname[$key]]."', fage = '".$_POST["fage[$key]"]."', frel = '".$_POST["frel[$key]"]."', fcivil = '".$_POST["fcivil[$key]"]."', fedu = '".$_POST["fedu[$key]"]."', foccup = '".$_POST["foccup[$key]"]."', finco = '".$_POST["finco[$key]"]."' WHEREapp_id= '".$_POST["app_id"]."'"); }

Suresh
  • 923
  • 1
  • 10
  • 21
0

Try this

$fullname=$_POST['fullname'];
$N = count($fullname);
for($i = 0 ;$i< count($fullname); $i++) {
$query = "UPDATE famcomp SET app_id = '".$_POST["app_id[$i]"]."', fullname = '".$_POST["fullname[$i]"]."', fage = '".$_POST["fage[$i]"]."', frel = '".$_POST["frel[$i]"]."', fcivil = '".$_POST["fcivil[$i]"]."', fedu = '".$_POST["fedu[$i]"]."', foccup = '".$_POST["foccup[$i]"]."', finco = '".$_POST["finco[$i]"]."' WHERE app_id = '".$_POST["app_id[$i]"]."'"; // Run the Mysql update query inside for loop
mysql_query($query, $con);
$message = 'Success Updating the record!!';
echo "<SCRIPT>alert('$message');</SCRIPT>";
echo "<script>windows: location='editrecord.php?name=$a'</script>";
}

Add this to your php

<?php
$result = $db->prepare("SELECT * FROM famcomp WHERE app_id='".     mysql_real_escape_string($app_id) ."'");
$result->execute();
for($i=0; $row = $result->fetch(); $i++)
$con = mysql_connect("host","user","password","database"); //you can check this in priviledges. name of your database, if user has no password do not include password, delete "password", 
if (!$con){
die("Can't connect".mysql_error());
}
mysql_select_db("database",$con); //the name of your database
?>
Micaela
  • 132
  • 13
  • $fullname=$_POST['fullname']; $N = count($fullname); I'm having problem with this. undefined variable. i dont know why it doesnt get the fullname post. – kim de castro Aug 19 '15 at 08:50