Jump to content
Sign in to follow this  
Panta

Generate Code

Recommended Posts

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

Share this post


Link to post
Share on other sites

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>";  }	?>

Share this post


Link to post
Share on other sites

@RahXephonis diplaying lets 2 Pin number(s) have been generated but is not inserting into database

Edited by Panta

Share this post


Link to post
Share on other sites

Try removing the quotes from (`pin`)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...