Jump to content

Inserting Multiple Checkbox Values into MySQL


philulrich

Recommended Posts

So first of all, I have a confession, I used Dreamweaver to take care of all my database work. Pheww. Now that I got that of my chest, I am having problems, not only with my lack of experience working with databases, but with my script also. I need to insert multiple checkbox values into my database into the same cell. So I have searched around and I thought I had found a fairly good approach, but with Dw's hodgepodge of overcomplicated mess, I am finding problems integrating it. The error I am getting post submission is "Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in C:\wamp\www\gov\ballot.php on line 10

Column 'involvement' cannot be null" So if you would, could you debug my script and give me some instruction along to way so that I can learn a little something too? Here is my script:

 

<?php require_once('Connections/ballot.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["MM_insert"])) && ($_POST["MM_insert"] == "Ballot")) {
  $insertSQL = sprintf("INSERT INTO votes (`first`, `last`, gender, grade, involvement, schedule, party, `time`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['first'], "text"),
                       GetSQLValueString($_POST['last'], "text"),
                       GetSQLValueString($_POST['gender'], "text"),
                       GetSQLValueString($_POST['grade'], "text"),
                       GetSQLValueString($_POST['involvement'], "text"),
                       GetSQLValueString($_POST['schedule'], "text"),
                       GetSQLValueString($_POST['party'], "text"),
                       GetSQLValueString($_POST['time'], "text"));

  mysql_select_db($database_ballot, $ballot);
  $Result1 = mysql_query($insertSQL, $ballot) or die(mysql_error());

  $insertGoTo = "thanks.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_ballot, $ballot);
$query_Recordset1 = "SELECT * FROM votes";
$Recordset1 = mysql_query($query_Recordset1, $ballot) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!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>Please Place your Vote Below</title>
</head>

<body style="background-image:url(images/bg.jpg); background-repeat:repeat-x; background-color:#000000;">
<table align="center" bgcolor="#FFFFFF" cellpadding="0" cellspacing="0" border="0">
<tr><td>
<div style="border:thin; border-color:000000; border-style:groove; width:700px; background-color:ffffff;">
<div align="center"><a href="index.php"><img src="images/header.jpg" alt="Mock Elections 2010" /></a></div>
<div style="margin:10px; margin-top:30px;">
Before continuing to vote, in order to count your vote, we ask that you please ensure you are adhereing to the following guidelines:
</div>
<div style="margin:30px">
  <p>- When putting your name, remember to put your full legal name. NO nicknames.<br />
    - Be sure that all your information is valid otherwise your vote will not be counted.
    <br />
  </p>
</div>
<div style="margin-left:150px; margin-right:150px; margin-top:50px;">
<form action="<?php echo $editFormAction; ?>" method="POST" name="Ballot">
First Name:  <input type="text" name="first" /><br />
Last Name:  <input type="text" name="last" /><br />
<div>Gender: 
<label>
Male <input type="radio" value="Male" name="gender" /></label>
<label>Female <input type="radio" value="Female" name="gender" /></label>
</div>
Grade: 
<select name="grade">
  <option selected="selected"> </option>
  <option value="7">7</option>
  <option value="8">8</option>
  <option value="9">9</option>
  <option value="10">10</option>
  <option value="11">11</option>
  <option value="12">12</option>
  <option value="Teacher">Teacher</option>
  <option value="Staff">Staff</option>
</select><br />
<div>Involvement: 
  <label>
    <input type="checkbox" name="involvement[]" value="Athletics" id="involvement_0" />
    Athletics</label>
  <label>
    <input type="checkbox" name="involvement[]" value="Co-Curricular" id="involvement_1" />
    Co-Curricular</label>
  <label>
    <input type="checkbox" name="involvement[]" value="Clubs" id="involvement_2" />
    Clubs</label>
</div>
<div>Schedule: 
<label><input type="radio" value="C4" name="schedule" />C4</label>
<label><input type="radio" value="All Day" name="schedule" />All Day Hauser Student</label>
</div>
<div>Party: 
<label><input type="radio" value="PokePartay" name="party" />PokePartay</label>
<label><input type="radio" value="Equilibrium" name="party" />Equilibrium Party</label>
</div>
<br />
<input type="Submit" value="Submit" />
<input type="hidden" value="<?php 
$tz =  new DateTimeZone('America/Indianapolis');
$date = new DateTime('now', $tz);
echo $date->format('m-d-Y h:i:s') . "\n";
?>" name="time" />
<?php
if (isset($_POST['submit'])) { 
   $involvements = $_POST['involvement'];

   foreach($involvements as $involvement) {
      $query2 = "INSERT INTO votes (involvement) VALUES ('$involvement')";
      $result2 = mysql_query($ballot, $query2) or die(mysql_error());
   }
}
?>
<input type="hidden" name="MM_insert" value="Ballot" />
</form>
</div>
<br />
<br />
<br />
<br />
</div>

</td>
</tr>
</table>

</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

 

Thanks,

Philip Ulrich

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.