简体   繁体   English

PHP的形式MySQL更新保存数据

[英]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? 填写表单值后,将其重置为month,year 1 year 2016的默认值。如何将其保存到mysql表详细信息中?

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? 如果我将$ result2删除为$ result,则该错误似乎在这里,它适用于会话,但是在删除所有数据并将其重置为默认值之后,因此不会保存到数据库中吗?

<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". 您是否在选择之前和之后尝试过像“ selected”而不是“ selected”这样的空间。 Check allso the the source of the page after submitting (view source on IE) 提交后检查所有页面的源(在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 并且是否有必要对$ month进行3次检查

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

should be enough I think. 我认为应该足够了。

Edit-> 编辑->

Case $month and $year are udated correctly after post -> 案例$ month和$ year在发布后正确地uded->

$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. 或者只是更新$ session月和年变量值。

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. 看起来您正在根据查询结果或默认会话值设置变量$ year和$ month,但是在插入时使用的是$ _SESSION变量,而从未使用$ month和$ year。 try: 尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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