Maintiaing status issues with mysql and php

I have a page that should display a checkbox form, submit it back if it is checked or not, save the result to the database and then on next page load, check the checkbox as noted and update it as unchecked if needed.

I realize my floating "checked" solution is not ideal, but it works great and I want to stick with it.

The problem I am facing is that while the record may be inserted correctly into the database, it does not appear as checked on page reload and does not update the records correctly.

Here is my code:

<?php
error_reporting(E_ALL);
if (isset($_GET["cmd"]))
  $cmd = $_GET["cmd"]; else
if (isset($_POST["cmd"]))
  $cmd = $_POST["cmd"]; else
die("Invalid URL");
if (isset($_GET["pk"])) {
    $pk = $_GET["pk"];
}
$checkDeleted = "";
$checkNotice = "";
$checkWarrant = "";
$checkPromise = "";
$checkCompensation = "";
$checkBond = "";
$checkInjunction = "";
$checkActionpay = "";
$checkActionorder = "";
$checkCourtpayment = "";
$checkForeclosure = "";
$checkPenalty = "";
$checkboxes = $_POST['checkboxes'];
if (in_array('deleted', $checkboxes))
  $checkDeleted = 'checked';
$con = mysqli_connect("localhost","user","pass", "db");
if (!$con) {
    echo "Can't connect to MySQL Server. Errorcode: %s\n". mysqli_connect_error();
    exit;
}
$con->set_charset("utf8");
$getformdata = $con->query("select ARTICLE_NO, deleted from STATUS where ARTICLE_NO = '$pk'");
while ($row = mysqli_fetch_assoc($getformdata)) {
    $ARTICLE_NO = $row['ARTICLE_NO'];
    $checkDeleted = $row['deleted'];
}
if($cmd=="submitinfo") {
    if ($ARTICLE_NO == null) {
        $statusQuery = "INSERT INTO STATUS VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        if ($statusInfo = $con->prepare($statusQuery)) {
            $statusInfo->bind_param("sssssssssssss", $pk, $checkDeleted, $checkNotice, $checkWarrant, $checkPromise, $checkCompensation, $checkBond, $checkInjunction, $checkActionpay, $checkActionorder, $checkCourtpayment, $checkForeclosure, $checkPenalty);
            $statusInfo->execute();
            $statusInfo->close();
            echo $pk;
            echo $checkDeleted;
        } else {
            print_r($con->error);
        }
    } else if ($ARTICLE_NO == $pk) {
        $statusQuery = "UPDATE STATUS SET deleted = ?, notice = ?, warrant = ?, promise = ?, compensation = ?, bond = ?, injunction = ?, actionpay = ?, actionorder = ?, courtpayment = ?, foreclosure = ?,penalty = ? WHERE ARTICLE_NO = ?";
        if ($statusInfo = $con->prepare($statusQuery)) {
            $statusInfo->bind_param("sssssssssssss", $checkDeleted, $checkNotice, $checkWarrant, $checkPromise, $checkCompensation, $checkBond, $checkInjunction, $checkActionpay, $checkActionorder, $checkCourtpayment, $checkForeclosure, $checkPenalty, $pk);
            $statusInfo->execute();
            $statusInfo->close();
        } else {
            print_r($con->error);
        }
    }
}
if($cmd=="EditStatusData") {
    echo '<link rel="stylesheet" href="style.css" type="text/css" />' . "\n";
    echo "<form name=\"statusForm\" action=\"test.php?pk=".$pk."\" method=\"post\" enctype=\"multipart/form-data\">
<h1>Editing information for Auction No: ".$pk."</h1>
<input type=\"checkbox\" name=\"checkboxes[]\" value=\"deleted\" ".$checkDeleted." />
<label for=\"deleted\">Löschung Ebay</label>
<br />
<input type=\"hidden\" name=\"cmd\" value=\"submitinfo\" />
<input name=\"Submit\" type=\"submit\" value=\"submit\" />
</form>";
}

      

I trimmed this as much as possible without knowing where the reason is.

0


a source to share


1 answer


If I understand your question correctly, replacing

$checkboxes = $_POST['checkboxes'];

from

$checkboxes = (isset($_POST['checkboxes'])? $_POST['checkboxes'] : array());



I think will solve your problem. (I haven't run the script to test, but hopefully it will be error free)

Complete example:

<?php
error_reporting(E_ALL);

if (isset($_GET["cmd"]))
  $cmd = $_GET["cmd"]; 
    else
if (isset($_POST["cmd"]))
  $cmd = $_POST["cmd"]; 
        else die("Invalid URL");

if (isset($_GET["pk"])) { $pk = $_GET["pk"]; }

$checkDeleted = "";
$checkNotice = "";
$checkWarrant = "";
$checkPromise = "";
$checkCompensation = "";
$checkBond = "";
$checkInjunction = "";
$checkActionpay = "";
$checkActionorder = "";
$checkCourtpayment = "";
$checkForeclosure = "";
$checkPenalty = "";

$checkboxes = (isset($_POST['checkboxes'])? $_POST['checkboxes'] : array());

if (in_array('deleted', $checkboxes)) $checkDeleted = 'checked';

$con = mysqli_connect("localhost","user","pass", "db");
if (!$con) { echo "Can't connect to MySQL Server. Errorcode: %s\n". mysqli_connect_error(); exit; }

$con->set_charset("utf8");

$getformdata = $con->query("select ARTICLE_NO, deleted from STATUS where ARTICLE_NO = '$pk'");

while ($row = mysqli_fetch_assoc($getformdata)) {
    $ARTICLE_NO = $row['ARTICLE_NO'];
    $checkDeleted = $row['deleted'];
}

if($cmd=="submitinfo") {
    if ($ARTICLE_NO == null) {
       $statusQuery = "INSERT INTO STATUS VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        if ($statusInfo = $con->prepare($statusQuery)) {
                $statusInfo->bind_param("sssssssssssss", $pk, $checkDeleted, $checkNotice, $checkWarrant, $checkPromise, $checkCompensation, $checkBond, $checkInjunction, $checkActionpay, $checkActionorder, $checkCourtpayment, $checkForeclosure, $checkPenalty);
                $statusInfo->execute();
                $statusInfo->close();
                echo $pk;
                echo $checkDeleted;
        } else {
                print_r($con->error);
        }
    } else if ($ARTICLE_NO == $pk) {
        $statusQuery = "UPDATE STATUS SET deleted = ?, notice = ?, warrant = ?, promise = ?, compensation = ?, bond = ?, injunction = ?, actionpay = ?, actionorder = ?, courtpayment = ?, foreclosure = ?,penalty = ? WHERE ARTICLE_NO = ?";
        if ($statusInfo = $con->prepare($statusQuery)) {
                $statusInfo->bind_param("sssssssssssss", $checkDeleted, $checkNotice, $checkWarrant, $checkPromise, $checkCompensation, $checkBond, $checkInjunction, $checkActionpay, $checkActionorder, $checkCourtpayment, $checkForeclosure, $checkPenalty, $pk);
                $statusInfo->execute();
                $statusInfo->close();
        } else {
                print_r($con->error);
     }  
    }
}
if($cmd=="EditStatusData") {
    echo '<link rel="stylesheet" href="style.css" type="text/css" />' . "\n";
    echo "<form name=\"statusForm\" action=\"test.php?pk=".$pk."\" method=\"post\" enctype=\"multipart/form-data\">
            <h1>Editing information for Auction No: ".$pk."</h1>
                <input type=\"checkbox\" name=\"checkboxes[]\" value=\"deleted\" ".$checkDeleted." />
                <label for=\"deleted\">Löschung Ebay</label>
                <br />
                <input type=\"hidden\" name=\"cmd\" value=\"submitinfo\" />
                <input name=\"Submit\" type=\"submit\" value=\"submit\" />
        </form>";
}
?>

      

0


a source







All Articles