doulu1020
2015-01-05 05:04
浏览 38
已采纳

PHP页面在第一次按键点击时没有加载生成的值?

I have a single page php application to generate an 8 digit number, user inputs a value and page generates another value, both values are inserted into mysql db and displaying the generated value in an input box at a single button click. Insertion is happening, but generated value is not displaying in the inputbox at the first time, I also tried session its not helping too.

here is my php code,

<?php
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<div>
<?php
if(isset($_POST['luckyButton']) && $_POST['myLuckyNumber'] != ""){
    //sleep(20);
    $myLuckyNum = $_POST['myLuckyNumber'];
    $resultmyLuckyNum = mysql_query("SELECT * FROM lucky where luckyNum='$myLuckyNum'") or die(mysql_error());
    if (mysql_num_rows($resultmyLuckyNum) > 0) {
        while ($rowmyLuckyNum = mysql_fetch_array($resultmyLuckyNum)) {
            $generatedNumber = $rowmyLuckyNum["generatedNum"];
        }
    }else{
        $gen = getGen();
        $resultGenNum = mysql_query("INSERT INTO lucky (slno, luckyNum, generatedNum) VALUES ( NULL, '$myLuckyNum', '$gen')");
        if (mysql_num_rows($resultGenNum) > 0) {
            $generatedNumber = $gen;
        }
    }
}
?>
<form action="" method="post">
<input type="text"class="inputs" name="myLuckyNumber" onClick="this.select();" placeholder="Enter You Lucky Number" value="<?php echo $myLuckyNum; ?>" />
<input type="submit" class="css_button" name="luckyButton" value="Check"/>
</form>
</div>
<br><br><br>
<?php
if($generatedNumber != ""){
    echo '<input type="text" name="myLuckyNumber" onClick="this.select();" id="generatedNumber" readonly="true" value="' . $generatedNumber . '" />';
}
echo '<p id="errText"> ' . $err . ' </p>';
function random_string($length) {
    $key = '';
    $keys = array_merge(range(0, 9));
    for ($i = 0; $i < $length; $i++) {
        $key .= $keys[array_rand($keys)];
    }
    return $key;
}
function getGen(){
    $gen1 = random_string(8);
    $result = mysql_query("SELECT * FROM lucky where generatedNum='$gen1'") or die(mysql_error());
    if (mysql_num_rows($result) > 0) {
        while ($row = mysql_fetch_array($result)) {
            if($row["generatedNum"] == $gen1){
                getGen();
            }
        }
    }else{
        //echo $gen1;
        return($gen1);
    }
}
?>
</body>
</html>

my table,.

CREATE TABLE `lucky` (
`slno` int(11) NOT NULL,
  `luckyNum` text NOT NULL,
  `generatedNum` text NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanlu1279 2015-04-08 07:12
    已采纳

    The problem with your code is that mysql_num_rows will return either 0 or 1, use it like

    if($resultGenNum === 1){
           $generatedNumber = $gen;
    }
    
    点赞 评论
  • douliang2935 2015-01-05 05:11

    Instead of using if ( $generatedNumber != "" ), perhaps you should try something like if ( isset( $generatedNumber ) ).

    If $generatedNumber doesn't exist (which it doesn't if nothing was posted to the script), then the comparison will fail (in fact, you're probably getting a PHP error).

    Of course, that leads us to the next problem - if nothing is posted to the script, you don't generate a number. So of course, if you don't generate a number, there is no number to display. So you should structure your code in such a way that a random number is always generated - regardless of whether a post variable was received or not.

    点赞 评论
  • doutou7740 2015-01-05 05:38

    You are using mysql_num_rows in a wrong way. When you run insert query, it does not return rows, it returns true or false (Boolean). When it will return true, you will get 1 (as $resultGenNum). use this instead:

     if ($resultGenNum === 1){
           $generatedNumber = $gen;
     }
    
    点赞 评论

相关推荐 更多相似问题