-
Problema logico if
Salve a tutti;
ho un problema nell'esecuzione di un if: questa pagian riceve da un form vari dati tra cui il numero di linea ($num) con cui verificare la disponibilità di un prodotto. Il problema è che sebbene questo prodotto sia disponbile ($numoff = 5) l'if lo considera come nullo e da il messaggio d'errore preimpostato. Qui il codice interessato:
Codice PHP:
<?php
session_start();
require_once('mysqlconn.php');
mysql_select_db($database, $offerte);
$bSuccesso = true;
$num = $_GET['num'];
$nome = $_POST['nome'];
$cognome = $_POST['cognome'];
$mail = $_POST['email'];
$nascita = $_POST['nascita'];
$provincia = $_POST['provincia'];
$indirizzo = $_indirizzo['indirizzo'];
$control = mysql_query("SELECT * FROM offerte WHERE num = '".$num."' ") or die(mysql_error());
echo "SELECT * FROM offerte WHERE num = '".$num."' ";
$info = mysql_fetch_array($control);
$numoff = $resrow['numoff'];
echo $numoff;
if ($numoff == 0) {
echo "Non è possibile completare l'operazione: l'offerta richiesta è esaurita";
} else {.......
-
Il problema penso sia questo:
Codice PHP:
$info = mysql_fetch_array($control);
$numoff = $resrow['numoff'];
Cambialo in:
Codice PHP:
$info = mysql_fetch_array($control);
$numoff = $info['numoff'];
-
Era proprio quello il problema, ora però mi da:
Fatal error: Cannot redeclare rpass() (previously declared in /membri/programmerscommunity/confbuy.php:26) in /membri/programmerscommunity/confbuy.php on line 26
-
Qui il codice completo:
Codice PHP:
<?php
session_start();
require_once('mysqlconn.php');
mysql_select_db($database, $offerte);
$bSuccesso = true;
$num = $_GET['num'];
$nome = $_POST['nome'];
$cognome = $_POST['cognome'];
$mail = $_POST['email'];
$nascita = $_POST['nascita'];
$provincia = $_POST['provincia'];
$indirizzo = $_indirizzo['indirizzo'];
$control = mysql_query("SELECT * FROM offerte WHERE num = '".$num."' ") or die(mysql_error());
$info = mysql_fetch_array($control);
$numoff = $info['numoff'];
if ($numoff == 0) {
echo "Non è possibile completare l'operazione: l'offerta richiesta è esaurita";
} else {
$numoff = $numoff - 1;
$countdown = mysql_query("UPDATE offerte SET numoff = '".$numoff."' WHERE num = '".$num."' ") or die(mysql_error());
//INIZIO GENERAZIONE CODICE
do {
mysql_select_db($database, $acquisti);
function rPass($length = 6) {
// Questo array contiene i caratteri con i quali
// saranno create le password
$lettere = explode(" ",
"A B C D E F G H I J K L M N O P Q R S T U V W X Y Z "
."a b c d e f g h i j k l m n o p q r s t u v w x y z "
."0 1 2 3 4 5 6 7 8 9");
// questo ? il clico tramite si crea la password
for($i=0;$i<$length;$i++) {
srand((double)microtime()*8625342);
$foo = rand(0, 61);
$pass = $pass.$lettere[$foo];
}
$control = mysql_query("SELECT * FROM acquisti WHERE codeoff = '".$pass."'") or die (mysql_error());
if (mysql_num_rows($control) != 0) {
$esist = 0;
} else { $esist = 1; }
return $pass;
return $esist;
}
} while ( $esist == 0);
//FINE GENERAZIONE CODICE
mysql_select_db($database, $acquisti);
$buy = mysql_query("INSERT INTO acquisti (num, nome, cognome, email, nascita, provincia, indirizzo, codeoff) values ('".$num."', '".$nome."', '".$cognome."', '".$email."', '".$nascita."', '".$provincia."', '".$indirizzo."', '".$pass."')") or die (mysql_error());
if ($bSuccesso)
{
$intestazioni = "MIME-Version: 1.0\r\n";
$intestazioni .= "Content-type: text/html; charset=iso-8859-1\r\n";
$messaggio = "
<html><head><title>Codice acquisto</title></head>
<body>
<h1>Grazie per aver effettuato l'acquisto</h1><br><br>
Stampa questa mail e consegnala presso $azienda; il pagamento dovrà essere effettuato in loco secondo le modalità stabilite dall'azienda erogatrice dell'offerta.<br><br>
CODICE OFFERTA: $pass
</body></html>";
mail ($mail, "Codice acquisto", $messaggio, $intestazioni);
echo "Acquisto avvenuto con successo; una mail con il codice per attivare l'offerta è stata inviata alla tua casella di posta elettronica.";
header('Location: index.php');
mysql_select_db($databse, $offerte);
$countdown = mysql_query("UPDATE offerte SET numoff = (numoff - 1) WHERE num = '".$num."' ") or die(mysql_error());
}
else
{
echo '<p align="center">Spiacenti, è avvenuto un errore! Indietro per riprovare.</p>';
}
}
?>
-
Cambia questa parte:
Codice PHP:
function rPass($length = 6) {
// Questo array contiene i caratteri con i quali
// saranno create le password
$lettere = explode(" ",
"A B C D E F G H I J K L M N O P Q R S T U V W X Y Z "
."a b c d e f g h i j k l m n o p q r s t u v w x y z "
."0 1 2 3 4 5 6 7 8 9");
// questo ? il clico tramite si crea la password
for($i=0;$i<$length;$i++) {
srand((double)microtime()*8625342);
$foo = rand(0, 61);
$pass = $pass.$lettere[$foo];
}
$control = mysql_query("SELECT * FROM acquisti WHERE codeoff = '".$pass."'") or die (mysql_error());
if (mysql_num_rows($control) != 0) {
$esist = 0;
} else { $esist = 1; }
return $pass;
return $esist;
}
Con:
Codice PHP:
/* function rPass($length = 6) {
// Questo array contiene i caratteri con i quali
// saranno create le password
$lettere = explode(" ",
"A B C D E F G H I J K L M N O P Q R S T U V W X Y Z "
."a b c d e f g h i j k l m n o p q r s t u v w x y z "
."0 1 2 3 4 5 6 7 8 9");
// questo ? il clico tramite si crea la password
for($i=0;$i<$length;$i++) {
srand((double)microtime()*8625342);
$foo = rand(0, 61);
$pass = $pass.$lettere[$foo];
}
$control = mysql_query("SELECT * FROM acquisti WHERE codeoff = '".$pass."'") or die (mysql_error());
if (mysql_num_rows($control) != 0) {
$esist = 0;
} else { $esist = 1; }
return $pass;
return $esist;
} */
Oppure con:
Codice PHP:
if(!function_exists("rPass")){ function rPass($length = 6) {
// Questo array contiene i caratteri con i quali
// saranno create le password
$lettere = explode(" ",
"A B C D E F G H I J K L M N O P Q R S T U V W X Y Z "
."a b c d e f g h i j k l m n o p q r s t u v w x y z "
."0 1 2 3 4 5 6 7 8 9");
// questo ? il clico tramite si crea la password
for($i=0;$i<$length;$i++) {
srand((double)microtime()*8625342);
$foo = rand(0, 61);
$pass = $pass.$lettere[$foo];
}
$control = mysql_query("SELECT * FROM acquisti WHERE codeoff = '".$pass."'") or die (mysql_error());
if (mysql_num_rows($control) != 0) {
$esist = 0;
} else { $esist = 1; }
return $pass;
return $esist;
}}
-
Fatal error: Maximum execution time of 30 seconds exceeded in /membri/programmerscommunity/confbuy.php on line 24
-
Cosa devo quindi fare visto che mi si presenta questo errore?
-
Che senso ha il do...while?
Comunque puoi riscrivere il codice per intero che stai usando?
Ciao!
-
Codice PHP:
<?php
session_start();
require_once('mysqlconn.php');
mysql_select_db($database, $offerte);
$bSuccesso = true;
$num = $_GET['num'];
$nome = $_POST['nome'];
$cognome = $_POST['cognome'];
$mail = $_POST['email'];
$nascita = $_POST['nascita'];
$provincia = $_POST['provincia'];
$indirizzo = $_indirizzo['indirizzo'];
$control = mysql_query("SELECT * FROM offerte WHERE num = '".$num."' ") or die(mysql_error());
$info = mysql_fetch_array($control);
$numoff = $info['numoff'];
if ($numoff == 0) {
echo "Non è possibile completare l'operazione: l'offerta richiesta è esaurita";
} else {
$numoff = $numoff - 1;
$countdown = mysql_query("UPDATE offerte SET numoff = '".$numoff."' WHERE num = '".$num."' ") or die(mysql_error());
//INIZIO GENERAZIONE CODICE
do {
mysql_select_db($database, $acquisti);
if(!function_exists("rPass")){ function rPass($length = 6) {
// Questo array contiene i caratteri con i quali
// saranno create le password
$lettere = explode(" ",
"A B C D E F G H I J K L M N O P Q R S T U V W X Y Z "
."a b c d e f g h i j k l m n o p q r s t u v w x y z "
."0 1 2 3 4 5 6 7 8 9");
// questo ? il clico tramite si crea la password
for($i=0;$i<$length;$i++) {
srand((double)microtime()*8625342);
$foo = rand(0, 61);
$pass = $pass.$lettere[$foo];
}
$control = mysql_query("SELECT * FROM acquisti WHERE codeoff = '".$pass."'") or die (mysql_error());
if (mysql_num_rows($control) != 0) {
$esist = 0;
} else { $esist = 1; }
return $pass;
return $esist;
}}
} while ( $esist == 0);
//FINE GENERAZIONE CODICE
mysql_select_db($database, $acquisti);
$buy = mysql_query("INSERT INTO acquisti (num, nome, cognome, email, nascita, provincia, indirizzo, codeoff) values ('".$num."', '".$nome."', '".$cognome."', '".$email."', '".$nascita."', '".$provincia."', '".$indirizzo."', '".$pass."')") or die (mysql_error());
if ($bSuccesso)
{
$intestazioni = "MIME-Version: 1.0\r\n";
$intestazioni .= "Content-type: text/html; charset=iso-8859-1\r\n";
$messaggio = "
<html><head><title>Codice acquisto</title></head>
<body>
<h1>Grazie per aver effettuato l'acquisto</h1><br><br>
Stampa questa mail e consegnala presso $azienda; il pagamento dovrà essere effettuato in loco secondo le modalità stabilite dall'azienda erogatrice dell'offerta.<br><br>
CODICE OFFERTA: $pass
</body></html>";
mail ($mail, "Codice acquisto", $messaggio, $intestazioni);
echo "Acquisto avvenuto con successo; una mail con il codice per attivare l'offerta è stata inviata alla tua casella di posta elettronica.";
header('Location: index.php');
mysql_select_db($databse, $offerte);
$countdown = mysql_query("UPDATE offerte SET numoff = (numoff - 1) WHERE num = '".$num."' ") or die(mysql_error());
}
else
{
echo '<p align="center">Spiacenti, è avvenuto un errore! Indietro per riprovare.</p>';
}
}
?>
-
Il do...while serve per far ripetere la generazione del codice fino a che questo non è diverso da uno già presente nel db
-
Ok. Ho riletto meglio il sorgente.
Non capisco perché dichiari la funzione "rPass()", e perché hai due return. Non puoi fare semplicemente così?:
Codice PHP:
//INIZIO GENERAZIONE CODICE
$length = 6;
// Questo array contiene i caratteri con i quali
// saranno create le password
$lettere = explode(" ",
"A B C D E F G H I J K L M N O P Q R S T U V W X Y Z "
."a b c d e f g h i j k l m n o p q r s t u v w x y z "
."0 1 2 3 4 5 6 7 8 9");
do {
// questo ? il clico tramite si crea la password
for($i=0;$i<$length;$i++) {
srand((double)microtime()*8625342);
$foo = rand(0, 61);
$pass = $pass.$lettere[$foo];
}
$control = mysql_query("SELECT * FROM acquisti WHERE codeoff = '".$pass."'") or die (mysql_error());
if (mysql_num_rows($control) != 0) {
$esist = 0;
} else { $esist = 1; }
} while ( $esist == 0);
//FINE GENERAZIONE CODICE
Inoltre non capisco perché selezioni più volte il database: il database è unico e le tabelle le selezioni dalla query.
Se ancora ti va in loop, probabilmente c'é qualche problema nella gestione del database.
p.s: occhio alle sql injection.
Ciao!
-