Reply
Another PHP/MySQL query I need help with
Old 03-10-2007, 03:23 PM Another PHP/MySQL query I need help with
Experienced Talker

Posts: 46
PHP Code:
// insert into database
include("encryptioninfo.php");

$query "INSERT INTO ".MYSQL_TBL_CLIENTS." SET $query_string, lastupdate=NOW(), date_referral=NOW()";

$query2 "Update ".MYSQL_TBL_CLIENTS." Set ssn=aes_encrypt(ssn,'$aes_key') where (????);

$result = mysql_query($query);
$result = mysql_query($query2); 
How do I do what I am trying to accomplish here? I have a long form where I want all of the values inserted into a DB, but I want the one field to be encrypted. I don't know how to do the AES_ENCRYPT with the first query because of the $query_string, so I thought you could run a second query when the form was submitted afterwards to encrypt the ssn value... But I don't know what to do for where. Each new entry is assigned an increasing ID with a bigint. Please help?

Last edited by bld44 : 03-10-2007 at 03:31 PM.
bld44 is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 03-10-2007, 04:00 PM Re: Another PHP/MySQL query I need help with
tripy's Avatar
Fetchez la vache!

Posts: 2,054
Name: Thierry
Location: In the void
Well, for a starter, you are mixing INSERT and UPDATE syntax:

insert:
Code:
INSERT INTO tblName
(filed1, field2,field3)
VALUES
(value1, value2, value3)
update:
Code:
UPDATE tblName
SET field1=value1, field2=value2, field3=value3
WHERE primaryKeyField=xxx
Secondly, you have an PHP syntax error, on the $query2 line.
PHP Code:
$query2 "Update ".MYSQL_TBL_CLIENTS." Set ssn=aes_encrypt(ssn,'$aes_key') where (????); 
should be:
PHP Code:
$query2 "Update ".MYSQL_TBL_CLIENTS." Set ssn=".aes_encrypt(ssn,$aes_key)." where (????);";
// !! Check the "ssn" parameter in the aes_encrypt function up there. 
// If it's not a constant, it will trigger an error. 
Now, what should your $query_string contain?
Is it the values of your form?

Then, you should compose your script differently. Hit the $_GET ot $_POST arrays directly.
You will found all the variables of your form there.
For exemple, if you have an input field named "ssn" in your form and are sumitting with a POST method:
PHP Code:
$ssn=$_POST['ssn']; 
Simply repeat this for all your forms fields (or use a foreach, or extract), and compose your query that way.

foreach example:
PHP Code:
/**
 * Does the same as extract(), but you could use the loop to do other  
 * actions. All the indexes in the $_POST array are traversed with this.
 * !!! the $$name is not a typo. It means "create a variable with the name as the content of $name".
 * So, if $name="heyYou", then it will create a variable $heyYou
 */
foreach($_POST as $name=>$value){
  $
$name=$value;

__________________
Listen to the ducky: "This is awesome!!!"


Last edited by tripy : 03-10-2007 at 04:03 PM.
tripy is offline
Reply With Quote
View Public Profile
 
Old 03-10-2007, 04:38 PM Re: Another PHP/MySQL query I need help with
Experienced Talker

Posts: 46
That doesn't really help considering the form works as is, $query_string is the values of the form fields.


Couldn't I just do a query like this and remove the value="$_POST[ssn]" from the SSN field?

PHP Code:
$query "INSERT INTO ".MYSQL_TBL_CLIENTS." SET $query_string, ssn='aes_encrypt('$_POST[ssn]','$aes_key')', lastupdate=NOW(), date_referral=NOW()"
bld44 is offline
Reply With Quote
View Public Profile
 
Old 03-10-2007, 05:46 PM Re: Another PHP/MySQL query I need help with
tripy's Avatar
Fetchez la vache!

Posts: 2,054
Name: Thierry
Location: In the void
Yes, you could too.
It's up to you to determine if it's secure enough.

Keep in mind that anyone with malicious mind could save your page on disk, and alter the form to post other fields.
Try to avoid SQL injection attacks too.
Believe me, they are real.
http://en.wikipedia.org/wiki/SQL_injection
__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Old 03-10-2007, 06:26 PM Re: Another PHP/MySQL query I need help with
Experienced Talker

Posts: 46
Hmm. What do you recommend to prevent against SQL Injection attacks? Restrict the input to alphanumerical chars only to run the query? I've never heard of that before and looked into it some.. pretty tricky.

Last edited by bld44 : 03-10-2007 at 06:30 PM.
bld44 is offline
Reply With Quote
View Public Profile
 
Old 03-10-2007, 07:39 PM Re: Another PHP/MySQL query I need help with
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
Wireless Audio
Posts: 2,320
Name: Keith Marshall
Location: West Hartford, CT
There is a good writeup on SQL Injection here in this forum:
http://www.webmaster-talk.com/php-fo...injection.html

Preventing SQL Injection is simply escaping any attempt to let the user "rewite" you query.

You can clean user input like: mysql_real_escape_string(stripslashes($_POST['ssn']))
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Another PHP/MySQL query I need help with
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.14208 seconds with 12 queries