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: [email protected]";

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
https://forums.phpfreaks.com/topic/31864-solved-need-help-query-not-working/
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: [email protected]";

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.