数组推送替换或删除现有值

时间:2014-07-08 16:37:08

标签: php mysql sql arrays array-push

我的代码在这里有问题...基本上,我有一个页面(edit_patient.php)从另一个页面接收(GET)一个数字(new_patient.php)...在edit_patient.php页面上我想要编辑或添加新信息到已经从new_patient.php发布的内容... 问题是,我有一个数组,我想推动旧数组为这个现有数组添加新值... 当我发布时,信息被替换或删除... 这是我的代码:

<?php
// Update the database with new information from form;
if (isset($_POST['dossier'])){
    $pid = mysqli_real_escape_string($connect,$_POST['thisID']);
    if (!empty($_POST['medication'])|| ($_POST['posology'])) {
        $thisArraysql = mysqli_query($connect, "SELECT medication, posology FROM patient WHERE dossier='$thisID' LIMIT 1");
        $medArray_count = mysqli_num_rows($thisArraysql);
        if ($medArray_count > 0) {      
            while ($row = mysqli_fetch_array($thisArraysql)) {
                    $old_medication = $row["medication"];
                    $old_posology = $row["posology"];
            }
            $medication .= array_push($old_medication, implode('-', $_POST['medication']));
            $posology .= array_push($old_posology, implode('-',$_POST['posology']));
        } else {
            $medication = implode('-', $_POST['medication']);
            $posology = implode('-', $_POST['posology']);
        }
    }

    $sql = "UPDATE patient  SET medication = '$medication', posology = '$posology' WHERE dossier = '$pid'";

    if (!mysqli_query($connect, $sql)) {
            printf("Error: %s\n", mysqli_error($connect));
        } else {
            header("location: patients_list.php");  
        }
    exit();
}
// Get pid from new_patient.php; Select the patient from database;
if (isset($_GET['pid'])){
    $targetID = $_GET['pid'];

    $sql = "SELECT * FROM patient WHERE dossier='$targetID' LIMIT 1";
    $query = mysqli_query($connect, $sql);

    $patientCount = mysqli_num_rows($query);
    if ($patientCount > 0){
        while($row = mysqli_fetch_array($query)){
            $id = $row["id"];
            $dossier = $row["dossier"];
            $medication = explode('-',$row['medication']);
                $medication_count = count($medication);
                $medication_count_added = $medication_count+1;
            $posology = explode('-',$row['posology']);
        }
    } else {

    echo "This patient does not exist!";

    exit();
    }
}
?>
<html>
<head>
<!-- this scripts adds more fields to the array -->
<script type="text/javascript">
$(document).ready(function() {

var MaxInputs          = 19-"<?php echo $medication_count; ?>"; //maximum input boxes allowed
var CurrentSituation    = $("#CurrentSituation"); //Input boxes wrapper ID
var AddButton              = $("#AddMoreFieldBox"); //Add button ID

var x = CurrentSituation.length; //initlal text box count
var FieldCount="<?php echo $medication_count_added; ?>"+1; //to keep track of text box added

$(AddButton).click(function (e)  //on add input button click
{
        if(x <= MaxInputs) //max input box allowed
        {
            FieldCount++; //text box added increment
            //add input box
            $(CurrentSituation).append('<tr class="field_added"><td><input type="text" name="medication['+ FieldCount +']" id="medication['+ FieldCount +']" size="40" /></td><td><input type="number" name="posology['+ FieldCount +']" id="posology['+ FieldCount +']" min="1" max="100000" /></td><td><a href="#" class="removeclass">Effacer</a></td></tr>');
            x++; //text box increment
        }
return false;
});

$("body").on("click",".removeclass", function(e){ //user click on remove text
        if( x > 1 ) {
                $(this).closest("tr").remove(); //remove text box
                x--; //decrement textbox
        }
return false;
}) 

});
</script>
</head>
<body>
<form action="edit_patient.php" enctype="multipart/form-data" method="post">
    Dossier Number: <input type="number" id="dossier" name="dossier" readonly value="<?php echo $dossier; ?>" />
    <table border="0" cellpadding="1" cellspacing="1">
        <tr>
            <td>Medication(s)</td>
            <td colspan="2">Posology(ies)</td>
        </tr>
        <?php 
        echo "<tr><td><table border=\"0\" cellpadding=\"1\" cellspacing=\"1\">";
        foreach ($medication as $key => $value) {
            echo "<tr><td>$value</td></tr>";            
        }
        echo "</table></td><td><table border=\"0\" cellpadding=\"1\" cellspacing=\"1\">";                   
        foreach ($posology as $key => $value) {
            echo "<tr><td>$value</td></tr>";
        }
        echo "</table></td></tr>";                  
        ?>
        <tr>
            <td><input type="text" name="medication[<?php echo $medication_count_added; ?>]" id="medication[<?php echo $medication_count_added; ?>]" size="40" /></td>
            <td><input type="number" name="posology[<?php echo $medication_count_added; ?>]" id="posology[<?php echo $medication_count_added; ?>]" min="1" max="100000" /></td>
        </tr>
    </table>
</form>
</body>
</html>

非常感谢你的帮助,时间,理解和奉献!

编辑:

我替换了数组的代码:

<?php
if (mysqli_num_rows($thisArraysql) < 0) {
    $medication = implode('-', $_POST['medication']);
    $posology = implode('-', $_POST['posology']);
} else {
    $medication = array();
    $posology = array();
    while ($row = mysqli_fetch_array($thisArraysql)) {
      $medication[] = $row["medication"];
      $posology[] = $row["posology"];
    }
     foreach ($_POST['medication'] as $key => $value) {
        $medication[$key] = $value;
    }
    foreach ($_POST['posology'] as $key => $value) {
        $posology[$key] = $value;
    }
}
?>

但是我仍然得到同样的错误......数组被替换为单词Array ...

编辑2

我创建了一个新患者(new_patient.php)并在数组中添加了一些信息。一旦进入edit_patient.php(这个我遇到问题的页面),我做了一个var_dump,这就是我得到的:

array (size=1)
  0 => string 'Med 1-Med 2-Med 3-Med 4-' (length=24)
array (size=1)
  0 => string '25-30-50-5-' (length=11)

现在,如果我更新数组,我得到这个:

array (size=1)
  0 => string 'Array' (length=5)
array (size=1)
  0 => string 'Array' (length=5)

0 个答案:

没有答案