Solved

Ignoring an emtpy field

Posted on 2007-03-25
17
247 Views
Last Modified: 2008-03-06
How can I make this sql statement ignore the variable strImage if it is empty so that it won't overwrite what is in the db with an empty value?

$SQL = mysql_query("UPDATE tblAdspace SET "
       . "strProviderservice = '" . $strProviderservice . "', "
     . "strCompanyname='" . $strCompanyname . "', "
     . "strOwner='" . $strOwner . "', "
       . "strAddress='" . $strAddress . "', "
     . "strTown='" . $strTown . "', "
       . "strZipcode='" . $strZipcode . "', "
       . "strPhone='" . $strPhone . "', "
     . "str2ndphone='" . $str2ndphone . "', "
     . "strMobile='" . $strMobile . "', "
     . "strPager='" . $strPager . "', "
     . "strFax='" . $strFax . "', "
     . "strEmail='" . $strEmail . "', "
     . "strWebsite='" . $strWebsite . "', "
     . "strLicense='" . $strLicense . "', "
     . "strInsured='" . $strInsured . "', "
     . "strBonded='" . $strBonded . "', "
     . "strHours='" . $strHours . "', "
     . "str24houremerg='" . $str24houremerg . "', "
     . "strOtherservices='" . $strOtherservices . "', "
     . "strServicearea='" . $strServicearea . "', "
     . "strInbusiness_since='" . $strInbusiness_since . "', "
     . "strServicesoffered='" . $strServicesoffered . "', "
       . "strFreeestimate='" . $strFreeestimate . "', "
     . "strWorkguaranteed='" . $strWorkguaranteed . "', "
       . "strProvidertagline='" . $strProvidertagline . "', "
     . "strAd_size='" . $strAd_size . "', "
       . "strImage='" . $target_path . "'"
     . " WHERE strCompanyname = '" . $strCompanyname . "'");
0
Comment
Question by:pingeyeg
  • 10
  • 6
17 Comments
 
LVL 20

Expert Comment

by:steelseth12
ID: 18788073
$SQL = mysql_query("UPDATE tblAdspace SET "
       . "strProviderservice = '" . $strProviderservice . "', "
     . "strCompanyname='" . $strCompanyname . "', "
     . "strOwner='" . $strOwner . "', "
       . "strAddress='" . $strAddress . "', "
     . "strTown='" . $strTown . "', "
       . "strZipcode='" . $strZipcode . "', "
       . "strPhone='" . $strPhone . "', "
     . "str2ndphone='" . $str2ndphone . "', "
     . "strMobile='" . $strMobile . "', "
     . "strPager='" . $strPager . "', "
     . "strFax='" . $strFax . "', "
     . "strEmail='" . $strEmail . "', "
     . "strWebsite='" . $strWebsite . "', "
     . "strLicense='" . $strLicense . "', "
     . "strInsured='" . $strInsured . "', "
     . "strBonded='" . $strBonded . "', "
     . "strHours='" . $strHours . "', "
     . "str24houremerg='" . $str24houremerg . "', "
     . "strOtherservices='" . $strOtherservices . "', "
     . "strServicearea='" . $strServicearea . "', "
     . "strInbusiness_since='" . $strInbusiness_since . "', "
     . "strServicesoffered='" . $strServicesoffered . "', "
       . "strFreeestimate='" . $strFreeestimate . "', "
     . "strWorkguaranteed='" . $strWorkguaranteed . "', "
       . "strProvidertagline='" . $strProvidertagline . "', "
     . "strAd_size='" . $strAd_size . "', "
       . "strImage='" . (isset($target_path)?$target_path:"") . "'"
     . " WHERE strCompanyname = '" . $strCompanyname . "'");
0
 
LVL 28

Expert Comment

by:gamebits
ID: 18788090
Since this is an update statement you must be pulling the information from the db in the first place (probably in a form to edit the values and then resubmit to the updating script).

What I'm getting at, is the field strImage should not be empty to start with when you resubmit or if it is  then you do not replace anything with a empty value.
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 18788127
steelseth12, that didn't work.  It still overwrote the image name in the db.
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 18788132
gamebits, the field strImage is a button that imports your image from your hdd and then inserts it into your database.  Well, when you already have one in the db, and I don't select another image, but I do make a change to the provider, the strImage field gets overwritten will a blank value.  I want to ignore that field if it is empty.
0
 
LVL 28

Expert Comment

by:gamebits
ID: 18788137
To make the changes you must have form, is it not?
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 18788139
Correct,

<tr><td align="right">
Select Custom Ad:
</td><td>
<input type="file" name="strImage">
</td></tr>
0
 
LVL 28

Expert Comment

by:gamebits
ID: 18788156
I don't kow how your application is set up but when I write a  application and I have to edit information in the db
I have a form who will pull all the information from the db, all the field are being displayed in the form, the ones that can be edited are displayed in a textbox (if applicable), the ones that cannot be edited (don't want them to be editable, like order numbers etc.) are displayed as regular text BUT they do have a hidden field to reinsert the info into the db upon submitting the update.

The other way around is to have only the fields you want to edit to be listed in your update statement

$SQL = mysql_query("UPDATE tblAdspace SET "
. "strZipcode='" . $strZipcode . "', "
 . " WHERE strCompanyname = '" . $strCompanyname . "'");


this would only change the zipcode in the db for this company nothing else would be touch, so if you know you will not change the strImage you can safely remove it from the list but if you ever have to change it this would not work.

0
 
LVL 1

Author Comment

by:pingeyeg
ID: 18788166
Yea, see that's not what I want to have happen.  What I want is if the field is empty, because the user didn't select an image from their computer, for the UPDATE statement to not look at it and keep on truckin.  If there is a value in that field then I want the UPDATE statement to take action on that field.  Does that make since?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 28

Accepted Solution

by:
gamebits earned 500 total points
ID: 18788189
Since updating an image involve uploading the image to the server and inserting the path in the db, I think the easiest way to do so would be to have a separate form to do that, one form to update all the text field and one form to change the picture.
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 18788289
So then I would have to have two different action pages.
0
 
LVL 28

Expert Comment

by:gamebits
ID: 18788296
No you could have both on the same action page using a switch statement, let me know if you need an example.
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 18788361
I did what you said, but right now when I try to insert an image i get a blank page:

<?php
if ($_GET['text'] == "text") {

if ($_SERVER['REQUEST_METHOD'] == "POST") {

$conn = mysql_connect("127.0.0.1", "", "");
mysql_select_db("providers", $conn)
or die(mysql_error());

$strProviderservice = $_POST['strProviderservice'];
$strCompanyname = $_POST['strCompanyname'];
$strOwner = $_POST['strOwner'];
$strAddress = $_POST['strAddress'];
$strTown = $_POST['strTown'];
$strZipcode = $_POST['strZipcode'];
$strPhone = $_POST['strPhone'];
$str2ndphone = $_POST['str2ndphone'];
$strMobile = $_POST['strMobile'];
$strPager = $_POST['strPager'];
$strFax = $_POST['strFax'];
$strEmail = $_POST['strEmail'];
$strWebsite = $_POST['strWebsite'];
$strInbusiness_since = $_POST['strInbusiness_since'];
$strLicense = $_POST['strLicense'];
$strInsured = $_POST['strInsured'];
$strBonded = $_POST['strBonded'];
$strHours = $_POST['strHours'];
$str24houremerg = $_POST['str24houremerg'];
$strServicesoffered = $_POST['strServicesoffered'];
$strOtherservices = $_POST['strOtherservices'];
$strServicearea = $_POST['strServicearea'];
$strFreeestimate = $_POST['strFreeestimate'];
$strWorkguaranteed = $_POST['strWorkguaranteed'];
$strProvidertagline = $_POST['strProvidertagline'];
$strAd_size = $_POST['strAd_size'];

addslashes($strOtherservices);
addslashes($strServicesoffered);
addslashes($strProvidertagline);

$SQL = mysql_query("SELECT * FROM tblAdspace where strCompanyname = '" .$strCompanyname. "'")
or die(mysql_error());

if ($row = mysql_fetch_array($SQL)) {

$SQL = mysql_query("UPDATE tblAdspace SET "
       . "strProviderservice = '" . $strProviderservice . "', "
     . "strCompanyname='" . $strCompanyname . "', "
     . "strOwner='" . $strOwner . "', "
       . "strAddress='" . $strAddress . "', "
     . "strTown='" . $strTown . "', "
       . "strZipcode='" . $strZipcode . "', "
       . "strPhone='" . $strPhone . "', "
     . "str2ndphone='" . $str2ndphone . "', "
     . "strMobile='" . $strMobile . "', "
     . "strPager='" . $strPager . "', "
     . "strFax='" . $strFax . "', "
     . "strEmail='" . $strEmail . "', "
     . "strWebsite='" . $strWebsite . "', "
     . "strLicense='" . $strLicense . "', "
     . "strInsured='" . $strInsured . "', "
     . "strBonded='" . $strBonded . "', "
     . "strHours='" . $strHours . "', "
     . "str24houremerg='" . $str24houremerg . "', "
     . "strOtherservices='" . $strOtherservices . "', "
     . "strServicearea='" . $strServicearea . "', "
     . "strInbusiness_since='" . $strInbusiness_since . "', "
     . "strServicesoffered='" . $strServicesoffered . "', "
       . "strFreeestimate='" . $strFreeestimate . "', "
     . "strWorkguaranteed='" . $strWorkguaranteed . "', "
       . "strProvidertagline='" . $strProvidertagline . "', "
     . "strAd_size='" . $strAd_size . "'"
     . " WHERE strCompanyname = '" . $strCompanyname . "'");
                    
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n".
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n".
"<head>\n".
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" />\n".
"<title>Ask2bsure.com - (Admin Section) Edit Provider</title>\n".
"<link style=\"text/css\" href=\"/css/style.css\" rel=\"stylesheet\">\n".
"</head>\n".
"<body>\n".
"<table align=\"center\" id=\"admin\">\n".
"<tr><td align=\"center\" colspan=\"2\">\n".
"<img src=\"/images/sublogo_2.png\"><br />\n".
"<span style=\"font-size: 12px; color: #0000FF\">(Admin Section)</span>\n".
"</td></tr>\n".
"<tr><td valign=\"top\">\n".
"<div id=\"navlist\">\n".
"<a href=\"/admin/\">Home</a>\n".
"<a href=\"/admin/add_provider.php\">Add Provider</a>\n".
"<a href=\"/admin/list_providers.php?list=list\">Delete / Edit Provider</a>\n".
"<a href=\"/admin/list_providers.php?review=review\">Toggle / Edit Reviews</a>\n".
"<a href=\"/index.php\">Back To Website</a>\n".
"</div>\n".
"</td><td width=\"625\">\n".
"<table id=\"main_admin\" align=\"center\" border=\"0\">\n".
"<tr><td align=\"center\">\n".
"The selected provider has now been updated.\n".
"</td></tr>\n".
"</table>\n".
"</td></tr>\n".
"</table>\n".
"</body>\n".
"</html>";

} else {
    $SQL = mysql_query("INSERT INTO tblAdspace
(strProviderservice, strCompanyname, strOwner, strAddress, strTown, strZipcode, strPhone, str2ndphone, strMobile, strPager, strFax, strEmail, strWebsite, strLicense, strBonded, strHours, str24houremerg, strServicesoffered, strOtherservices, strServicearea, strFreeestimate, strWorkguaranteed, strProvidertagline, strAd_size, strImage)
VALUES
('$strProviderservice', '$strCompanyname', '$strOwner', '$strAddress', '$strTown', '$strZipcode', '$strPhone', '$str2ndphone', '$strMobile', '$strPager', '$strFax, $strEmail', '$strWebsite', '$strLicense', '$strBonded', '$strHours', '$str24houremerg', '$strServicesoffered', '$strOtherservices', '$strServicearea', '$strFreeestimate', '$strWorkguaranteed', '$strProvidertagline', '$strAd_size', 'strImage')")
or die ("Could not insert values into DB tblAdspace: " . mysql_error());

echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n".
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n".
"<head>\n".
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" />\n".
"<title>Ask2bsure.com - (Admin Section) Edit Provider</title>\n".
"<link style=\"text/css\" href=\"/css/style.css\" rel=\"stylesheet\">\n".
"</head>\n".
"<body>\n".
"<table align=\"center\" id=\"admin\">\n".
"<tr><td align=\"center\" colspan=\"2\">\n".
"<img src=\"/images/sublogo_2.png\"><br />\n".
"<span style=\"font-size: 12px; color: #0000FF\">(Admin Section)</span>\n".
"</td></tr>\n".
"<tr><td valign=\"top\">\n".
"<div id=\"navlist\">\n".
"<a href=\"/admin/\">Home</a>\n".
"<a href=\"/admin/add_provider.php\">Add Provider</a>\n".
"<a href=\"/admin/list_providers.php?list=list\">Delete / Edit Provider</a>\n".
"<a href=\"/admin/list_providers.php?review=review\">Toggle / Edit Reviews</a>\n".
"<a href=\"/index.php\">Back To Website</a>\n".
"</div>\n".
"</td><td width=\"625\">\n".
"<table id=\"main_admin\" align=\"center\" border=\"0\">\n".
"<tr><td align=\"center\">\n".
"The selected provider has now been updated.\n".
"</td></tr>\n".
"</table>\n".
"</td></tr>\n".
"</table>\n".
"</body>\n".
"</html>";

}
}

} elseif ($_GET['image'] == "image") {

if ($_SERVER['REQUEST_METHOD'] == "POST") {

$conn = mysql_connect("127.0.0.1", "", "");
mysql_select_db("providers", $conn)
or die(mysql_error());

$target_path = "/Library/WebServer/Documents/images/";
$target_path = basename( $_FILES['strImage']['name']);
$filename = basename($_FILES['strImage']['name']);
$_FILES['strImage']['$filename'];
move_uploaded_file($_FILES['strImage']['$filename'], $target_path);

$SQL = mysql_query("SELECT strImage FROM tblAdspace where strCompanyname = '" .$strCompanyname. "'")
or die(mysql_error());

if ($row = mysql_fetch_array($SQL)) {

$SQL = mysql_query("UPDATE tblAdspace SET strImage='" . $target_path . "' WHERE strCompanyname = '" . $strCompanyname . "'");

echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n".
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n".
"<head>\n".
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" />\n".
"<title>Ask2bsure.com - (Admin Section) Edit Provider</title>\n".
"<link style=\"text/css\" href=\"/css/style.css\" rel=\"stylesheet\">\n".
"</head>\n".
"<body>\n".
"<table align=\"center\" id=\"admin\">\n".
"<tr><td align=\"center\" colspan=\"2\">\n".
"<img src=\"/images/sublogo_2.png\"><br />\n".
"<span style=\"font-size: 12px; color: #0000FF\">(Admin Section)</span>\n".
"</td></tr>\n".
"<tr><td valign=\"top\">\n".
"<div id=\"navlist\">\n".
"<a href=\"/admin/\">Home</a>\n".
"<a href=\"/admin/add_provider.php\">Add Provider</a>\n".
"<a href=\"/admin/list_providers.php?list=list\">Delete / Edit Provider</a>\n".
"<a href=\"/admin/list_providers.php?review=review\">Toggle / Edit Reviews</a>\n".
"<a href=\"/index.php\">Back To Website</a>\n".
"</div>\n".
"</td><td width=\"625\">\n".
"<table id=\"main_admin\" align=\"center\" border=\"0\">\n".
"<tr><td align=\"center\">\n".
"The selected provider has now been updated.\n".
"</td></tr>\n".
"</table>\n".
"</td></tr>\n".
"</table>\n".
"</body>\n".
"</html>";
}
}
}
?>      
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 18788473
Ok, I added the insert query as well, but now when I try to insert the image only the variable gets uploaded instead of the name of the image.

} elseif ($_GET['image'] == "image") {

if ($_SERVER['REQUEST_METHOD'] == "POST") {

$conn = mysql_connect("127.0.0.1", "", "");
mysql_select_db("providers", $conn)
or die(mysql_error());

$target_path = "/Library/WebServer/Documents/images/";
$target_path = basename( $_FILES['strImage']['name']);
$filename = basename($_FILES['strImage']['name']);
$_FILES['strImage']['$filename'];
move_uploaded_file($_FILES['strImage']['$filename'], $target_path);

$SQL = mysql_query("SELECT strImage FROM tblAdspace where strCompanyname = '" .$strCompanyname. "'")
or die(mysql_error());

if ($row = mysql_fetch_array($SQL)) {

$SQL = mysql_query("UPDATE tblAdspace SET strImage='" . $target_path . "' WHERE strCompanyname = '" . $strCompanyname . "'");

echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n".
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n".
"<head>\n".
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" />\n".
"<title>Ask2bsure.com - (Admin Section) Edit Provider</title>\n".
"<link style=\"text/css\" href=\"/css/style.css\" rel=\"stylesheet\">\n".
"</head>\n".
"<body>\n".
"<table align=\"center\" id=\"admin\">\n".
"<tr><td align=\"center\" colspan=\"2\">\n".
"<img src=\"/images/sublogo_2.png\"><br />\n".
"<span style=\"font-size: 12px; color: #0000FF\">(Admin Section)</span>\n".
"</td></tr>\n".
"<tr><td valign=\"top\">\n".
"<div id=\"navlist\">\n".
"<a href=\"/admin/\">Home</a>\n".
"<a href=\"/admin/add_provider.php\">Add Provider</a>\n".
"<a href=\"/admin/list_providers.php?list=list\">Delete / Edit Provider</a>\n".
"<a href=\"/admin/list_providers.php?review=review\">Toggle / Edit Reviews</a>\n".
"<a href=\"/index.php\">Back To Website</a>\n".
"</div>\n".
"</td><td width=\"625\">\n".
"<table id=\"main_admin\" align=\"center\" border=\"0\">\n".
"<tr><td align=\"center\">\n".
"The selected provider has now been updated.\n".
"</td></tr>\n".
"</table>\n".
"</td></tr>\n".
"</table>\n".
"</body>\n".
"</html>";

} else {

$SQL = mysql_query("INSERT INTO tblAdspace (strImage) VALUES ('strImage')")
or die (mysql_error());

echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n".
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n".
"<head>\n".
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" />\n".
"<title>Ask2bsure.com - (Admin Section) Edit Provider</title>\n".
"<link style=\"text/css\" href=\"/css/style.css\" rel=\"stylesheet\">\n".
"</head>\n".
"<body>\n".
"<table align=\"center\" id=\"admin\">\n".
"<tr><td align=\"center\" colspan=\"2\">\n".
"<img src=\"/images/sublogo_2.png\"><br />\n".
"<span style=\"font-size: 12px; color: #0000FF\">(Admin Section)</span>\n".
"</td></tr>\n".
"<tr><td valign=\"top\">\n".
"<div id=\"navlist\">\n".
"<a href=\"/admin/\">Home</a>\n".
"<a href=\"/admin/add_provider.php\">Add Provider</a>\n".
"<a href=\"/admin/list_providers.php?list=list\">Delete / Edit Provider</a>\n".
"<a href=\"/admin/list_providers.php?review=review\">Toggle / Edit Reviews</a>\n".
"<a href=\"/index.php\">Back To Website</a>\n".
"</div>\n".
"</td><td width=\"625\">\n".
"<table id=\"main_admin\" align=\"center\" border=\"0\">\n".
"<tr><td align=\"center\">\n".
"The selected provider has now been updated.\n".
"</td></tr>\n".
"</table>\n".
"</td></tr>\n".
"</table>\n".
"</body>\n".
"</html>";

}
}
}
?>      
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 18788501
Right now I have the image's name being inserted finally, but it is wanting to insert on a separate row other than on the row of the provider.
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 18788554
Any ideas?
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 18788809
Alright, I figured it out.  You guys probably knew I would huh?
0
 
LVL 28

Expert Comment

by:gamebits
ID: 18789081
Thanks for the points and the grade, sorry if I couldn't follow up, familly obligations, glad you figure it out though.
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

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 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

18 Experts available now in Live!

Get 1:1 Help Now