Connecting Tech Pros Worldwide Forums | Help | Site Map

php lost variable 0 from the form when i try to save it into mysql database - please help

Dominik Szczurek
Guest
 
Posts: n/a
#1: Feb 4 '06
Hello to anyone who is tring to help me :-)

As I was writed in the topic I have problem when somone input 0 value in the
form.
PHP is losting this value when it try to write it to mySQL.
I have php version 4.3.11 mysql 4.1.14 and apache 2.0.53

Please help because I don't know what is wrong.

The column type in database is float. When I try to add some record thru the
phpmyadmin for exapmle, everything is all right.
It is also right when the value is 0.0 but mysql save it as 0

Below is code from php (first the form in html, second sql statement in php)

--html--
<tr class=wiersz_2 valign="center" align="center" >
<td align="right">Zasadowość typu p :</td>
<td>
<input maxlength="6" size="6" name="zasadowosc_typu_p" tabindex="3">
</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
--html--

--php--
if(!empty($_POST['zasadowosc_typu_p'])) $zasadowosc_typu_p_b =
(double)$_POST['zasadowosc_typu_p'];
else
|
$zasadowosc_typu_p_b = -1; (I even try to change
temporary type of this variable)
if(!empty($_POST['twardosc_ogolna_w'])) $twardosc_ogolna_w_b =
$_POST['twardosc_ogolna_w'];
else
$twardosc_ogolna_w_b = -1;
..
..
..
$SQL = "INSERT INTO pomiary (obiekt, nazwa_pomiaru, wartosc, id_datap)";
$SQL .= " (SELECT '{$_SESSION['obiekt_b']}', 'zasadowosc_typu_p',
$zasadowosc_typu_p_b, datap.id";
$SQL .= " FROM datap";
$SQL .= " WHERE datap.id = (SELECT max(datap.id) from datap))";
$ResultSQL = mysql_query($SQL);
$SQL = "INSERT INTO pomiary (obiekt, nazwa_pomiaru, wartosc, id_datap)";
$SQL .= " (SELECT '{$_SESSION['obiekt_b']}', 'twardosc_ogolna_w',
$twardosc_ogolna_w_b, datap.id";
$SQL .= " FROM datap";
$SQL .= " WHERE datap.id = (SELECT max(datap.id) from datap))";
$ResultSQL = mysql_query($SQL);
--php--



ZeldorBlat
Guest
 
Posts: n/a
#2: Feb 4 '06

re: php lost variable 0 from the form when i try to save it into mysql database - please help



Dominik Szczurek wrote:[color=blue]
> Hello to anyone who is tring to help me :-)
>
> As I was writed in the topic I have problem when somone input 0 value in the
> form.
> PHP is losting this value when it try to write it to mySQL.
> I have php version 4.3.11 mysql 4.1.14 and apache 2.0.53
>
> Please help because I don't know what is wrong.
>
> The column type in database is float. When I try to add some record thru the
> phpmyadmin for exapmle, everything is all right.
> It is also right when the value is 0.0 but mysql save it as 0
>
> Below is code from php (first the form in html, second sql statement in php)
>
> --html--
> <tr class=wiersz_2 valign="center" align="center" >
> <td align="right">Zasadowość typu p :</td>
> <td>
> <input maxlength="6" size="6" name="zasadowosc_typu_p" tabindex="3">
> </td>
> <td></td>
> <td></td>
> <td></td>
> <td></td>
> <td></td>
> </tr>
> --html--
>
> --php--
> if(!empty($_POST['zasadowosc_typu_p'])) $zasadowosc_typu_p_b =
> (double)$_POST['zasadowosc_typu_p'];
> else
> |
> $zasadowosc_typu_p_b = -1; (I even try to change
> temporary type of this variable)
> if(!empty($_POST['twardosc_ogolna_w'])) $twardosc_ogolna_w_b =
> $_POST['twardosc_ogolna_w'];
> else
> $twardosc_ogolna_w_b = -1;
> .
> .
> .
> $SQL = "INSERT INTO pomiary (obiekt, nazwa_pomiaru, wartosc, id_datap)";
> $SQL .= " (SELECT '{$_SESSION['obiekt_b']}', 'zasadowosc_typu_p',
> $zasadowosc_typu_p_b, datap.id";
> $SQL .= " FROM datap";
> $SQL .= " WHERE datap.id = (SELECT max(datap.id) from datap))";
> $ResultSQL = mysql_query($SQL);
> $SQL = "INSERT INTO pomiary (obiekt, nazwa_pomiaru, wartosc, id_datap)";
> $SQL .= " (SELECT '{$_SESSION['obiekt_b']}', 'twardosc_ogolna_w',
> $twardosc_ogolna_w_b, datap.id";
> $SQL .= " FROM datap";
> $SQL .= " WHERE datap.id = (SELECT max(datap.id) from datap))";
> $ResultSQL = mysql_query($SQL);
> --php--[/color]

empty() doesn't behave exactly as you'd expect. You can read up on it
here:

<http://www.php.net/empty>

The short of the matter is that 1) all input coming from a form (i.e.
through $_POST) will be a string; 2) empty() returns true for false, 0,
"0" (the string 0), "" (the empty string), and null. So, if the user
inputs 0 into the form, your !empty() test fails and you get -1.

An easy fix that will give you the desired behavior (-1 if they put in
nothing or something other than a number) would be to switch your
!empty() calls to is_numeric(). This will return true for any numeric
string ("0", "0.0", "42.2", etc.) and false for anything else
(including the empty string).

Closed Thread