Panta Posted December 15, 2009 Share Posted December 15, 2009 Web masters please can you look into my script.i want a script that will generate code at random and insert it to my database.but my script is not working <? include 'config.php'; $submit=$_POST['Submit']; if($submit){$no = 1;while ($no <= $_POST['no'] ) { $pin = rand(1000000000,9999999999); mysql_query("INSERT INTO logincount (`pin`) VALUES ('$pin')") or die(mysql_error()); $no++; } echo "<font color='green'>". $_POST['no']. " Pin number(s) have been generated</font>";} ?><form name="form1" id="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <table border="0" cellpadding="3" cellspacing="3" width="62%"> <tbody> <tr> <td colspan="3" style="background-color: Silver;"><div align="center"> <strong><span style="font-size: 12pt;">Personal Identification Number Generator</span></strong> </div></td> </tr> <tr> <td width="66%" align="right">Please enter the number of PINS you want to generate. </td> <td width="20%"><input name="no" maxlength="16" id="no" /> </td> <td width="14%">(example, 20) </td> </tr> <tr> <td align="right"> </td> <td> </td> <td> </td> </tr> <tr> <td></td> <td style="text-align: left;"><input type="submit" name="Submit" value="Generate" /></td> <td></td> </tr> </tbody> </table></form><p> </p> will be waTIN Link to comment Share on other sites More sharing options...
Err Posted December 15, 2009 Share Posted December 15, 2009 I have never seen anyone use $_POST in a WHILE loop. That's probably what's causing your problem. Setting it to a variable before you use it would be better. Also, the way you have your WHILE loop set up, is exactly the way a FOR loop works, and looks muchcleaner. <?php include 'config.php'; $submit = $_POST['Submit']; $no = $_POST['no']; if ($submit) { for ($i = 1; i <= $no; $i++) { $pin = rand(1000000000,9999999999); mysql_query("INSERT INTO logincount (`pin`) VALUES ('$pin')") or die(mysql_error()); } echo "<font color='green'>$no Pin number(s) have been generated</font>"; } ?> Link to comment Share on other sites More sharing options...
Panta Posted December 15, 2009 Author Share Posted December 15, 2009 @RahXephonis diplaying lets 2 Pin number(s) have been generated but is not inserting into database Link to comment Share on other sites More sharing options...
Err Posted December 15, 2009 Share Posted December 15, 2009 Try removing the quotes from (`pin`) Link to comment Share on other sites More sharing options...
justsomeguy Posted December 15, 2009 Share Posted December 15, 2009 Anything from $_POST is a string, not a number. If you're going to be comparing it as a number you should use intval to convert it first. Right now you're comparing a number with a string instead of a number with a number. Link to comment Share on other sites More sharing options...
Panta Posted December 15, 2009 Author Share Posted December 15, 2009 Anything from $_POST is a string, not a number. If you're going to be comparing it as a number you should use intval to convert it first. Right now you're comparing a number with a string instead of a number with a number.So can you tell me how to convert it to number Link to comment Share on other sites More sharing options...
justsomeguy Posted December 15, 2009 Share Posted December 15, 2009 http://www.php.net/manual/en/function.intval.php Link to comment Share on other sites More sharing options...
clonetrooper9494 Posted December 16, 2009 Share Posted December 16, 2009 Concerning using intval(), in the past I have used $var+0 or $val*1, is there any difference? $val+0 is shorter... so I just always used that. Link to comment Share on other sites More sharing options...
justsomeguy Posted December 16, 2009 Share Posted December 16, 2009 I guess it's just a semantic issue, intval is specifically for converting a string to an integer where adding zero or multiplying by one seems like a hack, if you want to convert it to an integer might as well do it explicitly instead of as a side effect of automatic type casting. Future programmers looking at your code might not know what you're trying to do. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.