Jump to content

Help regarding storage of variable in database


Recommended Posts

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);
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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.

 

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.