Jump to content

mt_rand is producing the same number instead of randomizing


scmeeker

Recommended Posts

I've used this before and it worked fine but the mt_rand is not working properly.  It keeps producing the same number.

 

I've tested it on with the $_GET to the next page and it produces a unique code but it doesn't want to insert that number into the table, just the other same, repetitive number.

 

$transaction_id =  mt_rand() . mt_rand();
$item_email = mysql_real_escape_string($_POST[email]);
$item_first_name = mysql_real_escape_string($_POST[first_name]);
$item_last_name = mysql_real_escape_string($_POST[last_name]);
$item_street = mysql_real_escape_string($_POST[street]);
$item_street2 = mysql_real_escape_string($_POST[street_2]);
$item_city = mysql_real_escape_string($_POST[city]);
$item_state = mysql_real_escape_string($_POST[state]);
$item_zip_code = mysql_real_escape_string($_POST[zip_code]);
$item_country = mysql_real_escape_string($_POST[country]);
$username = $_SESSION['username'];
$item_id = $_GET['id'];
$item_title = mysql_real_escape_string($_POST[title]);
$item_price = mysql_real_escape_string($_POST[price]);
$item_shipping_cost = mysql_real_escape_string($_POST[shipping_cost]);
$item_in_shipping = mysql_real_escape_string($_POST[in_shipping]);
$okay = TRUE;

if ($item_country == 'United States'){
$shipping = "$item_shipping_cost";
}
else { $shipping = "$item_in_shipping    - <span class=\"submissionfont\">International Rate</span>";
}

$sql="INSERT INTO buyer (username, email, first_name, last_name, street, street_2, city, state, zip_code, country, transaction_id, date) VALUES ('$username', '$item_email', '$item_first_name', '$item_last_name', '$item_street', '$item_street2', '$item_city', '$item_state', '$item_zip_code', '$item_country', '$transaction_id', CURDATE())";

if (!mysql_query($sql))

  {

  die('Error: ' . mysql_error());

  }  
  
  $sql2="UPDATE product SET transaction_id='$transaction_id', sold='1', sold_date=CURDATE() WHERE id = '$item_id'";
  
if (!mysql_query($sql2))

  {

  die('Error: ' . mysql_error());

  } 

Link to comment
Share on other sites

When I run it, it does produce a new number when I tested it in the URL but it's not inserting this new random number into the "buyer" table.

 

Here is that section of the code from the previous code I posted:

 

$sql="INSERT INTO buyer (username, email, first_name, last_name, street, street_2, city, state, zip_code, country, transaction_id, date) VALUES ('$username', '$item_email', '$item_first_name', '$item_last_name', '$item_street', '$item_street2', '$item_city', '$item_state', '$item_zip_code', '$item_country', '$transaction_id', CURDATE())";

Link to comment
Share on other sites

It's inserting everything else into the table perfectly and outputting all the other information fine.  Like I said, I tested it with this line of code after the first two ($sql) &($sql2):

 

header("location:finalize.php?username=$username&id=$item_id&transaction_id=$transaction_id");

 

When doing this, on the next page it generated a new $transaction_id into the URL just fine.  It's just not inserting the new random number into the table....it keeps showing up as the same number (which I have no idea where it is getting that number from) even after deleting its presence from my database and resetting my server.

Link to comment
Share on other sites

$sql="INSERT INTO buyer (username, email, first_name, last_name, street, street_2, city, state, zip_code, country, transaction_id, date) VALUES ('$username', '$item_email', '$item_first_name', '$item_last_name', '$item_street', '$item_street2', '$item_city', '$item_state', '$item_zip_code', '$item_country', '$transaction_id', CURDATE())";
echo $sql;
die();

Link to comment
Share on other sites

mt_rand() . mt_rand() produces a rather large number and is probably exceeding your column definition.

 

Show us the value that is being inserted (seeing it might help someone help you), show us some of the values that are being generated for $transaction_id, and show us the column definition.

Link to comment
Share on other sites

Ok, well there you go then.... if transaction_id is an integer then you shouldn't have the quotes around it, as brianlange suggested.  However, you also shouldn't be doing this earlier:

 

$transaction_id =  mt_rand() . mt_rand();

 

You will easily overflow the integer range on a regular basis concatenating these numbers together.  On one of my servers the max number that can be returned from mt_rand() is a signed positive integer (21474836470, so mt_rand can return anything between 0 and that number which is already the largest number in the signed integer range.

 

 

Link to comment
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.