<?php require_once('../../Connections/#.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']);
}
function converter_data($strData) {
// Recebemos a data no formato: dd/mm/aaaa
// Convertemos a data para o formato: aaaa-mm-dd
if ( preg_match("#/#",$strData) == 1 ) {
$strDataFinal = "'";
$strDataFinal .= implode('-', array_reverse(explode('/',$strData)));
$strDataFinal .= "'";
}
return $strDataFinal;
}
if ($_POST['cadastrar']) {
// Recupera os dados dos campos
$imagem = $_FILES["not_imagem"];
// Se a foto estiver sido selecionada
if (!empty($imagem["name"])) {
// Largura máxima em pixels
$largura = 150;
// Altura máxima em pixels
$altura = 180;
// Tamanho máximo do arquivo em bytes
$tamanho = 1000;
// Verifica se o arquivo é uma imagem
if(!eregi("^image\/(pjpeg|jpeg|png|gif|bmp)$", $imagem["type"])){
$error[1] = "Isso não é uma imagem.";
}
// Pega as dimensões da imagem
$dimensoes = getimagesize($imagem["tmp_name"]);
// Verifica se a largura da imagem é maior que a largura permitida
if($dimensoes[0] > $largura) {
$error[2] = "A largura da imagem não deve ultrapassar ".$largura." pixels";
}
// Verifica se a altura da imagem é maior que a altura permitida
if($dimensoes[1] > $altura) {
$error[3] = "Altura da imagem não deve ultrapassar ".$altura." pixels";
}
// Verifica se o tamanho da imagem é maior que o tamanho permitido
if($arquivo["size"] > $tamanho) {
$error[4] = "A imagem deve ter no máximo ".$tamanho." bytes";
}
// Se não houver nenhum erro
if (count($error) == 0) {
// Pega extensão da imagem
preg_match("/\.(gif|bmp|png|jpg|jpeg){1}$/i", $imagem["name"], $ext);
// Gera um nome único para a imagem
$nome_imagem = md5(uniqid(time())) . "." . $ext[1];
// Caminho de onde ficará a imagem
$caminho_imagem = "fotos/" . $nome_imagem;
// Faz o upload da imagem para seu respectivo caminho
move_uploaded_file($imagem["tmp_name"], $caminho_imagem);
// Insere os dados no banco
$insertSQL = sprintf("INSERT INTO site_noticias (not_titulo, not_imagem, not_descricao, not_texto, not_categoria, not_autor, not_fonte, not_status, not_data) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['not_titulo'], "text"),
GetSQLValueString($imagem, "text"),
GetSQLValueString($_POST['not_descricao'], "text"),
GetSQLValueString($_POST['not_texto'], "text"),
GetSQLValueString($_POST['not_categoria'], "text"),
GetSQLValueString($_POST['not_autor'], "text"),
GetSQLValueString($_POST['not_fonte'], "text"),
GetSQLValueString(isset($_POST['not_status']) ? "true" : "", "defined","1","0"),
converter_data($_POST['not_data']));
// Se os dados forem inseridos com sucesso
if ($insertSQL){
echo "Você foi cadastrado com sucesso.";
}
}
// Se houver mensagens de erro, exibe-as
if (count($error) != 0) {
foreach ($error as $erro) {
echo $erro . "<br />";
}
}
}
}
// if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
mysql_select_db($database_gwb_connect, $gwb_connect);
$Result1 = mysql_query($insertSQL, $gwb_connect) or die(mysql_error());
$insertGoTo = "../sucesso.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
mysql_select_db($database_gwb_connect, $gwb_connect);
$query_rs_categorias = "SELECT * FROM site_categoria_noticia";
$rs_categorias = mysql_query($query_rs_categorias, $gwb_connect) or die(mysql_error());
$row_rs_categorias = mysql_fetch_assoc($rs_categorias);
$totalRows_rs_categorias = mysql_num_rows($rs_categorias);
?>
alguém tem alguma ideia do que eu posso fazer pra consertar essa script?
Pergunta
Portal do Químico
Boa tarde a todos, estou com um problema ao gravar a imagem no banco de dados. Na verdade o script nem chega a funcionar.
Vou dividir o tópico em 2: O Como está meu script ATUAL e como foi minha tentativa:
SCRIPT ATUAL:
<?php require_once('../../Connections/#.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']); } function converter_data($strData) { // Recebemos a data no formato: dd/mm/aaaa // Convertemos a data para o formato: aaaa-mm-dd if ( preg_match("#/#",$strData) == 1 ) { $strDataFinal = "'"; $strDataFinal .= implode('-', array_reverse(explode('/',$strData))); $strDataFinal .= "'"; } return $strDataFinal; } if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { $insertSQL = sprintf("INSERT INTO site_noticias (not_titulo, not_imagem, not_descricao, not_texto, not_categoria, not_autor, not_fonte, not_status, not_data) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)", GetSQLValueString($_POST['not_titulo'], "text"), GetSQLValueString($_POST['not_imagem'], "text"), GetSQLValueString($_POST['not_descricao'], "text"), GetSQLValueString($_POST['not_texto'], "text"), GetSQLValueString($_POST['not_categoria'], "text"), GetSQLValueString($_POST['not_autor'], "text"), GetSQLValueString($_POST['not_fonte'], "text"), GetSQLValueString(isset($_POST['not_status']) ? "true" : "", "defined","1","0"), converter_data($_POST['not_data'])); mysql_select_db($database_gwb_connect, $gwb_connect); $Result1 = mysql_query($insertSQL, $gwb_connect) or die(mysql_error()); $insertGoTo = "../sucesso.php"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo)); } mysql_select_db($database_gwb_connect, $gwb_connect); $query_rs_categorias = "SELECT * FROM site_categoria_noticia"; $rs_categorias = mysql_query($query_rs_categorias, $gwb_connect) or die(mysql_error()); $row_rs_categorias = mysql_fetch_assoc($rs_categorias); $totalRows_rs_categorias = mysql_num_rows($rs_categorias); ?>FORMULARIO<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1"> <table align="center"> <tr> <td colspan="2" class="table_header_left">TITULO</td> </tr> <tr valign="baseline"> <td colspan="2" align="left" nowrap="nowrap"><input type="text" name="not_titulo" value="" size="105" /></td> </tr> <tr> <td colspan="2" class="table_header_left">IMAGEM</td> </tr> <tr valign="baseline"> <td colspan="2" align="left" nowrap="nowrap"> <!-- <input type="text" name="not_imagem" value="" size="32" /> <select name="not_imagem"> <?php // lista as pastas se houverem if ($arquivos != "" ) { foreach($arquivos as $listar) { ?> <option value="<?php echo "$caminho/$listar"; ?>" selected="selected"><?php echo $listar;?></option> <?php } } ?> </select>--> <input type="file" name="not_imagem" /><br /><br /> </td> </tr> <tr> <td colspan="2" class="table_header_left">DESCRIÇÃO</td> </tr> <tr valign="baseline"> <td colspan="2" align="left" valign="top" nowrap="nowrap"> <textarea name="not_descricao" cols="50" rows="5"></textarea></td> </tr> <tr> <td colspan="2" class="table_header_left">CORPO DA NOTÍCIA</td> </tr> <tr valign="baseline"> <td colspan="2" align="right" valign="top" nowrap="nowrap"> <textarea name="not_texto" cols="50" rows="5"></textarea></td> </tr> <tr valign="baseline"> <td width="55" align="left" nowrap="nowrap">Categoria:</td> <td width="571" align="left"><select name="not_categoria"> <?php do { ?> <option value="<?php echo $row_rs_categorias['cat_nome']?>" ><?php echo $row_rs_categorias['cat_nome']?></option> <?php } while ($row_rs_categorias = mysql_fetch_assoc($rs_categorias)); ?> </select></td> </tr> <tr valign="baseline"> <td nowrap="nowrap" align="left">Autor:</td> <td align="left"><input type="text" name="not_autor" value="" size="32" /></td> </tr> <tr valign="baseline"> <td nowrap="nowrap" align="left">Fonte:</td> <td align="left"><input type="text" name="not_fonte" value="" size="32" /></td> </tr> <tr valign="baseline"> <td align="left" nowrap="nowrap">Status:</td> <td align="left"><input type="checkbox" name="not_status" value="" /></td> </tr> <tr valign="baseline"> <td nowrap="nowrap" align="left">Data:</td> <td align="left"><input name="not_data" type="text" value="<?php $dia = date(d); $mes = date(m); $ano = date(Y); echo "$dia/$mes/$ano"; ?>" size="32" /></td> </tr> <tr valign="baseline"> <td nowrap="nowrap" align="right"> </td> <td><input type="submit" value="Cadastrar" /> <input name="Reset" type="reset" value="Limpar" /></td> </tr> </table> <input type="hidden" name="MM_insert" value="form1" /> </form> <?php mysql_free_result($rs_categorias); ?>Como tentei<?php require_once('../../Connections/#.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']); } function converter_data($strData) { // Recebemos a data no formato: dd/mm/aaaa // Convertemos a data para o formato: aaaa-mm-dd if ( preg_match("#/#",$strData) == 1 ) { $strDataFinal = "'"; $strDataFinal .= implode('-', array_reverse(explode('/',$strData))); $strDataFinal .= "'"; } return $strDataFinal; } if ($_POST['cadastrar']) { // Recupera os dados dos campos $imagem = $_FILES["not_imagem"]; // Se a foto estiver sido selecionada if (!empty($imagem["name"])) { // Largura máxima em pixels $largura = 150; // Altura máxima em pixels $altura = 180; // Tamanho máximo do arquivo em bytes $tamanho = 1000; // Verifica se o arquivo é uma imagem if(!eregi("^image\/(pjpeg|jpeg|png|gif|bmp)$", $imagem["type"])){ $error[1] = "Isso não é uma imagem."; } // Pega as dimensões da imagem $dimensoes = getimagesize($imagem["tmp_name"]); // Verifica se a largura da imagem é maior que a largura permitida if($dimensoes[0] > $largura) { $error[2] = "A largura da imagem não deve ultrapassar ".$largura." pixels"; } // Verifica se a altura da imagem é maior que a altura permitida if($dimensoes[1] > $altura) { $error[3] = "Altura da imagem não deve ultrapassar ".$altura." pixels"; } // Verifica se o tamanho da imagem é maior que o tamanho permitido if($arquivo["size"] > $tamanho) { $error[4] = "A imagem deve ter no máximo ".$tamanho." bytes"; } // Se não houver nenhum erro if (count($error) == 0) { // Pega extensão da imagem preg_match("/\.(gif|bmp|png|jpg|jpeg){1}$/i", $imagem["name"], $ext); // Gera um nome único para a imagem $nome_imagem = md5(uniqid(time())) . "." . $ext[1]; // Caminho de onde ficará a imagem $caminho_imagem = "fotos/" . $nome_imagem; // Faz o upload da imagem para seu respectivo caminho move_uploaded_file($imagem["tmp_name"], $caminho_imagem); // Insere os dados no banco $insertSQL = sprintf("INSERT INTO site_noticias (not_titulo, not_imagem, not_descricao, not_texto, not_categoria, not_autor, not_fonte, not_status, not_data) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)", GetSQLValueString($_POST['not_titulo'], "text"), GetSQLValueString($imagem, "text"), GetSQLValueString($_POST['not_descricao'], "text"), GetSQLValueString($_POST['not_texto'], "text"), GetSQLValueString($_POST['not_categoria'], "text"), GetSQLValueString($_POST['not_autor'], "text"), GetSQLValueString($_POST['not_fonte'], "text"), GetSQLValueString(isset($_POST['not_status']) ? "true" : "", "defined","1","0"), converter_data($_POST['not_data'])); // Se os dados forem inseridos com sucesso if ($insertSQL){ echo "Você foi cadastrado com sucesso."; } } // Se houver mensagens de erro, exibe-as if (count($error) != 0) { foreach ($error as $erro) { echo $erro . "<br />"; } } } } // if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { mysql_select_db($database_gwb_connect, $gwb_connect); $Result1 = mysql_query($insertSQL, $gwb_connect) or die(mysql_error()); $insertGoTo = "../sucesso.php"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo)); } mysql_select_db($database_gwb_connect, $gwb_connect); $query_rs_categorias = "SELECT * FROM site_categoria_noticia"; $rs_categorias = mysql_query($query_rs_categorias, $gwb_connect) or die(mysql_error()); $row_rs_categorias = mysql_fetch_assoc($rs_categorias); $totalRows_rs_categorias = mysql_num_rows($rs_categorias); ?>alguém tem alguma ideia do que eu posso fazer pra consertar essa script?
Desde já agradeço!
Editado por Portal do QuímicoLink para o comentário
Compartilhar em outros sites
8 respostass a esta questão
Posts Recomendados
Participe da discussão
Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.