...

View Full Version : Inserting multiple values into MySQL with checkboxes



redfox
11-26-2010, 05:17 PM
So I know I need to create an array to do this, but I am not sure. This is what I have:


<?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());
}
}
?>

The error I get 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

Attached is the whole file, sorry about the mess, I'm new to this depth of PHP and I've been working with my good friend dreamweaver.

jim_keller
11-27-2010, 04:47 AM
where are you calling mysql_real_escape_string() ?

redfox
11-27-2010, 05:12 AM
where are you calling mysql_real_escape_string() ?

At the beginning of the script, line 8. Here, I'll just paste the whole thing up.


<?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;
}
}

// *** Redirect if username exists
$MM_flag="MM_insert";
if (isset($_POST[$MM_flag])) {
$MM_dupKeyRedirect="error.php";
$loginUsername = $_POST['first'] . " " . $_POST[
'last'];
$LoginRS__query = sprintf("SELECT CONCAT_WS(' ',`first`,`last`) FROM votes WHERE CONCAT_WS(' ',`first`,`last`)='%s'", GetSQLValueString($loginUsername, "-1"));
mysql_select_db($database_ballot, $ballot);
$LoginRS=mysql_query($LoginRS__query, $ballot) or die(mysql_error());
$loginFoundUser = mysql_num_rows($LoginRS);

//if there is a row in the database, the username was found - can not add the requested username
if($loginFoundUser){
$MM_qsChar = "?";
//append the username to the redirect page
if (substr_count($MM_dupKeyRedirect,"?") >=1) $MM_qsChar = "&";
$MM_dupKeyRedirect = $MM_dupKeyRedirect . $MM_qsChar ."requsername=".$loginUsername;
header ("Location: $MM_dupKeyRedirect");
exit;
}
}


$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:&nbsp; <input type="text" name="first" /><br />
Last Name:&nbsp; <input type="text" name="last" /><br />
<div>Gender:&nbsp;
<label>
Male <input type="radio" value="Male" name="gender" /></label>
<label>Female <input type="radio" value="Female" name="gender" /></label>
</div>
Grade:&nbsp;
<select name="grade">
<option selected="selected">&nbsp;</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:&nbsp;
<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:&nbsp;
<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:&nbsp;
<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 align="center">Copyright</div>
</div>

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

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

redfox
11-28-2010, 07:03 AM
Bump.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum