Solved

string replace in upload sql

Posted on 2009-07-08
7
337 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 109

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 109

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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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 109

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 109

Expert Comment

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

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

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…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

816 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

8 Experts available now in Live!

Get 1:1 Help Now