如何在php mysql中更新多个表行数组

时间:2015-08-19 07:20:07

标签: php mysql sql-update multiple-tables

它会显示记录,但是当我保存它时。它不起作用。请检查我的数组查询。

<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>";

3 个答案:

答案 0 :(得分:0)

请勿使用'famcomp'使用famcomp

同样,raveenanigam说你没有使用$_POST["app_id[$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[$i]"]."'") 
or die(mysql_error()); // Run the Mysql update query inside for loop

修改:试试这个

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";

答案 1 :(得分:0)

删除表名称的引号,并使用foreach循环数据。

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]"]."' WHERE {APP_ID {1}}

答案 2 :(得分:0)

试试这个

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

将此添加到您的php

{{1}}