Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

TiagoWanderley

ERRO! checkbox...

Recommended Posts

Olá pessoal,

 

Estou com um probleminhas na hora de cadastrar varias opções em meu banco de dados.

 

Exemplo:

 

Tenho um formulário de cadastro onde na parte do "checkbox" preciso gravar varias opções em meu BD só que mesmo quando seleciono mais de uma opção ele só grava uma. Segue abaixo código:

 

Programação:

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO cad_departamento (id, de_departamento, de_sigla) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['id'], "int"),
                       GetSQLValueString($_POST['de_departamento'], "text"),
                       GetSQLValueString($_POST['de_sigla'], "text"));

Formulário:

<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
  <table align="center">
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">De_departamento:</td>
      <td><input type="text" name="de_departamento" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">De_sigla:</td>
      <td>
      		<input type="checkbox" name="de_sigla" value="DP" checked="checked"/>DP
            <input type="checkbox" name="de_sigla" value="SB" checked="checked"/>SB
            <input type="checkbox" name="de_sigla" value="TR" checked="checked"/>TR
            <input type="checkbox" name="de_sigla" value="SP" checked="checked"/>SP
      </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="id" value="" />
  <input type="hidden" name="MM_insert" value="form1" />
</form>

Me ajudem por favor, não estou conseguindo enxergar o erro. Obrigado!

Compartilhar este post


Link para o post
Compartilhar em outros sites

Veja se fazendo isso dá certo.

 

Coloque os campos de "checkbox" em array assim você consegue pegar todos os valores para gravar

 

<input type="checkbox" name="de_sigla[]" value="DP" checked="checked"/>DP
<input type="checkbox" name="de_sigla[]" value="SB" checked="checked"/>SB
<input type="checkbox" name="de_sigla[]" value="TR" checked="checked"/>TR
<input type="checkbox" name="de_sigla[]" value="SP" checked="checked"/>SP

Compartilhar este post


Link para o post
Compartilhar em outros sites

Olá gucamargo,

 

Fiz a alteração porem esta dando o seguinte erro:

 

Atenção: mysql_real_escape_string () espera o parâmetro 1 para ser string, array dado em C: \ wamp \ www \ semtas_compras \ teste.php na linha 10

 

Abaixo segue o código completo para dar uma analisada:

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  Linha 10 ---> $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 cad_departamento (id, de_departamento, de_sigla) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['id'], "int"),
                       GetSQLValueString($_POST['de_departamento'], "text"),
                       GetSQLValueString($_POST['de_sigla'], "text"));

  mysql_select_db($database_config, $config);
  $Result1 = mysql_query($insertSQL, $config) or die(mysql_error());

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


$maxRows_Recordset1 = 20;
$pageNum_Recordset1 = 0;
if (isset($_GET['pageNum_Recordset1'])) {
  $pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;

mysql_select_db($database_config, $config);
$query_Recordset1 = "SELECT * FROM cad_departamento";
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $config) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

if (isset($_GET['totalRows_Recordset1'])) {
  $totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
} else {
  $all_Recordset1 = mysql_query($query_Recordset1);
  $totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
}
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;

?>

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.