mac007 Posted March 16, 2009 Share Posted March 16, 2009 Hello, all: Hope somebody can help me, I have a form, where there is a "Quantity" field, and I want to make it so that IF the field is left blank, it will automatically insert "1" as a default value. I tried several ways but couldnt make it work. Thing is I am trying to implement it into a Dreamweaver-created script, so it creates this sophisticated mumbo-jumbo that I can't seem to crack... This is the INSERT mysql script I am working with: <CODE> $insertSQL = sprintf("INSERT INTO works (Type, Subject, `Description`, Description2, Quantity, Keywords, `Size`, Price, Shipping, pict, bigpict, pict2, bigpict2) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", GetSQLValueString($_POST['Type'], "text"), GetSQLValueString($_POST['Subject'], "text"), GetSQLValueString($_POST['Title'], "text"), GetSQLValueString($_POST['Description2'], "text"), GetSQLValueString($_POST['Quantity'], "int"), GetSQLValueString($_POST['Keywords'], "text"), GetSQLValueString($_POST['Size'], "text"), GetSQLValueString($_POST['Price'], "double"), GetSQLValueString($_POST['Shipping'], "text"), GetSQLValueString($save, "text"), GetSQLValueString($file, "text"), GetSQLValueString($save2, "text"), GetSQLValueString($file2, "text")); </CODE> Thanks in advance!! Link to comment https://forums.phpfreaks.com/topic/149603-how-can-i-have-it-so-quantity-field-inserts-a-1-as-default-if-field-left-blank/ Share on other sites More sharing options...
trq Posted March 16, 2009 Share Posted March 16, 2009 Change... GetSQLValueString($_POST['Quantity'], "int"), to.... GetSQLValueString($_POST['Quantity'], "int") ? GetSQLValueString($_POST['Quantity'], "int") : '1', Id drop dreamweaver. It writes terrible code. Link to comment https://forums.phpfreaks.com/topic/149603-how-can-i-have-it-so-quantity-field-inserts-a-1-as-default-if-field-left-blank/#findComment-785579 Share on other sites More sharing options...
mac007 Posted March 16, 2009 Author Share Posted March 16, 2009 Thanks Thorpe... I know, the Dreamweaver code is soo bad... and so wordy I have been told; but I am still worse myself without it... so until I get a better at PHP, I still need to rely on it.. You know, I did replace the line there as you say, but it still didnt do it... it left the mysql field empty still.. Any ideas what could be wrong? Thanks again! Link to comment https://forums.phpfreaks.com/topic/149603-how-can-i-have-it-so-quantity-field-inserts-a-1-as-default-if-field-left-blank/#findComment-785591 Share on other sites More sharing options...
trq Posted March 16, 2009 Share Posted March 16, 2009 Your going to need to validate the value before it gets to your query. eg; Something like.... if (empty($_POST['Quantity'])) { $_POST['Quantity'] = 1; } Of course you could always setup your database field to use 1 as a default. Link to comment https://forums.phpfreaks.com/topic/149603-how-can-i-have-it-so-quantity-field-inserts-a-1-as-default-if-field-left-blank/#findComment-785594 Share on other sites More sharing options...
mac007 Posted March 16, 2009 Author Share Posted March 16, 2009 thanks Thorpe... OK, I had done that earlier.. but didnt work, but I didnt have your fix on the INSERT, let me try now... Also, I did try to make the database field to to be 1 as a default, but for some reason it woulndt take it either! that was the first thing I had tried, and since that didnt work, now I am trying to modify the script.. I'll try the database setting again.. maybe I didnt click right thing Link to comment https://forums.phpfreaks.com/topic/149603-how-can-i-have-it-so-quantity-field-inserts-a-1-as-default-if-field-left-blank/#findComment-785597 Share on other sites More sharing options...
mac007 Posted March 16, 2009 Author Share Posted March 16, 2009 OK, got it! I simply added your: if (empty($_POST['Quantity'])) { $_POST['Quantity'] = 1; } and it worked right off, even with the original Dreamweaver code: GetSQLValueString($_POST['Quantity'], "int"), I know what I was doing wrong, before I was writing the if statement with == instead of just one =, like this: if (empty($_POST['Quantity'])) { $_POST['Quantity'] == 1; } which was giving me the problem... but shouldnt that have worked either way??? Thanks Thorpe! Link to comment https://forums.phpfreaks.com/topic/149603-how-can-i-have-it-so-quantity-field-inserts-a-1-as-default-if-field-left-blank/#findComment-785602 Share on other sites More sharing options...
trq Posted March 16, 2009 Share Posted March 16, 2009 but shouldnt that have worked either way??? No. == is a comparison operator while = is assignment. Two completely different things. Link to comment https://forums.phpfreaks.com/topic/149603-how-can-i-have-it-so-quantity-field-inserts-a-1-as-default-if-field-left-blank/#findComment-785612 Share on other sites More sharing options...
mac007 Posted March 16, 2009 Author Share Posted March 16, 2009 ahh... that's right... everynow and then I lose it... Thanks! Link to comment https://forums.phpfreaks.com/topic/149603-how-can-i-have-it-so-quantity-field-inserts-a-1-as-default-if-field-left-blank/#findComment-785613 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.