-1

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???

14
  • 1
    Here is some help: (1) Stop using "mysql_"; it has been obsoleted; (2) Always use parameters to pass user input into queries. Nov 5, 2016 at 23:57
  • What does "doesn't work" mean? Error messages? Server crashes? Computer caught on fire? Nov 5, 2016 at 23:58
  • Hey Anthony, quick question have you made sure that the connection.php have the correct info? that you are using the correct host, user, password and database? Nov 5, 2016 at 23:58
  • What if you change <? to <?php ?
    – Bart
    Nov 5, 2016 at 23:58
  • As Gordon says you should look into prepared statements using placeholders this also secures your site from sql injection. for documentation visit w3schools.com/php/php_mysql_prepared_statements.asp Nov 5, 2016 at 23:59

1 Answer 1

1

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.

2
  • 1
    all yours buddy :-)
    – user6763587
    Nov 6, 2016 at 0:50
  • Cheers @nogad ;-) Nov 6, 2016 at 0:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.