SocomNegotiator Posted November 5, 2008 Share Posted November 5, 2008 Ok I have a submit button that takes the values entered and enters them into a database...pretty simple. Well if the quantity is not entered I want nothing to be put into the database under that field. However, it is putting a "0" into the field. I even have this code to try and prevent that... if($amount = ""){ $amount = ""; } Also I have the structure of this field set to Null. I had it as Not Null before and the default was zero. I thought by changing that to Null would change it, but it did not. Any suggestions? Link to comment https://forums.phpfreaks.com/topic/131518-solved-simple-question-about-php/ Share on other sites More sharing options...
flyhoney Posted November 5, 2008 Share Posted November 5, 2008 If you use NULL in your query it should work. UPDATE table SET column = NULL WHERE id = 1 Link to comment https://forums.phpfreaks.com/topic/131518-solved-simple-question-about-php/#findComment-683025 Share on other sites More sharing options...
Mchl Posted November 5, 2008 Share Posted November 5, 2008 First of all, your code will not work. It will always insert empty value, because you're doing an assignment in if() construct instead of comparison. That's how it's supposed to be. if($amount == ""){ $amount = ""; } Second thing. How is the field in database defined. Is it INTEGER? Does it allow NULL values? Link to comment https://forums.phpfreaks.com/topic/131518-solved-simple-question-about-php/#findComment-683028 Share on other sites More sharing options...
rhodesa Posted November 5, 2008 Share Posted November 5, 2008 set it to NULL instead of an empty string: INSERT INTO tableName (field1,field2,amount) VALUES ('abc','def',NULL); or, just leave it out of the list: INSERT INTO tableName (field1,field2) VALUES ('abc','def'); Link to comment https://forums.phpfreaks.com/topic/131518-solved-simple-question-about-php/#findComment-683030 Share on other sites More sharing options...
Mchl Posted November 5, 2008 Share Posted November 5, 2008 That's assuming, that NULL is valid for this field. Link to comment https://forums.phpfreaks.com/topic/131518-solved-simple-question-about-php/#findComment-683032 Share on other sites More sharing options...
revraz Posted November 5, 2008 Share Posted November 5, 2008 IF statements require two == not one if($amount == ""){ Also, make sure you remove the Default 0 as well as setting it to NULL and it should work. Link to comment https://forums.phpfreaks.com/topic/131518-solved-simple-question-about-php/#findComment-683033 Share on other sites More sharing options...
rhodesa Posted November 5, 2008 Share Posted November 5, 2008 That's assuming, that NULL is valid for this field. Also I have the structure of this field set to Null Link to comment https://forums.phpfreaks.com/topic/131518-solved-simple-question-about-php/#findComment-683036 Share on other sites More sharing options...
Mchl Posted November 5, 2008 Share Posted November 5, 2008 That's assuming I read posts in their entirety. One more thing. Does the field have a default value? If it does, inserting NULL into it, will in fact insert default value. Link to comment https://forums.phpfreaks.com/topic/131518-solved-simple-question-about-php/#findComment-683037 Share on other sites More sharing options...
SocomNegotiator Posted November 5, 2008 Author Share Posted November 5, 2008 If you use NULL in your query it should work. UPDATE table SET column = NULL WHERE id = 1 Ok yes it is an Integer that is set to NULL. I tried $amount == "" and it did not work. Ok that is for an update...now if I have an INSERT like this $db->query("INSERT INTO guide_order (item_id, amount, user_id, priority) VALUES ( '$item_id', '$amount', '$user_id', '$num')") or die('Error, query failed'); Now you notice that I have $amount...could I change that to NULL? However, with that if the person enters in an amount I want it put the amount they entered into the database. So I can't use NULL here, because no matter if they left it blank or entered in something they would always get nothing. Link to comment https://forums.phpfreaks.com/topic/131518-solved-simple-question-about-php/#findComment-683039 Share on other sites More sharing options...
Mchl Posted November 5, 2008 Share Posted November 5, 2008 The simplest way. Note that I dropped '' around $amount in query if($amount == "") $amount = "NULL"; $query = "INSERT INTO guide_order (item_id, amount, user_id, priority) VALUES ( '$item_id', $amount, '$user_id', '$num')"; Link to comment https://forums.phpfreaks.com/topic/131518-solved-simple-question-about-php/#findComment-683044 Share on other sites More sharing options...
SocomNegotiator Posted November 5, 2008 Author Share Posted November 5, 2008 The simplest way. Note that I dropped '' around $amount in query if($amount == "") $amount = "NULL"; $query = "INSERT INTO guide_order (item_id, amount, user_id, priority) VALUES ( '$item_id', $amount, '$user_id', '$num')"; Sweet stuff that right there worked. Thanks a lot everyone for your help. Link to comment https://forums.phpfreaks.com/topic/131518-solved-simple-question-about-php/#findComment-683051 Share on other sites More sharing options...
rhodesa Posted November 5, 2008 Share Posted November 5, 2008 I would do this: Set the field to allow NULL and set the default value to NULL. Then use this: <?php $data = array( 'item_id' => $item_id, 'user_id' => $user_id, 'priority' => $num, ); if(!empty($amount)) $data['amount'] = $amount; $sql = "INSERT INTO `guide_order` (`".implode("`,`",array_keys($data))."`) VALUES ('".implode("','",$data)."')"; $db->query($sql) or die('Error, query failed'); ?> Link to comment https://forums.phpfreaks.com/topic/131518-solved-simple-question-about-php/#findComment-683052 Share on other sites More sharing options...
SocomNegotiator Posted November 5, 2008 Author Share Posted November 5, 2008 I would do this: Set the field to allow NULL and set the default value to NULL. Then use this: <?php $data = array( 'item_id' => $item_id, 'user_id' => $user_id, 'priority' => $num, ); if(!empty($amount)) $data['amount'] = $amount; $sql = "INSERT INTO `guide_order` (`".implode("`,`",array_keys($data))."`) VALUES ('".implode("','",$data)."')"; $db->query($sql) or die('Error, query failed'); ?> Yeah I need to read up on the whole implode thing. I use arrays, but I don't use them as often as I should. Link to comment https://forums.phpfreaks.com/topic/131518-solved-simple-question-about-php/#findComment-683056 Share on other sites More sharing options...
rhodesa Posted November 5, 2008 Share Posted November 5, 2008 I like that method cus it keeps the SQL flexible....2 columns or 20 columns, it's the same statement to make the SQL Link to comment https://forums.phpfreaks.com/topic/131518-solved-simple-question-about-php/#findComment-683061 Share on other sites More sharing options...
SocomNegotiator Posted November 5, 2008 Author Share Posted November 5, 2008 I like that method cus it keeps the SQL flexible....2 columns or 20 columns, it's the same statement to make the SQL Yeah I think I understand the query...except for one thing ('".implode("','",$data)."') Why are their two commas? I know one to separate each value, but the other I am not sure... Link to comment https://forums.phpfreaks.com/topic/131518-solved-simple-question-about-php/#findComment-683067 Share on other sites More sharing options...
Mchl Posted November 5, 2008 Share Posted November 5, 2008 The other to separate arguments of implode() function. It's like $separator = "','" $dataFields = "('".implode($separator,$data)."')"; Link to comment https://forums.phpfreaks.com/topic/131518-solved-simple-question-about-php/#findComment-683069 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.