Jump to content

how can I have it so "Quantity" field inserts a 1 as default if field left blank


mac007

Recommended Posts

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

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!

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.

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

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.