lundi 5 juin 2017

How to pass an array as a form value?

I'm dealing with the issue of comparing an array of columns: [Date], [form_name], [I/O#] and [facility] column; with an index of checkbox array in my SQL query. Here's the php code where all the problem comes from:

$sql_query = "SELECT [Date], [form_name], [I/O#],[facility] FROM [$connectionInfo[Database]].[dbo].[form_record] WHERE [lock]='1' AND [facility] != 'ModeTemplate' ORDER BY [Date]";
$sql_result = sqlsrv_query($connection_sql, $sql_query)
or die('error: '.str_replace("[Microsoft][SQL Server Native Client 10.0][SQL Server]", "", array_pop(array_pop(sqlsrv_errors()))).'<br/>query: '.$sql_query.'<br/>');
$infoArray = sqlsrv_fetch_array($sql_result, SQLSRV_FETCH_ASSOC);
if ($infoArray['Date'] != Null)
{
    echo "<table style = 'background:#FBF8EF;'> <tr><td>Choose</td> <td>Date</td> <td>Form</td> <td>I/O#</td> <td>Facility</td></tr>";
    echo "<tr><td><input type='checkbox' name='checkbox[]' value='".json_encode($infoArray)."'/></td>";
    $rDate = $infoArray['Date'];
    $date = $rDate->format('Y-m-d');
    echo "<td>".$date."</td>";
    echo "<td>".$infoArray['form_name']."</td>";
    echo "<td>".$infoArray['I/O#']."</td>";
    echo "<td>".$infoArray['facility']."</td></tr>";
}
while($infoArray = sqlsrv_fetch_array($sql_result, SQLSRV_FETCH_ASSOC))
{
    echo "<tr><td><input type='checkbox' name='checkbox[]' value='".json_encode($infoArray)."'/></td>";
    $rDate = $infoArray['Date'];
    $date = $rDate->format('Y-m-d');
    echo "<td>".$date."</td>";
    echo "<td>".$infoArray['form_name']."</td>";
    echo "<td>".$infoArray['I/O#']."</td>";
    echo "<td>".$infoArray['facility']."</td></tr>";

}
echo "</table>";


echo"</br><tr><td align='center'><input type='submit' name='delete' value='Unlock Record'/></td></tr>";


if(isset($_POST['delete']))
{
    if (is_array($_POST['checkbox'])) {
        $checkbox = $_POST['checkbox'];
        foreach($checkbox as $value)
        {
            $value = json_decode($value);
            $sql_query = "UPDATE [$connectionInfo[Database]].[dbo].[form_record] SET [lock]='0' WHERE [Date]='".$value['Date']."' AND [form_name]='".$value['form_name']."' AND [I/O#]='".$value['I/O#']."' AND [facility]='".$value['facility']."'";
            $sql_result = sqlsrv_query($connection_sql, $sql_query)
            or die('error: '.str_replace("[Microsoft][SQL Server Native Client 10.0][SQL Server]", "", array_pop(array_pop(sqlsrv_errors()))).'<br/>query: '.$sql_query.'<br/>');               
        }
    }

}

I get the following error:

PHP Warning: json_decode() expects parameter 1 to be string, array given in C:\Inetpub\wwwroot\form\unlock_logs.php on line 114 PHP Warning: json_decode() expects parameter 1 to be string, array given in C:\Inetpub\wwwroot\form\unlock_logs.php on line 114 PHP Warning: json_decode() expects parameter 1 to be string, array given in C:\Inetpub\wwwroot\form\unlock_logs.php on line 114

Can't I give to the checkbox an array of all the columns i.e. just like the $infoArray so that each checkbox can be uniquely identified?

Note that I can give the checkbox a value of one column but not of all columns just like in the code below, it works:

$sql_query = "SELECT [Date], [form_name], [I/O#],[facility] FROM [$connectionInfo[Database]].[dbo].[form_record] WHERE [lock]='1' AND [facility] != 'ModeTemplate' ORDER BY [Date]";
$sql_result = sqlsrv_query($connection_sql, $sql_query)
or die('error: '.str_replace("[Microsoft][SQL Server Native Client 10.0][SQL Server]", "", array_pop(array_pop(sqlsrv_errors()))).'<br/>query: '.$sql_query.'<br/>');
$infoArray = sqlsrv_fetch_array($sql_result, SQLSRV_FETCH_ASSOC);
if ($infoArray['Date'] != Null)
{
    echo "<table style = 'background:#FBF8EF;'> <tr><td>Choose</td> <td>Date</td> <td>Form</td> <td>I/O#</td> <td>Facility</td></tr>";
    echo "<tr><td><input type='checkbox' name='checkbox[]' value='".$infoArray['form_name']."'/></td>"; //change occurs here
    $rDate = $infoArray['Date'];
    $date = $rDate->format('Y-m-d');
    echo "<td>".$date."</td>";
    echo "<td>".$infoArray['form_name']."</td>";
    echo "<td>".$infoArray['I/O#']."</td>";
    echo "<td>".$infoArray['facility']."</td></tr>";
}
while($infoArray = sqlsrv_fetch_array($sql_result, SQLSRV_FETCH_ASSOC))
{
    echo "<tr><td><input type='checkbox' name='checkbox[]' value='".$infoArray['form_name']."'/></td>"; //change occurs here
    $rDate = $infoArray['Date'];
    $date = $rDate->format('Y-m-d');
    echo "<td>".$date."</td>";
    echo "<td>".$infoArray['form_name']."</td>";
    echo "<td>".$infoArray['I/O#']."</td>";
    echo "<td>".$infoArray['facility']."</td></tr>";

}
echo "</table>";


echo"</br><tr><td align='center'><input type='submit' name='delete' value='Unlock Record'/></td></tr>";


if(isset($_POST['delete']))
{
    if (is_array($_POST['checkbox'])) {
        $checkbox = $_POST['checkbox'];
        foreach($checkbox as $value)
        {
            $sql_query = "UPDATE [$connectionInfo[Database]].[dbo].[form_record] SET [lock]='0' WHERE [form_name]='".$value."'"; //change occurs here
            $sql_result = sqlsrv_query($connection_sql, $sql_query)
            or die('error: '.str_replace("[Microsoft][SQL Server Native Client 10.0][SQL Server]", "", array_pop(array_pop(sqlsrv_errors()))).'<br/>query: '.$sql_query.'<br/>');               
        }
    }

}

The problem as previously stated is that it deletes every row that has the same form_name which is definitely not what I want. What I want is to give the checkbox an array of columns as value so that each checkbox is uniquely identified. This is what I attempted but it didn't work out.




Aucun commentaire:

Enregistrer un commentaire