UPDATE data from db to user to db through an input

2019-08-31 07:50发布

问题:

UPDATE data from db to user to db through an input field. Can anyone please tell me whats wrong with this UPDATE. I'm getting the data from a field where I'm also writing from db, is that not possible?

My sql code looks like this..

<?php session_start(); $brugerID = $_SESSION['brugerID']; $holdID = $_SESSION['holdID']; ?>

My php code looks like this:

<form method="POST" action="" id="submitvaegt">
    <tr>
        <td colspan="5"><h2>Hvor meget vejer du idag?</h2></td>
    </tr>
        <?php
        $brugerensID = $_SESSION['brugerID'];
        $sql = "SELECT * FROM `brugere` 
        WHERE brugerID = '$brugerensID'";
        $result = mysql_query($sql);
        $row = mysql_fetch_assoc($result);

        echo '<tr>';
        echo '<td rowspan="3" valign="middle" width="128">';
        echo '<img src="http://www.mahogni.com/afs/wp-content/uploads/2012/12/weight.png" width="60"></td>';
        echo '<td>&nbsp;</td>';
        echo '<td colspan="3">&nbsp;</td>';
        echo '</tr>';

        echo '<tr>';
        echo '<td width="245" height="35">Jeg vejer i dag</td>'; //vaegtnu
        echo '<td width="280">';
        echo '<input class="textboxReport" type="text" name="vaegtnu" size="3" value="';
        if ($row['vaegtnu']==0) 
        { 
            echo $row['vaegtstart'];
        } 
        else 
        {
            echo $row['vaegtnu'];
        }           
        echo '"/>&nbsp;kg&nbsp;&nbsp;';
        echo '<input type="submit" name="submitvaegt" id="submitvaegt" value="GEM" />';
        echo '<td width="280" height="35">Jeg har til nu tabt</td>'; //vaegtstart - vaegtnu
        echo '<td align="right">';
        if ($row['vaegtnu']==0) 
        { 
            echo $row['vaegtstart']-$row['vaegtstart'];
        } 
        else 
        {
            echo $row['vaegtstart']-$row['vaegtnu'];
        }           
        echo '&nbsp;kg</td>';               
        echo ' </tr>';

        echo '<tr>';
        echo '<td height="35">Mit mål er</td>'; //vaegtslut
        echo '<td>'. $row['vaegtslut'] . '&nbsp;kg</td>'; 
        echo '<td height="28">Jeg mangler at tabe</td>'; //vaegtslut - vaegtnu ELLER vaegtslut - vaegtstart
        echo '<td>';

        if ($row['vaegtnu']==0) 
        { 
            echo $row['vaegtslut']-$row['vaegtstart'] . '&nbsp;kg';
        } 
        else 
        {
            echo $row['vaegtslut']-$row['vaegtnu'] . '&nbsp;kg';
        }
        echo '</td>';
        echo '</tr>';
        ?>
</form>

回答1:

Can you try this:

$vaegtnu=$_POST['vaegtnu'];

$brugerensID= $_SESSION['brugerID'];

mysql_query("UPDATE `brugere` SET vaegtnu='$vaegtnu' WHERE brugerID='$brugerensID'");

And tell me if it helps.

Also check that your variables match, i.e. in XXXX = $_POST['YYYY'] YYYY should be the name of the input, and XXXX should be the name of the variable you're using in the query.



回答2:

Not sure, but maybe try removing the single quotes around the $brugerensID if your value is an integer:

mysql_query("UPDATE brugere SET vaegtnu='$vaegtnu' WHERE brugerID=$brugerensID");

This depends on how your schema is set up. Are you getting any PHP warnings or error messages?