469,609 Members | 1,169 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

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

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--
Feb 4 '06 #1
1 2371

Dominik Szczurek wrote:
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--


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).

Feb 4 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by PinkBishop | last post: by
3 posts views Thread by Matias Silva | last post: by
16 posts views Thread by David Lauberts | last post: by
5 posts views Thread by deaconj999 | last post: by
4 posts views Thread by Cigar2011 | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.