Jump to content

[SOLVED] Need Help. Query not working


marukochan

Recommended Posts

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 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: 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

Link to comment
Share on other sites

[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 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

$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]
Link to comment
Share on other sites

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?
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.