Jump to content

Inserting a form containing checkbox array into mysql database


jvance38

Recommended Posts

So I've spent 2 days now searching the web on how to make my checkbox array on my form insert the value selected by the user into my database. Here is the code that I've written. All fields except for the checkbox array works fine.

 

<?php virtual('/budgets/Connections/connAdmin.php'); ?>

<?php

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{

  if (PHP_VERSION < 6) {

    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  }

 

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

 

  switch ($theType) {

    case "text":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;   

    case "long":

    case "int":

      $theValue = ($theValue != "") ? intval($theValue) : "NULL";

      break;

    case "double":

      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";

      break;

    case "date":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;

    case "defined":

      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

      break;

  }

  return $theValue;

}

}

 

$editFormAction = $_SERVER['PHP_SELF'];

if (isset($_SERVER['QUERY_STRING'])) {

  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);

}

 

 

  if (isset($_POST['submit'])) {

    $workStyle = $_POST['art_photos'];

 

    // Setting up a blank variable to be used in the coming loop.

    $allStyles = "";

 

    // For every checkbox value sent to the form.

    foreach ($workStyle as $style) {

      // Append a string called $allStyles with the current array element, and then add a comma and a space at the end.

      $allStyles .= $style . ", ";

    }

 

    // Delete the last two characters from the string.

    $allStyles = substr($allStyles, 0, -2);

 

    echo "<p>The resulting string is: <strong>$allStyles</strong></p>\r\n";

 

  }

 

 

 

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

  $insertSQL = sprintf("INSERT INTO daily_budget (rundate, slug, reporter, budgetInfo, notes, `section`, deadline, art_photos sidebar, sBudget) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",

                      GetSQLValueString($_POST['rundate'], "text"),

                      GetSQLValueString($_POST['slug'], "text"),

                      GetSQLValueString($_POST['reporter'], "text"),

                      GetSQLValueString($_POST['budgetInfo'], "text"),

                      GetSQLValueString($_POST['notes'], "text"),

                      GetSQLValueString($_POST['section'], "text"),

                      GetSQLValueString($_POST['deadline'], "text"),

  GetSQLValueString($_POST['art_photos'], "text"),

                      GetSQLValueString($_POST['sidebar'], "text"),

                      GetSQLValueString($_POST['sBudget'], "text"));

 

  mysql_select_db($database_connAdmin, $connAdmin);

  $Result1 = mysql_query($insertSQL, $connAdmin) or die(mysql_error());

}

 

?>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Budget Record</title>

 

</head>

 

<body>

<?php

if (isset($error)) {

echo '<ul>';

foreach($error as $alert) {

echo "<li class='warning'>$alert</li>\n";

}

echo '</ul>';

}

?>

<form action="<?php echo $editFormAction; ?>" method="POST" name="form1" id="form1">

  <table align="center" width="60%" border="2" bgcolor="#FFFFFF">

    <tr valign="baseline">

      <td width="14%" align="right" nowrap="nowrap"><STRONG>Rundate:</STRONG></td>

      <td width="86%">

        <input type="text" name="rundate" size="30" value="" id="jQueryUICalendar2"/>

        <script type="text/javascript">

// BeginWebWidget jQuery_UI_Calendar: jQueryUICalendar2

jQuery("#jQueryUICalendar2").datepicker();

 

// EndWebWidget jQuery_UI_Calendar: jQueryUICalendar2

              </script></td>

    </tr>

    <tr valign="baseline">

      <td nowrap="nowrap" align="right"><STRONG>Section:</STRONG></td>

      <td>

        <select name="section">

        <option value=""></option>

          <option value="A1">A1</option>

          <option value="Pickup All">Pickup All</option>

          <option value="Region">Region</option>

          <option value="Atlantic">Atlantic</option>

        <option value="Business">Business</option>

          <option value="Features">Features</option>

          <option value="Sports">Sports</option>

        </select>

    </td>

    </tr>

    <tr valign="baseline">

      <td nowrap="nowrap" align="right"><strong>Slug:</strong></td>

      <td><span id="sprytextfield2">

      <input type="text" name="slug" value="" size="32" />

      <span class="textfieldRequiredMsg">REQUIRED</span></span><span style="color: #F00; font-weight: bold; font-size: 10px;"></span></td>

    </tr>

    <tr valign="baseline">

      <td nowrap="nowrap" align="right"><strong>Budget Information:</strong></td>

      <td><span id="sprytextarea1">

        <textarea name="budgetInfo" cols="50" rows="4"></textarea>

<span class="textareaRequiredMsg">Required.</span></span><span style="color: #F00; font-weight: bold; font-size: 11px;"></span></td>

    </tr>

    <tr valign="baseline">

      <td nowrap="nowrap" align="right"><strong>Notes:</strong></td>

      <td><input type="text" name="notes" value="" size="32" /></td>

    </tr>

    <tr valign="baseline">

      <td nowrap="nowrap" align="right"><strong>Reporter:</strong></td>

      <td><span id="sprytextfield3">

        <input type="text" name="reporter" value="" size="32" />

      <span class="textfieldRequiredMsg">Required.</span></span><span style="color: #F00; font-weight: bold; font-size: 10px;"></span></td>

    </tr>

    <tr valign="baseline">

      <td nowrap="nowrap" align="right"><strong>Deadline:</strong></td>

      <td><input type="text" name="deadline" size="30" value="Enter date" id="jQueryUICalendar1"/><script type="text/javascript">

// BeginWebWidget jQuery_UI_Calendar: jQueryUICalendar2

jQuery("#jQueryUICalendar1").datepicker();

 

// EndWebWidget jQuery_UI_Calendar: jQueryUICalendar2

              </script> </td>

    </tr>

    <tr valign="baseline">

      <td nowrap="nowrap" align="right"><strong>Artwork:</strong></td>

      <td><label>

          <input type="checkbox" name="art_photos[]" value="factbox" id="art_photos_0"  />

          Factbox</label>

       

        <label>

          <input type="checkbox" name="art_photos[]" value="graphic" id="art_photos_1" />

          Graphic</label>

     

        <label>

          <input type="checkbox" name="art_photos[]" value="photo" id="art_photos_2" />

          Photo</label>

       

        <label>

          <input type="checkbox" name="art_photos[]" value="video" id="art_photos_3"  />

          Video</label>

      <br /></td>

    </tr>

        <tr>

       

    </tr>

      <tr valign="baseline">

      <td nowrap="nowrap" align="right">      <strong>Sidebar:</strong></td>

      <td valign="baseline"><span id="spryradio2">

        <label>

          <input type="radio" name="sidebar" value="y" id="sidebar_0" />

          Yes</label>

        <br />

        <label>

          <input type="radio" name="sidebar" value="n" id="sidebar_1" />

          No</label>

        <br />

        <span class="radioRequiredMsg"><strong style="font-size: 11px">REQUIRED.</strong></span></span>

       

    </tr>

    <tr valign="baseline">

      <td nowrap="nowrap" align="right"><strong>Sidebar Information:</strong></td>

      <td><input type="text" name="sBudget" value="" size="32" /></td>

    </tr>

    <tr valign="baseline">

      <td nowrap="nowrap" align="right"> </td>

      <td><input type="submit" value="Insert record" /></td>

    </tr>

  </table>

  <input type="hidden" name="MM_insert" value="form1" />

</form>

 

<p> </p>

<h3><a href="/new_budget/admin/manage_budgets.php"><strong>View your budget entry, make changes or delete your budget.</strong></a></h3>

<script type="text/javascript">

<!--

jQuery("#jQueryUICalendar1").datepicker();

jQuery("#jQueryUICalendar2").datepicker();

var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2", "none", {maxChars:15, validateOn:["change"]});

var sprytextarea1 = new Spry.Widget.ValidationTextarea("sprytextarea1", {validateOn:["change"]});

var sprytextfield3 = new Spry.Widget.ValidationTextField("sprytextfield3");

var spryradio2 = new Spry.Widget.ValidationRadio("spryradio2");

//-->

</script>

</body>

</html>

So I've spent 2 days now searching the web on how to make my checkbox array on my form insert the value selected by the user into my database. Here is the code that I've written. All fields except for the checkbox array works fine.

 

<?php virtual('/budgets/Connections/connAdmin.php'); ?>

<?php

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{

  if (PHP_VERSION < 6) {

    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  }

 

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

 

  switch ($theType) {

    case "text":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;   

    case "long":

    case "int":

      $theValue = ($theValue != "") ? intval($theValue) : "NULL";

      break;

    case "double":

      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";

      break;

    case "date":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;

    case "defined":

      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

      break;

  }

  return $theValue;

}

}

 

$editFormAction = $_SERVER['PHP_SELF'];

if (isset($_SERVER['QUERY_STRING'])) {

  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);

}

 

 

  if (isset($_POST['submit'])) {

    $workStyle = $_POST['art_photos'];

 

    // Setting up a blank variable to be used in the coming loop.

    $allStyles = "";

 

    // For every checkbox value sent to the form.

    foreach ($workStyle as $style) {

      // Append a string called $allStyles with the current array element, and then add a comma and a space at the end.

      $allStyles .= $style . ", ";

    }

 

    // Delete the last two characters from the string.

    $allStyles = substr($allStyles, 0, -2);

 

    echo "<p>The resulting string is: <strong>$allStyles</strong></p>\r\n";

 

  }

 

 

 

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

  $insertSQL = sprintf("INSERT INTO daily_budget (rundate, slug, reporter, budgetInfo, notes, `section`, deadline, art_photos sidebar, sBudget) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",

                      GetSQLValueString($_POST['rundate'], "text"),

                      GetSQLValueString($_POST['slug'], "text"),

                      GetSQLValueString($_POST['reporter'], "text"),

                      GetSQLValueString($_POST['budgetInfo'], "text"),

                      GetSQLValueString($_POST['notes'], "text"),

                      GetSQLValueString($_POST['section'], "text"),

                      GetSQLValueString($_POST['deadline'], "text"),

  GetSQLValueString($_POST['art_photos'], "text"),

                      GetSQLValueString($_POST['sidebar'], "text"),

                      GetSQLValueString($_POST['sBudget'], "text"));

 

  mysql_select_db($database_connAdmin, $connAdmin);

  $Result1 = mysql_query($insertSQL, $connAdmin) or die(mysql_error());

}

 

?>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Budget Record</title>

 

</head>

 

<body>

<?php

if (isset($error)) {

echo '<ul>';

foreach($error as $alert) {

echo "<li class='warning'>$alert</li>\n";

}

echo '</ul>';

}

?>

<form action="<?php echo $editFormAction; ?>" method="POST" name="form1" id="form1">

  <table align="center" width="60%" border="2" bgcolor="#FFFFFF">

    <tr valign="baseline">

      <td width="14%" align="right" nowrap="nowrap"><STRONG>Rundate:</STRONG></td>

      <td width="86%">

        <input type="text" name="rundate" size="30" value="" id="jQueryUICalendar2"/>

        <script type="text/javascript">

// BeginWebWidget jQuery_UI_Calendar: jQueryUICalendar2

jQuery("#jQueryUICalendar2").datepicker();

 

// EndWebWidget jQuery_UI_Calendar: jQueryUICalendar2

              </script></td>

    </tr>

    <tr valign="baseline">

      <td nowrap="nowrap" align="right"><STRONG>Section:</STRONG></td>

      <td>

        <select name="section">

        <option value=""></option>

          <option value="A1">A1</option>

          <option value="Pickup All">Pickup All</option>

          <option value="Region">Region</option>

          <option value="Atlantic">Atlantic</option>

        <option value="Business">Business</option>

          <option value="Features">Features</option>

          <option value="Sports">Sports</option>

        </select>

    </td>

    </tr>

    <tr valign="baseline">

      <td nowrap="nowrap" align="right"><strong>Slug:</strong></td>

      <td><span id="sprytextfield2">

      <input type="text" name="slug" value="" size="32" />

      <span class="textfieldRequiredMsg">REQUIRED</span></span><span style="color: #F00; font-weight: bold; font-size: 10px;"></span></td>

    </tr>

    <tr valign="baseline">

      <td nowrap="nowrap" align="right"><strong>Budget Information:</strong></td>

      <td><span id="sprytextarea1">

        <textarea name="budgetInfo" cols="50" rows="4"></textarea>

<span class="textareaRequiredMsg">Required.</span></span><span style="color: #F00; font-weight: bold; font-size: 11px;"></span></td>

    </tr>

    <tr valign="baseline">

      <td nowrap="nowrap" align="right"><strong>Notes:</strong></td>

      <td><input type="text" name="notes" value="" size="32" /></td>

    </tr>

    <tr valign="baseline">

      <td nowrap="nowrap" align="right"><strong>Reporter:</strong></td>

      <td><span id="sprytextfield3">

        <input type="text" name="reporter" value="" size="32" />

      <span class="textfieldRequiredMsg">Required.</span></span><span style="color: #F00; font-weight: bold; font-size: 10px;"></span></td>

    </tr>

    <tr valign="baseline">

      <td nowrap="nowrap" align="right"><strong>Deadline:</strong></td>

      <td><input type="text" name="deadline" size="30" value="Enter date" id="jQueryUICalendar1"/><script type="text/javascript">

// BeginWebWidget jQuery_UI_Calendar: jQueryUICalendar2

jQuery("#jQueryUICalendar1").datepicker();

 

// EndWebWidget jQuery_UI_Calendar: jQueryUICalendar2

              </script> </td>

    </tr>

    <tr valign="baseline">

      <td nowrap="nowrap" align="right"><strong>Artwork:</strong></td>

      <td><label>

          <input type="checkbox" name="art_photos[]" value="factbox" id="art_photos_0"  />

          Factbox</label>

       

        <label>

          <input type="checkbox" name="art_photos[]" value="graphic" id="art_photos_1" />

          Graphic</label>

     

        <label>

          <input type="checkbox" name="art_photos[]" value="photo" id="art_photos_2" />

          Photo</label>

       

        <label>

          <input type="checkbox" name="art_photos[]" value="video" id="art_photos_3"  />

          Video</label>

      <br /></td>

    </tr>

        <tr>

       

    </tr>

      <tr valign="baseline">

      <td nowrap="nowrap" align="right">      <strong>Sidebar:</strong></td>

      <td valign="baseline"><span id="spryradio2">

        <label>

          <input type="radio" name="sidebar" value="y" id="sidebar_0" />

          Yes</label>

        <br />

        <label>

          <input type="radio" name="sidebar" value="n" id="sidebar_1" />

          No</label>

        <br />

        <span class="radioRequiredMsg"><strong style="font-size: 11px">REQUIRED.</strong></span></span>

       

    </tr>

    <tr valign="baseline">

      <td nowrap="nowrap" align="right"><strong>Sidebar Information:</strong></td>

      <td><input type="text" name="sBudget" value="" size="32" /></td>

    </tr>

    <tr valign="baseline">

      <td nowrap="nowrap" align="right"> </td>

      <td><input type="submit" value="Insert record" /></td>

    </tr>

  </table>

  <input type="hidden" name="MM_insert" value="form1" />

</form>

 

<p> </p>

<h3><a href="/new_budget/admin/manage_budgets.php"><strong>View your budget entry, make changes or delete your budget.</strong></a></h3>

<script type="text/javascript">

<!--

jQuery("#jQueryUICalendar1").datepicker();

jQuery("#jQueryUICalendar2").datepicker();

var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2", "none", {maxChars:15, validateOn:["change"]});

var sprytextarea1 = new Spry.Widget.ValidationTextarea("sprytextarea1", {validateOn:["change"]});

var sprytextfield3 = new Spry.Widget.ValidationTextField("sprytextfield3");

var spryradio2 = new Spry.Widget.ValidationRadio("spryradio2");

//-->

</script>

</body>

</html>

When posting code, please enclose it within the forum's

 . . . 

BBCode tags.

Sorry, but I'm new to this forum. I will remember to do so next time.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.