Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 数组推送替换或擦除现有值_Php_Mysql_Sql_Arrays_Array Push - Fatal编程技术网

Php 数组推送替换或擦除现有值

Php 数组推送替换或擦除现有值,php,mysql,sql,arrays,array-push,Php,Mysql,Sql,Arrays,Array Push,我这里的代码有问题。。。基本上,我有一个页面(edit_patient.php),它从另一个页面(new_patient.php)接收(获取)一个数字。。。在edit_patient.php页面上,我想编辑或添加新信息到已经从new_patient.php发布的内容中。。。 问题是,我有一个数组,我想推动旧数组向现有数组添加新值。。。 当我发布时,信息要么被替换,要么被删除。。。 这是我的密码: <?php // Update the database with new informati

我这里的代码有问题。。。基本上,我有一个页面(edit_patient.php),它从另一个页面(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>
现在,如果我更新阵列,我会得到以下结果:

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

您将使用此值替换数组值,而不是添加到其中。首先将您的值从MySQL构建到一个数组中,然后将整个数组推送到$\u POST值中。嗯,我应该从sql中分解,然后使用array\u push($medicing,inclode…)?您不必分解任何内容。您已经从数据库中获取了一个数组。构建一个数组,将$\u POST['mediates']值添加到该数组中,然后用新数组覆盖$\u POST['mediates']。好的,我想我明白了。。。我会试试的B我会让你知道的!!!多谢各位VikingBlooded@VikingBlooded我在代码中添加了一些更改,但仍然出现一些错误。。。
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)