AnthonyKamers AnthonyKamers - 29 days ago 10
PHP Question

Same script php on localhost doesn´t work online

My php script is working on localhost, otherwise it´s not working online (web hosting)

The php versions are the same and the SQL as well

I don´t know what´s wrong with it:

<?php

error_log();

$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'controleestoque';

// conexão e seleção do banco de dados
$con = mysqlI_connect($host, $user, $pass, $db);

// GET ID
if(isset($_GET['id']))
{
$idUsuario = $_GET['id'];

$query = "SELECT * FROM usuarios WHERE idUsuario = '$idUsuario'";
$qr = mysqli_query($con, $query) or die(mysqli_error());
$ln = mysqli_fetch_assoc($qr);

$nomeUsuario = $ln['nomeUsuario'];
$celularUsuario = $ln['celularUsuario'];
$emailUsuario = $ln['emailUsuario'];
$dataBoleto = date("Y-m-d");
$tipoPlano = "planoPrata";
$valorBoleto = "120,00";


/* Verifica se já tem um ID Boleto Repetido */
while(true){

$idBoleto = rand(1, 99999999999);
$res = "SELECT * FROM boleto WHERE idBoleto = '$idBoleto'";
$search = mysqli_query($con, $res);

if(mysqli_num_rows($search) == 0){
break;
}
}

/* Se não retornar com ID Boleto igual -> Continua */

$sql = mysqli_query("INSERT INTO boleto (idBoleto, tipoPlano, valorBoleto, idUsuario, nomeUsuario, celularUsuario, emailUsuario, dataBoleto) values ('$idBoleto', '$tipoPlano', '$valorBoleto', '$idUsuario', '$nomeUsuario', '$celularUsuario', '$emailUsuario', '$dataBoleto')");

header("Location:gerar_boletoPrata.php?id=$idBoleto");

} /* Termina Isset Get ID */

else {
echo "
<meta http-equiv='refresh' content='0; url=index1.php' />
<script type='text/javascript'>alert ('Você precisa estar logado para visualizar esta página!!')</script>
";
}
// END
?>


The error is: doesn´t insert on database and doesn´t show the right $idBoleto for the user

Could anyone help me here???

Answer

You never passed the connection to this:

$sql = mysqli_query("INSERT INTO boleto ... ");

Therefore:

$sql = mysqli_query($con, "INSERT INTO boleto ... ");

and check for errors on all queries:

You're also open to a serious sql injection; use a prepared statement:

It's also best to add exit; after header.

header("Location:gerar_boletoPrata.php?id=$idBoleto");
exit;

Otherwise, your code may want to continue to execute.

Also note that mysqli_error() requires a db connection as an argument.

  • mysqli_error($con)

As per the manual:

string mysqli_error ( mysqli $link )


Footnotes:

I suggest you use mysqli_connect in all lowercase letters, instead of mysqlI_connect as the uppercase I (i) could be a distraction.

Comments