Jump to content

Recommended Posts

So what i am trying to do is get a random number to be in the database and change each time a user goes to that page via the update statement but also show the user that random number.

hopefully if you press the refresh button a random number will come up diffrent but also entred in the database to one colum thanks.

This will display a 8 digit number.
[code]
<?php
//post rand_pass
$rand_pass=$_POST['var'];

// create a random number

$length    = 8;
$key_chars = '0123456789';
$rand_max  = strlen($key_chars) - 1;

for ($i = 0; $i < $length; $i++)
{
   $rand_pos  = rand(0, $rand_max);
   $rand_key[] = $key_chars{$rand_pos};
}

$rand_pass = implode('', $rand_key);

// show the random number to the user.
echo $rand_pass;

// insert into the database the first random number

$query = "insert into  valadate values( '$var')";
$results = mysql_query($query) or die("Failed on query!");


// check the number in the database to the one displayed and update with new number.

else{

$check_ver=mysql_query("SELECT * FROM valadate WHERE var='$rand_num' ");

if(mysql_num_rows($check_var = '$rand_num') ){

UPDATE valadate SET var = $rand_num;
}
}

?>
[/code]



Someone kindly give an example how to update a random number on a singles field and then display it and make it change on one field in the database and also change the number each time the page is called.
advance thank you.
Link to comment
https://forums.phpfreaks.com/topic/3999-random-number/
Share on other sites

[code]
do {
$random_min = 1; // change to your min
$random_max = 200; // change to your max.

$rand_no = rand($random_min, $random_max);

$updatedb = mysql_query("UPDATE `validate` SET `var` = $rand_num!);
} while(mysql_affected_rows() === 0);

echo $rand_no;
Link to comment
https://forums.phpfreaks.com/topic/3999-random-number/#findComment-13892
Share on other sites


thank you for the code but i got a error please help someone cheers.


This line.
} while(mysql_affected_rows() === 0);





[code]
<?

$sql_host =   'xxx';  //add host information here (localhost, mysql.host.com, etc.)
$sql_un   =   'xxx';  //add your user name here
$sql_pass =   'xxx';  //add your password here
$sql_db   =   'valadate';  //add your database name here


mysql_connect("$sql_host", "$sql_un", "$sql_pass") or
    die ("Could not connect to database");
mysql_select_db($sql_db) or

    die ("Could not select database");

do {
$random_min = 1; // change to your min
$random_max = 200; // change to your max.

$rand_no = rand($random_min, $random_max);

$updatedb = mysql_query("UPDATE `validate` SET `var` = $rand_num!);
} while(mysql_affected_rows() === 0);


echo $rand_no;
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/3999-random-number/#findComment-13899
Share on other sites

How can i get this to update when you enter a page automatickly thank you so much.
[code]
<?

$sql_host =   'xxx';  //add host information here (localhost, mysql.host.com, etc.)
$sql_un   =   'xxx';  //add your user name here
$sql_pass =   'xxx';  //add your password here
$sql_db   =   'valadate';  //add your database name here


mysql_connect("$sql_host", "$sql_un", "$sql_pass") or
    die ("Could not connect to database");
mysql_select_db($sql_db) or

    die ("Could not select database");



do {
$random_min = 1; // change to your min
$random_max = 2; // change to your max.

$rand_no = rand($random_min, $random_max);

$updatedb = mysql_query("UPDATE `valadate` SET `var` = $_post($rand_no!)");
} while(mysql_affected_rows() === 0);


echo $rand_no;
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/3999-random-number/#findComment-13909
Share on other sites

right sorry...

thought you had set up a table on your database called validate - but its the actual database that is called validate!!!

ok in the query line

$updatedb = mysql_query("UPDATE `valadate` SET `var` = $_post($rand_no!)");

change that to this

$updatedb = mysql_query("UPDATE `YOURTABLE` SET `FIELD` = $rand_no");

(don't know why you have that $_post($rand_no!) in - it won't work!)

now you need to replace TABLE with the name of the database table you are using to store this in, and FIELD by the name of the field in that table that you are storing this in.
Link to comment
https://forums.phpfreaks.com/topic/3999-random-number/#findComment-13930
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.