mysql - Perl, SQL, DBI : Why doesn't my UPDATE function work? 代码和我在里面尝试过的东西

标签 mysql sql perl dbi

更新2:我从来没有找到解决方案。由于讲师的代码很容易让我们与我们自己的代码一起使用和破解,而且我已经浪费了数周时间尝试调试,所以我刚刚创建了一个新表并从他的代码开始,进行了更改并在我进行时进行了测试,直到我最终得到了我想要的功能。如果有人遇到这种奇怪行为的原因,我会真诚地感谢知道是什么原因造成的。

几乎所有提出任何建议的人都提出了我已经尝试过的想法,并在我原来的帖子中列出了已经尝试过的想法。感谢大家提供他们的见解,但请在提出建议之前完整阅读帖子。

更新:更清楚地提出我的主要问题。原始帖子低于所有代码以及我尝试过的代码。

我设计了我的 updateData() 函数来更新记录。相反,它返回其错误代码。

sub updateData {
    # and this returns "Update Failed!" for me. I can't figure out why.

    $sql = "UPDATE friendface SET first_name = '$first_name',
        last_name = '$last_name',
        email_address = '$email_address',
        password = '$password'
        WHERE user_id = $user_id;";

    $rc = $dbh->do($sql);
    if ($rc != 1) {
        print "Update Failed!";
    }
}

忍者编辑,我重读这个烂摊子 3:

代码在底部。对不起,如果格式很糟糕或者我描述性不够。我真的试过了。问我是否可以提供任何其他有用的东西。

大家好,

学校作业在这里,打印一个表格,其中包含您可以编辑和删除的记录。

我有一个带有用于插入数据的表单的 HTML 页面,然后是一个显示带有更新和删除按钮的表格的 perl 脚本。删除工作正常。整个脚本工作正常(除了当您直接访问 .pl 时获得的文本 block 中的链接,但我目前不在乎)没有我的 UPDATE 函数的内容。 UPDATE 函数的代码在 MySQL 中逐行运行良好。但我无法获得 updateData在我的 .pl 文件中使用 UPDATE 查询的函数。

我们被允许完全访问讲师的示例代码。我可以从他的页面开始,然后将其修改为我自己的页面。我宁愿不用依靠那个拐杖就可以编写自己的页面。但是,我正在将我的代码与他的代码进行比较,但我看不出我哪里出错了。据我所知/可以找到文档,语法看起来不错。
  • 为了以防万一,我还是使用了不同的语法。在我的 WHERE 子句之前的最终记录之后加一个逗号,因为没有它看起来是错误的,但显然没有是要走的路。我读到的所有内容都表明当前的语法是正确的。
  • 我想也许它正在尝试编辑 user_id不存在,但打印 user_id表明它使用的是正确的。
  • 我想我的 DBI->connect正在工作,因为它不返回连接失败。
  • 正确的函数,updateData() , 正在运行,因为最终输出是该函数独有的更新失败返回码。
  • 我可以通过 Edit button > Edit form > Confirm Edit function > updateData function > $dbh 验证成功,do($sql) 来跟踪代码,这是我的 UPDATE 查询,看起来语法正确。但由于某种原因,$dbh->do($sql);结果不是真的。
  • updateData功能,我想我会确保问题不在于我只更新了 6 个字段中的 4 个。好吧,5,因为 key 永远不会更新。无论如何,我认为这可能会以某种方式绊倒它,但是添加一行来更新用户名并没有帮助,所以我又把它拿出来了。

  • 我真的一直在试图自己解决这个问题,我正在拔头发。它总是一些愚蠢的小东西。是间距吗?通常没关系,但我知道ENDFORM为了为我工作,必须是第一个,也是唯一一个上线的东西。但我不知道这个特定代码的任何间距规则。请。为我指明正确的方向。

    明确地说,printEditButton()printDeleteButton()是编辑和删除按钮的代码...

    DESC 表、html 页面的代码和 pl 脚本如下:
    *********************
    *********************
    ***DESC friendface***
    *********************
    *********************
    
    Field   Type    Null    Key Default Extra
    user_id int(11) NO  PRI NULL    auto_increment
    username    char(50)    YES     NULL    
    first_name  char(20)    YES     NULL    
    last_name   char(20)    YES     NULL    
    email_address   char(50)    YES     NULL    
    password    char(50)    YES     NULL    
    
    
    
    
    *********************
    *********************
    *friendFaceForm.html*
    *********************
    *********************
    
    <table bgcolor='antiquewhite' align=center cellpadding=2>
    <form action='friendFaceForm.pl' method=GET>
    
    <tr>
        <td align=right>Username</td>
        <td><input type=text name=username></td>
    </tr>
    
        <tr>
            <td align=right>First Name</td>
            <td><input type=text name=first_name></td>
        </tr>
    
        <tr>
            <td align=right>Last Name</td>
            <td><input type=text name=last_name></td>
        </tr>
    
    
        <tr>
            <td align=right>Email Address</td>
            <td><input type=text name=email_address></td>
        </tr>
    
        <tr>
            <td align=right>Password</td>
            <td><input type=text name=password></td>
        </tr>
    
        <tr>
            <td align=center colspan=2> <input type=submit name=action value='Insert Data'></td>
        </tr>
    
    
    </form>
    </table>
    
    
    *********************
    *********************
    **friendFaceForm.pl**
    *********************
    *********************
    
    #!/usr/bin/perl
    
    use CGI qw(:standard);
    use DBI;
    use warnings;
    
    print "Content-type: text/html\n\n";
    
    $dbh = DBI->connect("DBI:mysql:jwiard1:localhost", "jwiard1", "jwiard1")
        or endProgram("Connection Failed!");
    
    $action = param('action');
    $user_id = param('user_id');
    $username = param('username');
    $first_name = param('first_name');
    $last_name = param('last_name');
    $email_address = param('email_address');
    $password = param('password');
    
    if ($action eq 'Insert Data') {
    #$action holds this value coming from the html page
    #this happens first
    
        insertData();
        printTable();
    
    }
    elsif ($action eq 'Edit') {
    #prints the edit form
    
        printEditForm();
    
    }
    elsif ($action eq 'Confirm Edit') {
    #then updateData() runs
    
        updateData();
        printTable();
    
    }
    elsif ($action eq 'Delete') {
    
        deleteData();
        printTable();
    
    }
    elsif ($action eq 'Print Table') {
    
        printTable();
    
    }
    else {
    
        print "Either you are accessing this file directly or \$action has an unaccounted         for value.<br><br>
    
        If it's the former, get out of here!<br><br>
    
        If it's the latter... you're getting sleepy. You're getting verrrry sleepy. When     you reach the end of this sentence, you'll wake up with no memory of this page and a strong     feeling that Joe Wiard's code is perfect.<br><br>
    
        ...or did you just want to see the table?";
    
        print "<input type=submit name=action value='Print Table'>";
    
    }
    
    
    
    
    
    
    
    ####
    #Functions! Yay!
    ####
    
    sub endProgram {
        my ($msg) = @_;
        print $msg;
        die();
    }
    
    sub insertData {
    #after inserting data, the user is left to click Edit or Delete
    #making $action hold the value of 'Edit' or 'Delete' Go to elsif($action eq 'Edit'
    
        print "Your data has been saved.";
    
        $sql = "INSERT INTO friendface SET user_id='$user_id',
            username='$username',
            first_name='$first_name',
            last_name='$last_name',
            email_address='$email_address',
            password='$password'  ;";
    
            $rc = $dbh->do($sql);
            if ($rc != 1) {
                print "Insert failed!";
            }
    }
    
    sub printEditButton {
    
    print "<form>";
    print "<input type=hidden name=user_id value='$href->{user_id}'>";
    print "<input type=submit name=action value='Edit'>";
    print "</form>";
    
    }
    
    sub printDeleteButton {
    
    print "<form>";
    print "<input type=hidden name=user_id value='$href->{user_id}'>";
    print "<input type=submit name=action value='Delete'>";
    print "</form>";
    
    }
    
    sub confirmEdit {
    
    
    
    }
    
    sub lookUpRow {
    
        $sql = "SELECT * FROM friendface WHERE user_id=$user_id;";
    
        $sth = $dbh->prepare($sql);
    
        $rc = $sth->execute();
    
        $href = $sth->fetchrow_hashref();
    
    }
    
    sub printEditForm {
    #prints fields for 4 of the values in a record. I don't want the user to be able to
    #change their username. They can only edit first and last names, email and password.
    #after this, $action either holds 'Confirm Edit' or 'Cancel'. Go to elsif
    #($action'Confirm Edit')
    
        lookUpRow();
    
        print <<ENDOFFORM;
        <form>
        First Name: <input type=text name=first_name value='$href->{first_name}'> <br>
        Last Name:  <input type=text name=last_name value='$href->{last_name}'> <br>
        Email Address:  <input type=text name=email_address value='$href->{email_address}'>         <br>
        Password:  <input type=text name=password value='$href->{password}'> <br>
        <input type=hidden name=user_id value=$href->{user_id}'> <br>
        <input type=submit value="Confirm Edit" name=action>
        <input type=submit value="Cancel" name=action>
        </form>
    ENDOFFORM
    #It seems that ENDOFFORM *MUST* be at the beginning of the line. No TABS or SPACES
    #preceeding, and NOTHING after. Half an hour of debugging lead me to discovery this
    #tidbit that I should have just remembered from class. Or Googled. :P
    }
    
    sub updateData {
    #and this returns "Update Failed!" for me. I can't figure out why.
    
    
        $sql = "UPDATE friendface SET first_name = '$first_name',
            last_name = '$last_name',
            email_address = '$email_address',
            password = '$password'
            WHERE user_id = $user_id   ;";
    
        $rc = $dbh->do($sql);
        if ($rc != 1) {
            print "Update Failed!";
        }
    
    }
    
    sub deleteData {
    
        $sql = "DELETE FROM friendface WHERE user_id = $user_id;";
    
        $rc = $dbh->do($sql);
    
    }
    
    sub printTable {
    
        $sql = "SELECT * FROM friendface;";
        $sth = $dbh->prepare($sql);
        $rc = $sth->execute();
    
        $count = 0;
    
        print "<table>";
    
        #print header
    
        while ($href = $sth->fetchrow_hashref() ) {
    
            $count ++;
            if ($count % 2 == 0) {
                print "<tr bgcolor=lightblue>";
            }
            else {
                print "<tr bgcolor=lightgray>";
            }
    
            print "<td>";
            print $href->{'user_id'};
            print "</td>";
            print "<td>";
            print $href->{'username'};
            print "</td>";
            print "<td>";
            print $href->{'first_name'};
            print "</td>";
            print "<td>";
            print $href->{'last_name'};
            print "</td>";
            print "<td>";
            print $href->{'email_address'};
            print "</td>";
            print "<td>";
            print $href->{'password'};
            print "</td>";
            print "<td>";
            printEditButton();
            print "</td>";
            print "<td>";
            printDeleteButton();
            print "</td>";
    
            print "</tr>";
        }
    
        print "</table>";
    

    最佳答案

    来自 DBI documentation ,似乎“do”方法对返回值做了一些魔术:

    do

    If you're doing an UPDATE, INSERT, or DELETE there is no data that comes back from the database, so there is a short cut. You can say

    $dbh->do('DELETE FROM people WHERE age > 65'); for example, and DBI will prepare the statement, execute it, and finish it. do returns a true value if it succeeded, and a false value if it failed. Actually, if it succeeds it returns the number of affected rows. In the example it would return the number of rows that were actually deleted. (DBI plays a magic trick so that the value it turns is true even when it is 0. This is bizarre, because 0 is usually false in Perl. But it's convenient because you can use it either as a number or as a true-or-false success code, and it works both ways.)



    你确定更新没用?之后执行选择以仔细检查。可能只是您误解了返回码。

    关于mysql - Perl, SQL, DBI : Why doesn't my UPDATE function work? 代码和我在里面尝试过的东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20135982/

    相关文章:

    mysql - SQL - 触发器和意外事件

    php - MySQL - 比较来自 Ajax 数组的值,如果不在表中则删除它们

    mysql - 用于大量使用 wordpress 博客的简单脚本解决方案(shell、perl、其他)?

    mysql - 如何编写好的Mysql授权脚本

    MySQL WHERE OR : if first condition is true, 第二次执行?

    php - 使用 PHP 在 MySQL 中搜索完全匹配的单词

    php - mysql 获取值(值结束=0)

    sql - 递归 SQL 语句 (Postgresql) - 简化版

    html - Perl:HTML::PrettyPrinter - 处理自闭合标签

    perl - 标量值错误(使用 IO::Socket)