0

当我回显输出时,将显示所需的更新值。但是当传递相同的值来更新数据库时,更新语句什么也不做。

代码部分是

if (empty($_POST) === false)
{
    $fname= $_POST['fname'];
    $srno=  $_POST['SRNO'];
    echo $fname.' and'. $srno;

    mysql_query('update names set fname="$fname" where SRNO="$srno"');          
}

完整的代码是

<!DOCTYPE html>
<html>
    <head>
        <title>List of users</title>
    </head>
<body>
    <?php
        mysql_connect("localhost","root","") or die (mysql_error());
        mysql_select_db("list") or die (mysql_error());

        if (empty($_POST) === false)
        {
            $fname= $_POST['fname'];
            $srno=  $_POST['SRNO'];
            echo $fname.' and'. $srno;

            mysql_query('update names set fname="$fname" where SRNO="$srno"');          
        }


        if(isset($_GET['edit']))
        {
            $getedit=mysql_query('SELECT SRNO, fname, lname, phone, email from names where SRNO='.mysql_real_escape_string((int)$_GET['edit']));
            while ($get_row=mysql_fetch_assoc($getedit))
                {
                    echo '<form method="POST" action="">';
                    echo 'Sr. No: '.$get_row['SRNO'].'<br />';
                    echo 'Sr.No:<input type="text" value='.$get_row['SRNO'].' name="SRNO" readonly="readonly">';
                    echo 'First Name: <input type="text" value='.$get_row['fname'].' name="fname"><br />';
                    echo '<input type="submit" name="submit" value="save">';
                    echo '</form>';                                 
                }   

        }

        $get=mysql_query('SELECT  SRNO, fname, lname, email, phone, address, comments from names ORDER BY SRNO ASC');

        if (mysql_num_rows($get)==0)
        {
            echo 'There are no entries';
        }
            else
        {
            echo '<table border=0>';
            echo'<tr><th>Sr. No</th><th>First Name</th><th>Last Name</th><th>Phone No</th><th>E-mail</th><th>Modify</th></tr>';     
            while($get_row=mysql_fetch_assoc($get))
                {
                    echo '<tr><td>'.$get_row['SRNO'].'</td><td>'.$get_row['fname'].'</td><td>'.$get_row['lname'].'</td><td>'.$get_row['phone'].'</td><td>'.$get_row['email'].'</td><td><a href="index.php?edit='.$get_row['SRNO'].'">Edit</a></td></tr>';

                }
            echo '</table>';
        }       
    ?>

</body> 
</html>
4

3 回答 3

1

单引号内的变量名不会用它们的值替换。尝试这个:

update names set fname="'.$fname.'" where SRNO="'.$srno.'"
于 2013-03-10T20:15:04.773 回答
0


试试这个可能对你有帮助

<!DOCTYPE html>
<html>
 <head>
    <title>List of users</title>
</head>
<body>
<?php
    mysql_connect("localhost","root","") or die (mysql_error());
    mysql_select_db("list") or die (mysql_error());

    if (isset($_POST['fname']))
    {
        $fname= $_POST['fname'];
        $srno=  $_POST['SRNO'];
        echo $fname.' and'. $srno;

        mysql_query("update `names` set fname='$fname' where SRNO='$srno'");          
    }


    if(isset($_GET['edit']))
    {
        $getedit=mysql_query('SELECT SRNO, fname, lname, phone, email from names where     SRNO='.mysql_real_escape_string((int)$_GET['edit']));
        while ($get_row=mysql_fetch_assoc($getedit))
            {
                echo '<form method="POST" action="">';
                echo 'Sr. No: '.$get_row['SRNO'].'<br />';
                echo 'Sr.No:<input type="text" value='.$get_row['SRNO'].' name="SRNO" readonly="readonly">';
                echo 'First Name: <input type="text" value='.$get_row['fname'].' name="fname"><br />';
                echo '<input type="submit" name="submit" value="save">';
                echo '</form>';                                 
            }   

    }

    $get=mysql_query('SELECT  SRNO, fname, lname, email, phone, address, comments from names ORDER BY SRNO ASC');

    if (mysql_num_rows($get)==0)
    {
        echo 'There are no entries';
    }
        else
    {
        echo '<table border=0>';
        echo'<tr><th>Sr. No</th><th>First Name</th><th>Last Name</th><th>Phone No</th><th>E-mail</th>   <th>Modify</th></tr>';     
        while($get_row=mysql_fetch_assoc($get))
            {
                echo '<tr><td>'.$get_row['SRNO'].'</td><td>'.$get_row['fname'].'</td><td>'.$get_row['lname'].'</td><td>'.$get_row['phone'].'</td><td>'.$get_row['email'].'</td><td><a href="index.php?edit='.$get_row['SRNO'].'">Edit</a></td></tr>';

            }
        echo '</table>';
    }       
?>

于 2013-03-10T20:19:20.463 回答
0

我建议您在遇到问题时回显查询,以便您可以将其复制/粘贴到 mysql 客户端并测试错误。

尝试将其更改为:

$query = "update names set fname='$fname' where SRNO='$srno'";
echo $query;
mysql_query($query);

我也建议在您的查询中使用单引号(如我上面的示例)。我不确定 MySQL 处理双引号的效果如何,或者根本不知道。我一直养成使用单引号的习惯。也许这只是个人喜好。

于 2013-03-10T20:19:24.960 回答