inserir registro my sql com php 7

19/09/2017

0

Bom dia pessoal.
estou tentando cadastrar, um registro no banco mysql com php 7 e ele retor um erro na lnha 24 (mysql_query) e não cadastra. alguem pode me ajudar

<?php
$host = "localhost";
$user = "root";
$pass = "vertrigo";
$banco = "jornaldodia";

$conexao = mysqli_connect($host, $user, $pass, $banco);
if(mysqli_connect_errno($conexao)) {
echo"Erro de conexão";
}else{
echo"Conexao ok";
}

$foto = '$_POST[foto]';
$titulo = '$_POST[titulo]';
$editoria = '$_POST[editoria]';
$principal = '$_POST[principal]';
$desc = '$_POST[desc]';
$noticia = '$_POST[noticia]';
$autor = '$_POST[autor]';
$data = '$_POST[data]';
$status = '$_POST[status]';

mysql_query("INSERT INTO noticias (not_foto, not_titulo, not_editoria, not_principal, not_desc, not_noticia, not_autor, not_data, not_status) VALUES ('$foto', '$titulo', '$editoria', '$principal', '$desc', '$noticia', '$autor', '$data', '$status')");
?>

<script>
alert('Notícia cadastrada com sucesso.');
</script>

<meta http-equiv="refresh content="0; url="noticias.php">
Antonio Santos

Antonio Santos

Responder

Posts

19/09/2017

Aparecida Gonçalves

Olá Antonio,
tudo bem?

Eu acho que você abriu aspas simples no lugar errado.
Como está:
$desc = '$_POST[desc]';

Como deveria ser:
$desc = $_POST["desc"];

Altere as aspas simples de todas as variáveis onde utilizou o $_POST (http://php.net/manual/pt_BR/reserved.variables.post.php) e depois poste o código para verificarmos a próxima fase que é o seu comando INSERT.

Espero ter ajudado.

Abraço,
Cida Luna.
Responder

19/09/2017

Antonio Santos

Cida querida boa tarde.
fiz essas alterações abaixo, ele não da erro mas tambem não grava

<html>
<head>
<?php
$host = "localhost";
$user = "root";
$pass = "vertrigo";
$banco = "jornaldodia";

$conexao = mysqli_connect($host, $user, $pass, $banco);
?>

<?php
$foto = $_POST[''foto''];
$titulo = $_POST[''titulo''];
$editoria = $_POST[''editoria''];
$principal = $_POST[''principal''];
$desc = $_POST[''desc''];
$noticia = $_POST[''noticia''];
$autor = $_POST[''autor''];
$data = $_POST[''data''];
$status = $_POST[''status''];

$sql=("INSERT INTO noticias
(not_foto, not_titulo, not_editoria, not_principal, not_desc, not_noticia, not_autor, not_data, not_status) VALUES (''$foto'', ''$titulo'', ''$editoria'', ''$principal'', ''$desc'', ''$noticia'', ''$autor'', ''$data'', ''$status'')");

$res = mysqli_query($conexao, $sql);
?>

<script>
alert(''Notícia cadastrada com sucesso.'');
</script>

<meta http-equiv="refresh content="0; url="noticias.php">

</head>
</html>
Responder

19/09/2017

Gxf

Usando mysqli voce tem que fazer assim:

$sql=($conexao, "INSERT INTO noticias
(not_foto, not_titulo, not_editoria, not_principal, not_desc, not_noticia, not_autor, not_data, not_status) VALUES (''$foto'', ''$titulo'', ''$editoria'', ''$principal'', ''$desc'', ''$noticia'', ''$autor'', ''$data'', ''$status'')");



Caso não de certo, faça um if para ver se apresenta algum erro, por exemplo:

if(mysqli_affected_rows($conexao) > 0){
echo "deu certo"
} else {
echo mysqli_error($conexao);
}
Responder

20/09/2017

Antonio Santos

Cida querida boa noite, resolvi deu certo.

agora queria saber se vc consegue me ajudar com com esse codigo abaixo:

Os erros estão nas linhas tracejadas


<?php require_once('Connections/conJd.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("mysqli_real_escape_string") ? mysqli_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;
}
}

$colname_RsNotDestaques = "-1";
if (isset($_GET['SIM'])) {
$colname_RsNotDestaques = $_GET['SIM'];
}



--------------------------------------------------
$conJd = mysqli_connect($banco);
-------------------------------------------------


$query_RsNotDestaques = sprintf("SELECT * FROM noticias WHERE not_principal = %s ORDER BY not_id DESC", GetSQLValueString($colname_RsNotDestaques, "text"));


-------------------------------------------------------------------------------------------------------------------
$RsNotDestaques = mysqli_query($conJd, $query_RsNotDestaques, $sql) or die(mysqli_error());
-----------------------------------------------------------------------------------------------------------------------------


$row_RsNotDestaques = mysqli_fetch_assoc($RsNotDestaques);
$totalRows_RsNotDestaques = mysqli_num_rows($RsNotDestaques);
$res = mysqli_query($conJd, $sql);
?>
Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar