简体   繁体   中英

php form mysql update saving data

Having difficulty with saving fields 'month' and 'year as they are not being stored session correctly. not sure if it because I have combined month and year to make sortcode. After submtting on form values get reset to default values of month,year 1 2016. How do I get it to save selected values to mysql table details?

I need to improve following function to get the month and year field to store correctly rather then storing default values.

 <?php 


 $sql = "SELECT * FROM payments WHERE user_id = '".$_SESSION['user']['id']."' LIMIT 1;";
    $result2 = mysql_query($sql);
    if($result2) {
        $row2 = mysql_fetch_assoc($result2);
    }
    $name = (isset($row2) && !empty($row2['name'])) ? $row2['name'] : (isset($_SESSION['name']) ? $_SESSION['name'] : '');
    $no = (isset($row2) && !empty($row2['no'])) ? $row2['no'] : (isset($_SESSION['no']) ? $_SESSION['no'] : '');
    $year = (isset($row2) && !empty($row2['sortcode'])) ? explode('/',$row2['sortcode'])[0] : (isset($_SESSION['year']) ? $_SESSION['year'] : '');
    $month = (isset($row2) && !empty($row2['sortcode'])) ? explode('/',$row2['sortcode'])[1] : (isset($_SESSION['month']) ? $_SESSION['month'] : '');

the error seems to be here if i remove $result2 to $result it works for the session but then after all data is deleted and reset to default value therefore not being saved to the database?

<option <?php if(isset($month) && !empty($month) && $month == '1') echo " selected "; ?> value="1">1</option>

have u tried space before and after selected like " selected " instead of "selected". Check allso the the source of the page after submitting (view source on IE)

I think the output will get like this:

<option selectedvalue="1">1</option>

instead of

<option selected value="1">1</option>

And is it necessary to 3x check on $month

if (isset($month) && $month=='1')

should be enough I think.

Edit->

Case $month and $year are udated correctly after post ->

$query = "INSERT INTO `details`(`user_id`, `name`, `no`, `sortcode`)
            VALUES (";
        $query .= "'" . $userID . "',";
        $query .= "'" . $_SESSION['name']  . "',";
        $query .= "'" . $_SESSION['no']    . "',";
        $query .= "'".$month."/".$year."'";
        $query .=
            ");";

Or use the values of the select inputs (after post)->

$query = "INSERT INTO `details`(`user_id`, `name`, `no`, `sortcode`)
                VALUES (";
            $query .= "'" . $userID . "',";
            $query .= "'" . $_SESSION['name']  . "',";
            $query .= "'" . $_SESSION['no']    . "',";
            $query .= "'".$_POST['month']."/".$_POST['year']."'";
            $query .=
                ");";

Or just update the $session month and year variables values.

It looks like you're setting the variable $year and $month based on query results or default session value, but when you make your insert you're using the $_SESSION variables and $month and $year are never used. try:

$query .= "'".$month."/".$year."'";
." `sortcode` = '".$month."/".$year."'";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM