Solved

string replace in upload sql

Posted on 2009-07-08
7
362 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 110

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 110

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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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 110

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 110

Expert Comment

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

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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…
This article discusses how to create an extensible mechanism for linked drop downs.
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.

623 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