PHP - 轮询在数据库中发布数组

时间:2018-03-02 16:17:39

标签: php html

我正在进行学校民意调查,我发现了一个适合我作为新手here的需求。但是当我尝试将选项从收音机更改为复选框时,我遇到了发布数组的问题。这是代码:

<?php require_once('Connections/conn_vote.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $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"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO poll (id, question) VALUES (%s, %s)",
                       GetSQLValueString($_POST['id'], "int"),
                       GetSQLValueString($_POST['Poll'], "text"));
					   
  mysql_select_db($database_conn_vote, $conn_vote);
  $Result1 = mysql_query($insertSQL, $conn_vote) or die(mysql_error());

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

$colname_rs_vote = "-1";
if (isset($_GET['recordID'])) {
  $colname_rs_vote = $_GET['recordID'];
}
mysql_select_db($database_conn_vote, $conn_vote);
$query_rs_vote = sprintf("SELECT * FROM poll WHERE id = %s", GetSQLValueString($colname_rs_vote, "int"));
$rs_vote = mysql_query($query_rs_vote, $conn_vote) or die(mysql_error());
$row_rs_vote = mysql_fetch_assoc($rs_vote);
$totalRows_rs_vote = mysql_num_rows($rs_vote);
?>
<!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>
<script src="jquery-3.3.1.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>2017 CES Elections</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>

<fieldset>
	<legend>Select at most two from the following candidates.</legend>
	
	<form action="<?php echo $editFormAction; ?>" id="form1" name="form1" method="POST">
	<div id="subscriber_1" class="subscriber">
		<label>
    	<input type="checkbox" name="Poll[]" value="bm1" id="Poll_0" />
     	<img src="images/bm1.jpg" align="center">
		Candidate 1 <i>(SMM)</i>
		</label>
    	<label>
		<input type="checkbox" name="Poll[]" value="bm2" id="Poll_1" />
		<img src="images/bm2.jpg" align="center">
      	Candidate 2 <i>(SMAM)</i>
    	</label>
		<label>
		<input type="checkbox" name="Poll[]" value="bm3" id="Poll_2" />
		<img src="images/bm3.jpg" align="center">
      	Candidate 3 <i>(SMAM)</i>
    	</label>
		<label>
		<input type="checkbox" name="Poll[]" value="bm4" id="Poll_3" />
		<img src="images/bm4.jpg" align="center">
      	Candidate 4 <i>(SMM)</i>
		</label>
	</div>
   	<center>
		<input type="submit" name="submit" id="submit" value="Next" /></input>
		<input type="hidden" name="id" value="form1" />
		<input type="hidden" name="MM_insert" value="form1" />	
	</center>
	</form>
</fieldset>
<script>
$('.subscriber :checkbox').change(function () {
    var $cs=$(this).closest('.subscriber').find(':checkbox:checked');
    if ($cs.length > 2) {
        this.checked=false;
    }
});
</script>
</body>
</html>
<?php
mysql_free_result($rs_vote);
?>

然而,当我运行代码时,数据库会选择一个数组而不是单独的投票。

IMAGE HERE

如何使其发布不是数组的单独值? 提前谢谢!

1 个答案:

答案 0 :(得分:0)

如果您希望将$_POST['Poll']的字符串解释存储在数据库中,请更改$ insertSQL的代码:

$insertSQL = sprintf("INSERT INTO poll (id, question) VALUES (%s, %s)",
                       GetSQLValueString($_POST['id'], "int"),
                       GetSQLValueString(implode(",", $_POST['Poll']), "text"));