Results 1 to 4 of 4

Thread: Why my Query Not Working??

  1. #1
    Join Date
    Aug 2006
    Location
    Seri Kembangan
    Posts
    12
    Rep Power
    0

    Why my Query Not Working??

    I have a script that when the user request for verification code, it will generate the verification code, store the verification code in the database and send the code to the user.

    However, the code is not stored in the database. Can anybody check my script?

    My script is :

    <?php
    $title = "Process Request";
    include ("header_footer_function.php");

    header_start();

    // Connect to database to store verification code
    mysql_connect("localhost", "root", "admin") or die(mysql_error());
    mysql_select_db("project_db") or die(mysql_error());


    // Function to generate random alphabets and numbers
    function generate_str ($length) {
    $characters = array('a','b','c','d','e','f','g','h','i','j','k', 'l','m','n','o','p','q','r','s','t','u','v','w','x ','y','z','0','1','2','3','4','5','6','7','8','9') ;
    $random_str = "";
    for ($i = 0; $i < $length; $i++) {
    srand((double)microtime()*1000000);
    $random_chr = round(rand(0, count($characters)-1));
    $random_str .= $characters[$random_chr];
    }
    return ($random_str);
    }

    $code = generate_str(12);

    // Store code in database
    mysql_query("INSERT INTO code (code_string) VALUES ('$code')");

    // Send e-mail
    $subject = "Verification Code Request";
    $email = $_POST['email'] ;
    $name = $_POST['name'];
    $company = $_POST['company'];
    $message = "Name: $name\r\n"."Company: $company\r\n"."Verification Code: $code\r\n";
    $headers = "From: xxxxx@xxx.com.my";

    ini_set("SMTP","mail.xxx.com.my");
    $sent = mail($email, $subject, $message, $headers) ;
    if($sent)
    {print "Please check your e-mail for verification code"; }
    else
    {print "We encountered an error sending your mail"; }

    footer();
    ?>

  2. #2
    Join Date
    Jul 2006
    Location
    My
    Posts
    967
    Rep Power
    0
    please check your db table field name is correct or you may discard the field name in the insert query by using this code

    mysql_query("INSERT INTO code VALUES ('$code')");

  3. #3
    Join Date
    Jan 2007
    Location
    Sarawak
    Posts
    9
    Rep Power
    0
    Plz check if your value for $code fix into the column code_string.

  4. #4
    Join Date
    Aug 2006
    Location
    Malaysia
    Posts
    1,576
    Rep Power
    191
    For tracing/debugging purpose, you may try to create a "testing" script just to test that particular lines of codes, i.e. executing the query.
    - Validate the SQL query syntax, the table name, field name and field length against the input length etc.
    - Or even, display out the SQL query and plug the query into the SQL front-end tool or the SQL console to manually execute the statement to ensure it is correct.

    Suggest you can wrap up the lines that execute the SQL query as a method, in line with the concept of DAL (Data Access Layer) instead of scattering the SQL queries everywhere in different scripts, so that when it can be called in the testing script and then later reused in the development or production script.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Query Vs Array
    By semutpower in forum Website Programming
    Replies: 2
    Last Post: 30-01-2009, 02:44 PM
  2. Need help : send & query image in php
    By mimosa in forum Website Programming
    Replies: 11
    Last Post: 04-11-2007, 11:53 PM
  3. Help in query..
    By sweets in forum Website Programming
    Replies: 10
    Last Post: 27-03-2007, 10:39 AM
  4. Sql query untuk A+B-C ...
    By nixx in forum Website Programming
    Replies: 5
    Last Post: 06-10-2004, 04:51 PM
  5. how to secured query string
    By vash in forum Website Programming
    Replies: 3
    Last Post: 28-04-2004, 02:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Search Engine Optimization by vBSEO 3.5.0 RC1 PL1

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112