marukochan Posted December 26, 2006 Share Posted December 26, 2006 Hi!,I am working on script where when a user request for a verification code, the script will generate the code, store it in the database and send the code to the user.However, the code is not being stored in table code in the database after it has been generated. Could anyone check my script. My script goes :<?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 numbersfunction 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 databasemysql_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: marukochan@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();?> Thanks Quote Link to comment Share on other sites More sharing options...
redarrow Posted December 26, 2006 Share Posted December 26, 2006 [code]<?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 numbersfunction 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$query="insert into code(`code_string`) values('$code') ";$result=mysql_query($query);// 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: marukochan@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();?> [/code] Quote Link to comment Share on other sites More sharing options...
mandrews81 Posted December 26, 2006 Share Posted December 26, 2006 Make sure your column length is long enough to hold the number of characters generated by $code and you may try assigning code to include the single quote like:[code]$code = "'" . generate_str(12) ."'";[/code]Regards,Max Quote Link to comment Share on other sites More sharing options...
marukochan Posted December 27, 2006 Author Share Posted December 27, 2006 Thanks for replying. I tried both by still my table is empty. Looking at the query, it should be storing the code in table code already. Before this script, I have another file which only generate the code and store it to the database. It worked then but when I tried it again it no longer working. It could not store the code. I did not change anything. What are the possibilities that could make the script working then but not now? Quote Link to comment Share on other sites More sharing options...
trq Posted December 27, 2006 Share Posted December 27, 2006 Try some debugging. ie;[code=php:0]$result=mysql_query($query) or die(mysql_error());[/code] Quote Link to comment Share on other sites More sharing options...
marukochan Posted December 28, 2006 Author Share Posted December 28, 2006 I was unable to access this site due to the earthquake in Taiwan yesterday. Now I'm back and I love you thorpe. My problem is solved. It was SQL syntax error. Quote Link to comment 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.