abhishekdeveloper Posted May 27, 2012 Share Posted May 27, 2012 Dear All, I have a website on which the users vote on a set of images. A script that generates a random code immediately after a particular user has finished voting on an image. Below is the script to generate and store this code in the table in the column titled CompletionCode. However, after running the script, I am unable to see the random number stored in my database in the CompletionCode column. Kindly help me out by suggesting changes/modifications: <?php $character_set_array = array( ); $character_set_array[ ] = array( 'count' => 5, 'characters' => 'abcdefghijklmnopqrstuvwxyz' ); $character_set_array[ ] = array( 'count' => 1, 'characters' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' ); $character_set_array[ ] = array( 'count' => 1, 'characters' => '0123456789' ); $character_set_array[ ] = array( 'count' => 1, 'characters' => '!@#$+-*&?:' ); $temp_array = array( ); foreach ( $character_set_array as $character_set ) { for ( $i = 0; $i < $character_set[ 'count' ]; $i++ ) { $temp_array[ ] = $character_set[ 'characters' ][ rand( 0, strlen( $character_set[ 'characters' ] ) - 1 ) ]; } } shuffle($temp_array); $output=implode($temp_array); echo "<b><b><P ALIGN=Center>$output</P></b></b>"; //this is where the code stores the output in the database $host="localhost"; // Host name $username="computat_xyz"; // Mysql username $password="1234"; // Mysql password $db_name="computat_pqr"; // Database name // Connect to server and select database. $handle = mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db($db_name,$handle)or die("cannot select DB"); $sql="INSERT INTO registration (CompletionCode) VALUES ('$output')"; mysql_close($handle); ?> Quote Link to comment https://forums.phpfreaks.com/topic/263224-help-regarding-storage-of-variable-in-database/ Share on other sites More sharing options...
BarryT06 Posted May 27, 2012 Share Posted May 27, 2012 Your just missing one piece. $result = mysql_query($sql); Just like this. <?php $character_set_array = array( ); $character_set_array[ ] = array( 'count' => 5, 'characters' => 'abcdefghijklmnopqrstuvwxyz' ); $character_set_array[ ] = array( 'count' => 1, 'characters' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' ); $character_set_array[ ] = array( 'count' => 1, 'characters' => '0123456789' ); $character_set_array[ ] = array( 'count' => 1, 'characters' => '!@#$+-*&?:' ); $temp_array = array( ); foreach ( $character_set_array as $character_set ) { for ( $i = 0; $i < $character_set[ 'count' ]; $i++ ) { $temp_array[ ] = $character_set[ 'characters' ][ rand( 0, strlen( $character_set[ 'characters' ] ) - 1 ) ]; } } shuffle($temp_array); $output=implode($temp_array); echo "<b><b><P ALIGN=Center>$output</P></b></b>"; //this is where the code stores the output in the database $host="localhost"; // Host name $username="computat_xyz"; // Mysql username $password="1234"; // Mysql password $db_name="computat_pqr"; // Database name // Connect to server and select database. $handle = mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db($db_name,$handle)or die("cannot select DB"); $sql="INSERT INTO registration (CompletionCode) VALUES ('$output')"; $result = mysql_query($sql); mysql_close($handle); ?> Barry Quote Link to comment https://forums.phpfreaks.com/topic/263224-help-regarding-storage-of-variable-in-database/#findComment-1349070 Share on other sites More sharing options...
abhishekdeveloper Posted May 28, 2012 Author Share Posted May 28, 2012 Hi Barry, Thank you for your reply. I incorporated the change that you mentioned. But I am still unable to see the change in my database against the particular user name in the respective table against the particular username. Please let me know if there are any other modifications required, below is my modified code: <?php $character_set_array = array( ); $character_set_array[ ] = array( 'count' => 5, 'characters' => 'abcdefghijklmnopqrstuvwxyz' ); $character_set_array[ ] = array( 'count' => 1, 'characters' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' ); $character_set_array[ ] = array( 'count' => 1, 'characters' => '0123456789' ); $character_set_array[ ] = array( 'count' => 1, 'characters' => '!@#$+-*&?:' ); $temp_array = array( ); foreach ( $character_set_array as $character_set ) { for ( $i = 0; $i < $character_set[ 'count' ]; $i++ ) { $temp_array[ ] = $character_set[ 'characters' ][ rand( 0, strlen( $character_set[ 'characters' ] ) - 1 ) ]; } } shuffle($temp_array); $output=implode($temp_array); echo "<b><b><P ALIGN=Center>$output</P></b></b>"; //this is where the code stores the output in the database $host="localhost"; // Host name $username="computat_abhi"; // Mysql username $password="1123581321"; // Mysql password $db_name="computat_rajesh"; // Database name // Connect to server and select database. $handle = mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db($db_name,$handle)or die("cannot select DB"); mysql_select_db("computat_rajesh",$handle); $sql="INSERT INTO registration (CompletionCode) VALUES ('$output')"; $result = mysql_query($sql); mysql_close($handle); ?> Quote Link to comment https://forums.phpfreaks.com/topic/263224-help-regarding-storage-of-variable-in-database/#findComment-1349078 Share on other sites More sharing options...
BarryT06 Posted May 28, 2012 Share Posted May 28, 2012 HI, From what I could tell there wasn't any other changes neccessary. Could you echo out your $sql variable onto your page? It will print exactly what it is sending to the DB. Copy the query it echoes and run it through PHPMyAdmin to see if it works there. I should add that I tried this with your code above and it worked once I added that line of code. I just had a different table_name and field_name, but everything else remained the same. EDIT: I tried the code you posted just there now and it works for me. Just had to change the DB details. Quote Link to comment https://forums.phpfreaks.com/topic/263224-help-regarding-storage-of-variable-in-database/#findComment-1349196 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.