Php 正在更新mySQL DB,但POST未返回值

Php 正在更新mySQL DB,但POST未返回值,php,mysql,post,Php,Mysql,Post,我试图更新单个记录中的两个字段。但是,当我单击“更新”按钮并提交更改时,不会将任何内容发布到assigned\u to=$\u POST['assigned']、completed='\u POST['completedTime']或id='$\u POST['ticket']。我想当我点击“更新”时,它会看到每个名为“分配”/“完成时间”/“票证”的字段。如何确保只选择一条记录 <?php require_once('../con.php'); ?> <?php

我试图更新单个记录中的两个字段。但是,当我单击“更新”按钮并提交更改时,不会将任何内容发布到
assigned\u to=$\u POST['assigned']
completed='\u POST['completedTime']
id='$\u POST['ticket']
。我想当我点击“更新”时,它会看到每个名为“分配”/“完成时间”/“票证”的字段。如何确保只选择一条记录

<?php require_once('../con.php'); ?>

<?php

    //query DB
    $query = "SELECT * FROM table";

    $results = mysql_query($query, $con) or die (mysql_error() . mysql_error($con));
    $row = mysql_fetch_assoc($results);
    $totalRows = mysql_num_rows($results);


    //Update db
    if (isset($_POST['update']))
    {       
        $update = "UPDATE table SET assigned_to=$_POST['assigned'], completed='$_POST['completedTime'] WHERE id='$_POST['ticket']'";
        mysql_query($update, $con);

        if (!mysql_query($update, $con)) 
        {
            die('Error: ' . mysql_error($con));
        }
    }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
body,td,th {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
}
h1,h2,h3,h4,h5,h6 {
    font-family: Arial, Helvetica, sans-serif;
}
h1 {
    font-size: 18px;
}
h2 {
    font-size: 14px;
}
</style>
</head>

<body>
<h1>Report</h1>


<form action="<?php echo $_SERVER['PHP_SELF']; ?>"  method="POST" accept-charset="iso-8859-1">

<hr size="1" noshade="noshade" />

<table border="1" cellpadding="3" cellspacing="0" class="small">
  <tr>
    <td><strong>TicketNum</strong></td>
    <td><strong>User</strong></td>
    <td><strong>Category</strong></td>
    <td><strong>Description</strong></td>
    <th>Comments</th>
    <td><strong>Date</strong></td>
    <td><strong>AssignedTo</strong></td>
    <td><strong>CompletedDate</strong></td>
  </tr>
  <?php do { ?>
    <tr bgcolor="<?php 
    $colorarray=array('#FFFFFF','#D9ECFF');
    $colorarraysize=count($colorarray);
        if ($BRB_rowcounter++%1 == 0){ 
        if ($colorarrayindex<($colorarraysize-1)){
            $colorarrayindex++;
        }else{
            $colorarrayindex=0;
        }
    } 
    echo "$colorarray[$colorarrayindex]"; ?>">
        <td valign="top" nowrap="nowrap"><?php echo $row['id']; ?></td>
        <td valign="top" nowrap="nowrap"><?php echo $row['username']; ?></td>
        <td valign="top" nowrap="nowrap"><?php echo $row['category']; ?></td>
        <td valign="top" nowrap="nowrap"><?php echo $row['title']; ?></td>
        <td valign="top"><?php echo $row['issue_details']; ?></td>
        <td valign="top" nowrap="nowrap"><?php echo $row['timestamp']; ?></td>
        <td valign="top" nowrap="nowrap"><input type="text" name="assigned" value ='<?php echo $row['assigned_to']; ?>'></td>
        <td valign="top" nowrap="nowrap"><input type="text" name="completedTime" value ='<?php echo $row['completed']; ?>'></td>
        <td><input type="hidden" name="ticket" value ='<?php echo $row['id']; ?>'></td>
        <td><input type="submit" name="update" value="Update"></td>
    </tr>
    <?php } while ($row = mysql_fetch_assoc($results)); ?>

</table>

</form>

</body>
</html>
<?php

mysql_free_result($update);

mysql_free_result($results);
?>

正文,td,th{
字体系列:Arial、Helvetica、无衬线字体;
字体大小:12px;
}
h1、h2、h3、h4、h5、h6{
字体系列:Arial、Helvetica、无衬线字体;
}
h1{
字号:18px;
}
氢{
字体大小:14px;
}
报告

您的字符串插值语法不正确。应该是:

$update = "UPDATE table SET assigned_to='{$_POST['assigned']}', completed='{$_POST['completedTime']}' WHERE id='{$_POST['ticket']}'";
如果索引包含引号,则需要在数组引用周围放置
{}
。您也可以省略引号:

$update = "UPDATE table SET assigned_to='$_POST[assigned]', completed='$_POST[completedTime] WHERE id='$_POST[ticket]'";
你也有一些缺少的引号和美元符号,我已经在这里修复了

有关字符串语法的详细信息,请参见

然而,这是一种非常糟糕的风格,因为它受制于SQL注入。您最好使用支持准备好的查询的API(PDO、mysqli);如果没有,请将
$\u POST
参数分配给变量,并调用
mysql\u real\u escape\u string()
对其进行清理。

尝试:

<?php
        require_once('../con.php');

            //Update db
            if (isset($_POST['update']))
            {       
                $update = "UPDATE table SET assigned_to='{$_POST['assigned']}', completed='{$_POST['completedTime']}' WHERE id='{$_POST['ticket']}'";
                $result = mysql_query($update, $con) or die ('Error' . mysql_error($con));
        }

        //query DB
            $query     = "SELECT * FROM table";
            $results   = mysql_query($query, $con) or die (mysql_error() . mysql_error($con));
            $totalRows = mysql_num_rows($results);

    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <style type="text/css">
    body,td,th {
        font-family: Arial, Helvetica, sans-serif;
        font-size: 12px;
    }
    h1,h2,h3,h4,h5,h6 {
        font-family: Arial, Helvetica, sans-serif;
    }
    h1 {
        font-size: 18px;
    }
    h2 {
        font-size: 14px;
    }
    </style>
    </head>

    <body>
    <h1>Report</h1>


    <hr size="1" noshade="noshade" />
    <table border="1" cellpadding="3" cellspacing="0" class="small">
      <tr>
        <td><strong>TicketNum</strong></td>
        <td><strong>User</strong></td>
        <td><strong>Category</strong></td>
        <td><strong>Description</strong></td>
        <th>Comments</th>
        <td><strong>Date</strong></td>
        <td><strong>AssignedTo</strong></td>
        <td><strong>CompletedDate</strong></td>
      </tr>

    <?php
        while ($row = mysql_fetch_assoc($results)) {

                $colorarray     = array("#FFFFFF","#D9ECFF");
                $colorarraysize = count($colorarray);

            if ($BRB_rowcounter++%1 == 0){ 
                    if ($colorarrayindex<($colorarraysize-1)){
                            $colorarrayindex++;
                    }else{
                            $colorarrayindex=0;
                    }
                } 

            $content .= <<< END
                <form action="" method="POST" accept-charset="iso-8859-1">
                <tr bgcolor="{$colorarray[$colorarrayindex]}">
                        <td valign="top" nowrap="nowrap">{$row['id']}</td>
                    <td valign="top" nowrap="nowrap">{$row['username']}</td>
                        <td valign="top" nowrap="nowrap">{$row['category']}</td>
                    <td valign="top" nowrap="nowrap">{$row['title']}</td>
                    <td valign="top">{$row['issue_details']}</td>
                    <td valign="top" nowrap="nowrap">{$row['timestamp']}</td>
                    <td valign="top" nowrap="nowrap"><input type="text" name="assigned" value="{$row['assigned_to']}"></td>
                    <td valign="top" nowrap="nowrap"><input type="text" name="completedTime" value="{$row['completed']}"></td> 
                    <td><input type="submit" name="update" value="Update"></td>
                    </tr>
                <input type="hidden" name="ticket" value="{$row['id']}">
                </form>
END;

            }
        echo $content;
        mysql_free_result($results);
      ?>

    </table>

正文,td,th{
字体系列:Arial、Helvetica、无衬线字体;
字体大小:12px;
}
h1、h2、h3、h4、h5、h6{
字体系列:Arial、Helvetica、无衬线字体;
}
h1{
字号:18px;
}
氢{
字体大小:14px;
}
报告

TicketNum 用户 类别 说明 评论 日期 分配给 完成日期
很抱歉,复制和粘贴错误。此行有很多错误
$update=“更新表集分配给=$发布['assigned'],completed=''发布['completedTime'],其中id='$发布['ticket']”---缺少
$
completed=''u POST['completedTime']
只需使用
mysqli即可。您易受攻击,并且存在PHP语法错误(无法在
-引用字符串。例如?我只是在学习PHP,mySQLI并不完全担心SQL注入这只是一两个人的内部使用。我添加了{}”但是我仍然没有得到数据库的任何更新。分配给
类型是什么?如果是字符串,则需要在值周围加引号。它是varchar。没有错误。单击“更新”后它只是清除了文本框。听起来你好像遇到了PHP语法错误。检查你的错误日志。如果没有错误,它必须打印一些东西。即使更新没有做任何事情,你的页面也会打印表格标题和所有其他样板。浏览器控制台中的“网络”选项卡说什么,它是否显示成功的
200
代码或错误?单击“更新”后,此操作与我以前的代码相同。文本框被清除,数据库中没有更新值。仅供参考,$content后面应该没有空格。=我收到一个内部服务器500错误,除非我注释掉表单:$content。=我的意思是$content。=有效!非常感谢。