Solved

string replace in upload sql

Posted on 2009-07-08
7
331 Views
Last Modified: 2012-05-07
I'm uploading images to my server and also using those form fields to update my database. Because of the way the front end is already built, I need to strip the file extensions before they update my database. In other words I need "myimage" not "myimage.jpg".

It looks like I need to use string replace but I'm not savvy enough to pull it off - how would i add a string repliace to this update statement?l
$updateSQL = "UPDATE ".$thumbs_table." SET

".((trim($_POST['thumb1'])) != '' ? "thumb1=".GetSQLValueString($_POST['thumb1'], "text")."," : "")."

".((trim($_POST['thumb2'])) != '' ? "thumb2=".GetSQLValueString($_POST['thumb2'], "text")."," : "")."

".((trim($_POST['thumb3'])) != '' ? "thumb3=".GetSQLValueString($_POST['thumb3'], "text")."," : "")."

".((trim($_POST['thumb4'])) != '' ? "thumb4=".GetSQLValueString($_POST['thumb4'], "text")."," : "")."

description=".GetSQLValueString($_POST['description'], "text").",

DrID=".GetSQLValueString($_POST['DrID'], "text")."

WHERE case_id=".GetSQLValueString($_POST['case_id'], "int");

Open in new window

0
Comment
Question by:phillystyle123
  • 4
  • 3
7 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 24813022
Insert this "foreach" iterator before the query creation.  It will look for anything in the posted form that is named starting with "thumb."  For those values it will remove the suffix ".jpg" from the end and it will replace those values with the new, truncated, name.

HTH, ~Ray
foreach ($_POST as $key => $value)

{

    if (substr($key,0,5) == 'thumb')

    {

        $value = eregi_replace("\.jpg$", '', $value);

        $_POST[$key] = $value;

    }

}
 

$updateSQL = "UPDATE ".$thumbs_table." SET

".((trim($_POST['thumb1'])) != '' ? "thumb1=".GetSQLValueString($_POST['thumb1'], "text")."," : "")."

".((trim($_POST['thumb2'])) != '' ? "thumb2=".GetSQLValueString($_POST['thumb2'], "text")."," : "")."

".((trim($_POST['thumb3'])) != '' ? "thumb3=".GetSQLValueString($_POST['thumb3'], "text")."," : "")."

".((trim($_POST['thumb4'])) != '' ? "thumb4=".GetSQLValueString($_POST['thumb4'], "text")."," : "")."

description=".GetSQLValueString($_POST['description'], "text").",

DrID=".GetSQLValueString($_POST['DrID'], "text")."

WHERE case_id=".GetSQLValueString($_POST['case_id'], "int");

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24813040
If you also wanted to remove '.png' suffixes, you would add a statement like this to the script after line 5.  You could follow a similar pattern for other suffixes you might want to remove.

Best regards, ~Ray
        $value = eregi_replace("\.png$", '', $value);

Open in new window

0
 

Author Comment

by:phillystyle123
ID: 24813494
hi ray - thanks so much for this! just so i'm straight:

will this iterator remove ".jpg" from all records i'm inserting using this form? in other words, if i insert "bla.jpg" will the ".jpg" be stripped or does it have to be "thumbbla.jpg" ?

foreach ($_POST as $key => $value)
{
    if (substr($key,0,5) == 'thumb')
    {
        $value = eregi_replace("\.jpg$", '', $value);
        $_POST[$key] = $value;
    }
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Closing Comment

by:phillystyle123
ID: 31601592
this worked perfectly! thanks so much for the help ray!

foreach ($_POST as $key => $value)
{
    if (substr($key,0,5) == 'thumb')
    {
        $value = eregi_replace("\.jpg$", '', $value);
        $_POST[$key] = $value;
    }
}
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24814205
The $_POST array is a superglobal associative array.  Let's deconstruct that terminology.

"Superglobal" means that it is available in ALL namespaces and variable scope.  You can use it inside classes and functions without an special consideration - just address it.

"Associative" means that an array has textual keys associated with each value.

Read up on the foreach() command on the PHP web site.  It shows you how to work with associative arrays.  The code above tests to see if the KEY starts with the character string "thumb."  The KEY comes from the HTML form. It is the "name=" in the <input type=text> statement.

Does that help clear it up?

Best, ~Ray
0
 

Author Comment

by:phillystyle123
ID: 24814298
Thanks for the information and the help Ray  -much appreciated as always.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24814324
Gladly!  Carry on ;-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now