Solved

string replace in upload sql

Posted on 2009-07-08
7
351 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP 5.6 and 7.x 4 38
Adding additional variables to key => value association 17 37
Make check boxes work 8 41
How do I speed up this PDO query 4 13
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

762 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