views:

167

answers:

1

I have a page which should show a form with checkboxes, post back if it is checked or not, store the result in the database, and then on next loading of the page show the checkbox as checked, and update it as unchecked as necessary.

I understand my solution having the floating "checked" is not ideal, but it works fine, and I want to stick with it.

The problem I am having is that while a record may be inserted correctly into the database, it is both not showing as checked upon reloading the page, and is not updating 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 have trimmed this down as much as possible, without being sure where the cause lies.

A: 

Hi there,

If I've understood your question right replacing

$checkboxes = $_POST['checkboxes'];

with

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

I guess will solve your problem. (I did not run the script to test but hopefully will be bug free)

Full 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>";
}
?>
Iman Samizadeh
Why did the line you change solve the problem? Also, the update branch is still not funtional
Joshxtothe4
Because no instant array value was assigned when there was no $_POST array accomplished. Also you might check your "if (isset($_GET["cmd"])) $cmd = $_GET["cmd"]; else if (isset($_POST["cmd"])) $cmd = $_POST["cmd"]; else die("Invalid URL"); " validation again!
Iman Samizadeh