PHP file to GET variables, process them, INSERT them to a MySQL database table, help!
07-14-2008, 05:26 PM
|
PHP file to GET variables, process them, INSERT them to a MySQL database table, help!
|
Posts: 4
Name: K Jax
|
Hello,
This is my first time posting here. I need a php file that when called to via GET url with variables, the php file will have to encrypt a password, whichj will be one of the variables. Then I need the resulting info written to a specific MySQL database table.
So far this is what I have.
php GET variables and write to database code...
Code:
<?php
$db=mysql_connect("localhost", "usrnm", "pswd") or die("Could not connect to localhost."); mysql_select_db("visitors", $db) or die("Could not find visitors.");
// The above lines establishes a connection with the // database. Keep localhost as is unless something different // is mentioned by your sql host. usrnm is user name and pswd is // password. What I want to say is, copy these lines as they are // and just replace the required fields and it should connect.
$querySQL = "insert into visinfo (d_name, d_email,
d_city) values ($name, $email, $city)";
if(!$querySQL) error_message(sql_error());
// The above statement generates an error if you have setup the table in such a way that there should not be a duplicate entry.
?>
I can see how that will work. But what I don't see in the above code is how to clarify which table the data will be written to. I NEED TO SPECIFY TABLE.
Here is a bit of the code from the help file that encrypts the password with the correct encryption...
Code:
/**
* Formats a password using the current encryption.
*
* @access public
* @param string $plaintext The plaintext password to encrypt.
* @param string $salt The salt to use to encrypt the password. []
* If not present, a new salt will be
* generated.
* @param string $encryption The kind of pasword encryption to use.
* Defaults to md5-hex.
* @param boolean $show_encrypt Some password systems prepend the kind of
* encryption to the crypted password ({SHA},
* etc). Defaults to false.
*
* @return string The encrypted password.
*/
function getCryptedPassword($plaintext, $salt = '', $encryption = 'md5-hex', $show_encrypt = false)
{
// Get the salt to use.
$salt = JUserHelper::getSalt($encryption, $salt, $plaintext);
// Encrypt the password.
switch ($encryption)
{
case 'plain' :
return $plaintext;
case 'sha' :
$encrypted = base64_encode(mhash(MHASH_SHA1, $plaintext));
return ($show_encrypt) ? '{SHA}'.$encrypted : $encrypted;
case 'crypt' :
case 'crypt-des' :
case 'crypt-md5' :
case 'crypt-blowfish' :
return ($show_encrypt ? '{crypt}' : '').crypt($plaintext, $salt);
case 'md5-base64' :
$encrypted = base64_encode(mhash(MHASH_MD5, $plaintext));
return ($show_encrypt) ? '{MD5}'.$encrypted : $encrypted;
case 'ssha' :
$encrypted = base64_encode(mhash(MHASH_SHA1, $plaintext.$salt).$salt);
return ($show_encrypt) ? '{SSHA}'.$encrypted : $encrypted;
case 'smd5' :
$encrypted = base64_encode(mhash(MHASH_MD5, $plaintext.$salt).$salt);
return ($show_encrypt) ? '{SMD5}'.$encrypted : $encrypted;
case 'aprmd5' :
$length = strlen($plaintext);
$context = $plaintext.'$apr1$'.$salt;
$binary = JUserHelper::_bin(md5($plaintext.$salt.$plaintext));
for ($i = $length; $i > 0; $i -= 16) {
$context .= substr($binary, 0, ($i > 16 ? 16 : $i));
}
for ($i = $length; $i > 0; $i >>= 1) {
$context .= ($i & 1) ? chr(0) : $plaintext[0];
}
$binary = JUserHelper::_bin(md5($context));
for ($i = 0; $i < 1000; $i ++) {
$new = ($i & 1) ? $plaintext : substr($binary, 0, 16);
if ($i % 3) {
$new .= $salt;
}
if ($i % 7) {
$new .= $plaintext;
}
$new .= ($i & 1) ? substr($binary, 0, 16) : $plaintext;
$binary = JUserHelper::_bin(md5($new));
}
$p = array ();
for ($i = 0; $i < 5; $i ++) {
$k = $i +6;
$j = $i +12;
if ($j == 16) {
$j = 5;
}
$p[] = JUserHelper::_toAPRMD5((ord($binary[$i]) << 16) | (ord($binary[$k]) << 8) | (ord($binary[$j])), 5);
}
return '$apr1$'.$salt.'$'.implode('', $p).JUserHelper::_toAPRMD5(ord($binary[11]), 3);
case 'md5-hex' :
default :
$encrypted = ($salt) ? md5($plaintext.$salt) : md5($plaintext);
return ($show_encrypt) ? '{MD5}'.$encrypted : $encrypted;
}
}
So that's that. I am at a point where I could use the first piece of code posted above, except I can't figure how to specify the table. And as far as the encrypting of the password... I am totally lost.
Please help.
All the best,
~Slim~
Oh... attached is the text file with all the code need for this file, I guess 
|
|
|
|
07-14-2008, 07:13 PM
|
Re: PHP file to GET variables, process them, INSERT them to a MySQL database table, h
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
The sql syntax for an insert allows you to name the table as shown:
Code:
INSERT INTO table_name
(fieldname1, fieldname2, fieldname3)
VALUES (
'value1',
'value2',
'value3'
)
So in your example, it would look similar to:
Code:
INSERT INTO visinfo
(d_name, d_email, d_city)
VALUES (
'$name',
'$email',
'$city'
)
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
07-14-2008, 07:52 PM
|
Re: PHP file to GET variables, process them, INSERT them to a MySQL database table, h
|
Posts: 4
Name: K Jax
|
Thanks for the reply mrgraphic.
This is an update to where I am with this PHP problem.
Ok, so I am going to give it a shot. Thanks to mrgapic's (and other's) replies... this is what I have now.
Code:
<?php
$db=mysql_connect("localhost", "usrnm", "pswd") or die("Could not connect to localhost."); mysql_select_db("sha0818911585312", $db) or die("Could not find visitors.");
$querySQL = "insert into jos_users (id,name,email,username,password,usertype,registerDate) values (--,--,$email,$username,$password,--,$start_date)";
if(!$querySQL) error_message(sql_error());
?>
I am understanding that the VALUES (values (--,--,$email,$username,$password,--,$start_date) came from the GET url that's going to call the PHP file.
Here is another spin on things...
1. The id variable won't be coming from the GET url string, so I will have to set that variable via the PHP file AND I will need it set at auto_incrememnts and it can't be a duplicate on the database. Maybe I could go in and add that to the database before hand, say at like 300 - 500 ready id's?
2. I left the value for name blank as well, because coming from the GET url I will have two varibles for name (customer_fname "first name" and customer_lname "last name") so I will need to know how to combine the two and make them into the one value.
3. I left the usertype value blank. All entries to the database thru this file will all be one type of user. the default for this value is going to be registered
4. Finally, the password will be coming in the GET url string already encrypted in .htpasswd. I will need to decrypt it first, then encrypt it with the proper method.
If I can figure out these last 4 steps, I can go ahead and start testing this!
All the best,
~Slim~
|
|
|
|
07-14-2008, 10:14 PM
|
Re: PHP file to GET variables, process them, INSERT them to a MySQL database table, h
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
For step 1: You want to set your database table id field to auto increment. MySQL will do this automatically creating a new id record for each new insert. Once you do this, you don't even need to set it on the query string as it will be automated for you. After you have executed the query, you can use PHP function mysql_insert_id() to return the new id.
Step 2: You can apend the first and last name strings together with a space.
Step 3: Set the usertype as a mysql table field default value, or you can pass the value directly.
Step 4: I'm not sure about the encrypted password by the htpassword, but you can encrypt the "plaintext" password by the class method or using PHP function md5().
Also... You must quote you input values (for string types) for MySQL or you will encounter an error. You may also want to protect your input values from nasties by using PHP function mysql_real_escape_string(). The date field can be set as a datetime type too.
So for an example, you might want to set your database table something like this:
Code:
CREATE TABLE `jos_users` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 100 ) NOT NULL ,
`email` VARCHAR( 100 ) NOT NULL ,
`username` VARCHAR( 50 ) NOT NULL ,
`password` VARCHAR( 32 ) NOT NULL ,
`usertype` VARCHAR( 50 ) NOT NULL ,
`registerDate` DATETIME NOT NULL ,
PRIMARY KEY ( `id` ) ,
INDEX ( `email` , `username` , `password` )
);
And here is a good PHP example of how to insert your values to a new row:
PHP Code:
$JUser = new JUserHelper; mysql_query(" INSERT INTO jos_users (name, email, username, password, usertype, registerDate) VALUES ( '" . mysql_real_escape_string($_GET['customer_fname'] . ' ' . $_GET['customer_lname']) . "', '" . mysql_real_escape_string($email) . "', '" . mysql_real_escape_string($username) . "', '" . $JUser->getCryptedPassword($password) . "', 'registered', NOW() ) "); // Get the new id $id = mysql_insert_id();
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
07-15-2008, 02:08 AM
|
Re: PHP file to GET variables, process them, INSERT them to a MySQL database table, h
|
Posts: 4
Name: K Jax
|
Okay... this is what I am going to test with.
Code:
<?php
$db=mysql_connect("localhost", "usrnm", "pswd") or die("Could not connect to localhost."); mysql_select_db("sha0818911585312", $db) or die("Could not find visitors.");
$encrypted_pass = md5($password);
$usertype = Registered;
$username = $customer_fname.' '.$customer_lname;
$querySQL = "insert into jos_users (name,email,username,password,registerDate) values ($name,$email,$username,$encrypted_pass,$start_date)";
if(!$querySQL) error_message(sql_error());
?>
Come to find out, the password will be pased as plain text. So that means I don't have to worry about decrypting it, just adding the md5 spin on it. As is .htpasswd encryption can't be decrypted. It's a one way algorithm.
If you see ANYTHING that may keep my test from being succesful, please let me know.
Thanks for your replies.
All the best,
~Slim~
|
|
|
|
07-15-2008, 12:45 PM
|
Re: PHP file to GET variables, process them, INSERT them to a MySQL database table, h
|
Posts: 4
Name: K Jax
|
This is the code I have tested, and it's not working.
Code:
<?php
$db=mysql_connect("myhostname", "myusername", "password") or die("Could not connect to localhost");
$encrypted_pass = md5($password);
$username = "$customer_fname.' '.$customer_lname";
$querySQL = "insert into jos_users (name,email,username,password,registerDate) values ($name,$email,$username,$encrypted_pass,$start_date)";
if(!$querySQL) error_message(sql_error());
?>
I have to figure out why and I don't know where to start.
All the best,
~Slim~
|
|
|
|
07-15-2008, 06:58 PM
|
Re: PHP file to GET variables, process them, INSERT them to a MySQL database table, h
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
Two things are wrong:
1. You must quote your input values or you will return a MySQL error.
2. (This is why it's not working) You are only setting a query string and it is not being passed to MySQL to run. You must pass to PHP function mysql_query()
I highly suggest you go through some basic online tutorials to learn the relationship and methods between PHP and MySQL. Two sites I know of that provide a great learner's tutorial is www.w3schools.com and www.htmlgoodies.com
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
|
« Reply to PHP file to GET variables, process them, INSERT them to a MySQL database table, help!
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|