Passing 0 to database but not an empy text input - php

I am passing user entered numerical values into a database using text inputs. I want the db to execute the insert for all numerical values greater than or equal to 0 however if nothing is in the textbox I do not want the database to execute the insert. So I would like to distinguish between 0 and no entry. isset would still return true. any help would be great
Thank You
I have my code written, I just need help with the logic test of determining whether zero or null.

You want to use isset in this case as it will detect if the textbox is null or not set however it will pass if it is 0
if (isset($_POST['textbox']))
You can further combine it with strlen like this:
if (isset($_POST['textbox']) && strlen($_POST['textbox']) > 0)

Related

MySQL Query - replace or remove 0 (zero) in a column, but not remove the digit zero in a word or number?

I have a table where one column has 0 for a value. The problem is that my page that fetches this data show's the 0.
I'd like to remove the 0 value, but only if it's a single 0. And not remove the 0 if it's in a word or a numeric number like 10, 1990, 2006, and etc.
I'd like to see if you guys can offer a SQL Query that would do that?
I was thinking of using the following query, but I think it will remove any 0 within a word or numeric data.
update phpbb_tree set member_born = replace(member_born, '0', '')
Hopefully you guys can suggest another method? Thanks in advance...
After discussed at the comments you have said that you want to not show 0 values when you fetching the data. The solution is simple and should be like this.
lets supposed that you have make your query and fetch the data with a $row variable.
if($row['born_year'] == '0'){
$born_year = "";
} else {
$born_year = $row['born_year'];
}
Another solution is by filtering the query from the begging
select * from table where born_year !='0';
update
if you want to remove all the 0 values from your tables you can do it in this way. Consider making a backup before.
update table set column='' where column='0';
if the value is int change column='0' to column=0

How do I insert NULL values for blank fields when using JQuery Serialize?

I have many forms that I am serializing. The data is posted to either an insert.php page or update.php.
If a form input is left blank, I want to insert a NULL value into my database. I'm using mySQL.
It seems like when a form is serialized, any fields that are left blank are sent as value of '' which does NOT insert as NULL into my db.
I can filter out empty inputs before serializing, but this isn't good when a record needs to be updated.
Here's a fiddle of a form that you can at least see that empty inputs are logged as "".
Any advice on how I can set those values to NULL instead?
jsfiddle.net/gabrieleromanato/bynaK/
You can simply at php side check if value is empty use instead null with strlen(...) == 0 condition ( as #PaulSpiegel mentioned in comment of this answer empty will consider that 0 is also empty and will write null to your db ) - http://php.net/manual/en/function.strlen.php
// Example
$value = !strlen($_POST['value']) ? null:$_POST['value'];
// .. save in MySQL

Why is this md5 function returning null sometimes?

I'm selecting a couple of values from a mysql database, combining and hashing them. Sometimes the result is null. Anyone know why?
First we fetch an associative array representing the row.
$results = dal::query("select foo, email from wtf where email = ?", $_GET["email"], "row");
Then we check for truthy results and hash.
if($results["foo"] && $results["email"]){
$whyisthisnullsometimes = md5($results["foo"] . $results["email"]);
$url = "https://example.com/dostuff.php?thing={$whyisthisnullsometimes}";
}
$whyisthisnullsometimes occasionally is just null. Not sure why.
The url is appended to an email message and sent. Users are hitting this link, which would not exist if ($results["foo"] && $results["email"]) evaluates to false.
wtf.foo is char 32 and wtf.email is varchar 250. collation is latin1_swedish_ci
That is quick answer:
when
if($results["foo"] && $results["email"]){
your condition is false you have $whyisthisnullsometimes not defined and php interprets it as null.
For further information you should post bigger fragment of your code :-)

0 Value instead of NULL in column MySQL

I have a script that geocodes physical addresses and puts it into a MySQL database with two columns: coord_lat and coord_long. These are float type columns but unfortunately some addresses don't get geocoded correctly and the script tries to push the address as a null value into the database which then breaks the script because the database cannot hold a null value.
I'm trying to find a way I can rewrite my script to determine if the geocoded address comes out to a null value to automatically rewrite that value to 0 so that the database and script don't break.
Can anyone give me some advice on switching/replacing a null values to 0?
try this:
if PHP (shorthand if)
(is_null($value) ? 0:$value)
else if MySQL (coalesce function)
SELECT COALESCE(#your_value_here,0) FROM dual
for reference of coalesce:
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html
for shorthand if statement (ternary):http://davidwalsh.name/php-ternary-examples
If the value is NULL, then assign 0
if(is_null($value)){
$value = 0;
}

PHP: validate decimal Mysqli data type

I have a table with the column data type like this:
price decimal(6,2) unsigned NOT NULL,
And my validation code is like this:
if ( (!empty($_POST['price'])) && (is_numeric($_POST['price'])) && (isset($_POST['price'])) > 0 ) {
$price = ($_POST['price']);
} else{
$price = FALSE;
echo '<p> Only accept number and must be higher than zero</p>';
}
I use the $_POST form for users to submit the value. But you know,
1/ When the user types any non-numeric value such as a,b,c etc, it also validates well.
2/ When the user types the value zero in, it validates well too.
However, the question is that when I tested it with no value typed in. I mean that I left the value empty and hit the 'submit' button, the error message still returned as per the }else { part does, but the value was still inserted into the table column with a value of 0.00 technically at the same time.
To my limited knowledge, I can guess that the problem was probably at the Mysqli data type of the table I chose, but i don't know how to correct it.
Can you help me, please?
I think the solution you're looking for is to simply move the inserting code to the first if statement. That way it'll only insert the value if it is numeric and not empty.
What you describe means that you've failed to stop the insert when $price===false
(i.e. the problem is not the evaluation; it has given you the correct message. You've some programming logic error elsewhere)

Categories