Solved

Ignoring an emtpy field

Posted on 2007-03-25
17
250 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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
 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert html page to a php post email form automatically? 10 53
showing loader for php/mysql/ajax live search 13 23
mysql date time 14 28
Cpanel file manager 8 22
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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.

786 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