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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dominik Szczurek

    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">Z asadowo¶æ typu p :</td>
    <td>
    <input maxlength="6" size="6" name="zasadowos c_typu_p" tabindex="3">
    </td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    </tr>
    --html--

    --php--
    if(!empty($_POS T['zasadowosc_typ u_p'])) $zasadowosc_typ u_p_b =
    (double)$_POST['zasadowosc_typ u_p'];
    else
    |
    $zasadowosc_typ u_p_b = -1; (I even try to change
    temporary type of this variable)
    if(!empty($_POS T['twardosc_ogoln a_w'])) $twardosc_ogoln a_w_b =
    $_POST['twardosc_ogoln a_w'];
    else
    $twardosc_ogoln a_w_b = -1;
    ..
    ..
    ..
    $SQL = "INSERT INTO pomiary (obiekt, nazwa_pomiaru, wartosc, id_datap)";
    $SQL .= " (SELECT '{$_SESSION['obiekt_b']}', 'zasadowosc_typ u_p',
    $zasadowosc_typ u_p_b, datap.id";
    $SQL .= " FROM datap";
    $SQL .= " WHERE datap.id = (SELECT max(datap.id) from datap))";
    $ResultSQL = mysql_query($SQ L);
    $SQL = "INSERT INTO pomiary (obiekt, nazwa_pomiaru, wartosc, id_datap)";
    $SQL .= " (SELECT '{$_SESSION['obiekt_b']}', 'twardosc_ogoln a_w',
    $twardosc_ogoln a_w_b, datap.id";
    $SQL .= " FROM datap";
    $SQL .= " WHERE datap.id = (SELECT max(datap.id) from datap))";
    $ResultSQL = mysql_query($SQ L);
    --php--


  • ZeldorBlat

    #2
    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">Z asadowo¶æ typu p :</td>
    > <td>
    > <input maxlength="6" size="6" name="zasadowos c_typu_p" tabindex="3">
    > </td>
    > <td></td>
    > <td></td>
    > <td></td>
    > <td></td>
    > <td></td>
    > </tr>
    > --html--
    >
    > --php--
    > if(!empty($_POS T['zasadowosc_typ u_p'])) $zasadowosc_typ u_p_b =
    > (double)$_POST['zasadowosc_typ u_p'];
    > else
    > |
    > $zasadowosc_typ u_p_b = -1; (I even try to change
    > temporary type of this variable)
    > if(!empty($_POS T['twardosc_ogoln a_w'])) $twardosc_ogoln a_w_b =
    > $_POST['twardosc_ogoln a_w'];
    > else
    > $twardosc_ogoln a_w_b = -1;
    > .
    > .
    > .
    > $SQL = "INSERT INTO pomiary (obiekt, nazwa_pomiaru, wartosc, id_datap)";
    > $SQL .= " (SELECT '{$_SESSION['obiekt_b']}', 'zasadowosc_typ u_p',
    > $zasadowosc_typ u_p_b, datap.id";
    > $SQL .= " FROM datap";
    > $SQL .= " WHERE datap.id = (SELECT max(datap.id) from datap))";
    > $ResultSQL = mysql_query($SQ L);
    > $SQL = "INSERT INTO pomiary (obiekt, nazwa_pomiaru, wartosc, id_datap)";
    > $SQL .= " (SELECT '{$_SESSION['obiekt_b']}', 'twardosc_ogoln a_w',
    > $twardosc_ogoln a_w_b, datap.id";
    > $SQL .= " FROM datap";
    > $SQL .= " WHERE datap.id = (SELECT max(datap.id) from datap))";
    > $ResultSQL = mysql_query($SQ L);
    > --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).

    Comment

    Working...