1
I am making a form and for this I have created a database where such data will be saved. I would like to make the connection to the database from an external file, so it is necessary to just call it with 'require' or 'include'. However, when this is done, the connection variable '$con' is null, that is, I cannot use it. I think there’s some way to do it, but even after a lot of research I couldn’t find anything to fix it.
I need guidance on the possibility of that. Thanks in advance.
Notice: Undefined variable: con in C: Users Fulltime Documents Adaptando php cadastrar.php on line 25
php server.
<?php
$con = mysqli_connect("localhost", "root", "", "db");
if (!$con) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($con) . PHP_EOL;
?>
register.php
<?php
session_start();
include_once('php/servidor.php');
ini_set('display_errors',true); error_reporting(E_ALL);
// Aqui são definidas as variáveis vindas do formulário
$nome = $_POST['nome'];
$email = $_POST['email'];
$telefone = $_POST['telefone'];
$cpf = $_POST['cpf'];
//aqui eu defino meu botão
if (isset($_POST['enviar'])) {
// Aqui é executado a inserção no banco de dados
if (!empty($_POST['nome']) || !empty($_POST['email']) || !empty($_POST['telefone']) || !empty($_POST['cpf'])){
$sql = mysqli_query($con, "INSERT INTO clientes(nomecli, emailcli, telefonecli, cpfcli) VALUES ('$nome', '$email', '$telefone', '$cpf')");
if ($sql) {
echo "<script>
javascript:window.location='../cadastro.html';
alert('Cadastrado realizado com sucesso!!!');</script>";
exit;
}
else{
echo "Opss!! Alguma coisa deu errado, cadastro não realizado.";
exit;
}
}
else{
echo "<script>javascript:window.location='../cadastro.html';
alert('Opsss!! Algum dos campos está em branco, por favor, preencha-os.');</script>";
exit;
}
}
else{
echo "<script>alert('Opsss!! Botão não identificado.');</script>";
exit;
}
mysqli_close($con);
?>
It was included yes, the problem was another, a friend managed to help me. Thank you !!
– Jonatan