[RESOLVED] Problems with simple MySql updating in php
Results 1 to 3 of 3

Thread: [RESOLVED] Problems with simple MySql updating in php

  1. #1
    Unappreciated Programmer
    Join Date
    Jan 2006
    Location
    texas
    Posts
    147

    resolved [RESOLVED] Problems with simple MySql updating in php

    I have an issue with receiving an error message when trying to do a simple update on a single record in my coding. I display a form with information on it, and allow the user to modify the information, and then click on an update button. Here is the code for that form:
    PHP Code:
    <?php
    session_start
    ();
    include 
    'config.php';
    ?>


    <form method="post" action="teacherupdate.php">
    <table width="500" cellspacing="2" cellpadding="2" align="left" bordercolor="#804040">
    <tr>
        <th>QID</th>
       <th>Question</th>
        <th>Answer</th>
        <th>Update</th>
        <th>Delete</th>
    </tr>
    <tr>
    <?php
    $query 
    "SELECT * FROM learn";
        
    $result mysql_query($query) or exit(mysql_error());
        while(
    $row mysql_fetch_array($resultMYSQL_ASSOC)) 
            {                    
            
    ?> 
                <td><?php echo $row['key'];?></td>
                <td><textarea name="question" rows="2" cols="25"><?php echo $row['question'];?></textarea></td>
                <td><textarea name="answer" rows="2" cols="25"><?php echo $row['answer'];?></textarea></td>
                <td><button type="submit" name="update" value=<?php echo $row['key'];?>>UPDATE</button></td>
                <td><button type="submit" name="delete" value=<?php echo $row['key'];?>>DELETE</button></td>            
              </tr>        
        <?php }         
        
    mysql_close(); 
        
    ?>
    This works out great to display the information and allow for changing it. Now the form submits to the processing script, and this is the error message I get when the UPDATE button is pressed:

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key=1' at line 1
    Here is the code in the processor script to do this:

    PHP Code:
    <?php
    session_start
    ();
    include 
    'config.php';

    $question=$_POST['question'];
    $answer=$_POST['answer'];
    //$keyid=$_POST['update'];
    $keyid = (ord($_POST['update']))-48;

    $query "UPDATE learn SET question='$question', answer='$answer' WHERE key=$keyid ";

    $result mysql_query($query) or exit(mysql_error());
    ?>
    Previously in another php website which I had written, the successful code to do an update worked just fine. So, I simply copied and pasted the code, changing the info as needed to reflect the table names etc.. Here is the copied code:

    PHP Code:
    $query "UPDATE band SET genre='$genre', description='$description', links='$linkinfo', center='$maincontent', cogs='$widgets', footer='$footinfo' WHERE band = '$band' LIMIT 1"
    I have tried modifying the code via information from many different google lookups for code on updating, but none work. I have verified that my $keyid does give me the correct index number, which is what the dsiplay record is based upon. The previous form displays all of the records for that table, and allows the user to update an individual record and update that record. The update button is linked to that record.

    Any ideas?

    Thank you;
    Ice

  2. #2
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,429
    key is a MySQL reserved word, so you should either not use it for the name of an identifier or always surround that identifier with the proper delimiters (usually a backtick for MySQL).

  3. #3
    Unappreciated Programmer
    Join Date
    Jan 2006
    Location
    texas
    Posts
    147
    Quote Originally Posted by bradgrafelman View Post
    key is a MySQL reserved word, so you should either not use it for the name of an identifier or always surround that identifier with the proper delimiters (usually a backtick for MySQL).
    Thank you very much. That seemed to be the issue. I did not realize this.

    Ice

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •