Solved

string replace in upload sql

Posted on 2009-07-08
7
321 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

706 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

21 Experts available now in Live!

Get 1:1 Help Now