Jump to content

Could not update data: Unknown column 'zxc' in 'field list'

<html>
   
   <head>
      <title>Update a Record in MySQL Database</title>
   </head>
   
   <body>
      <?php
         if(isset($_POST['update'])) {
              mysql_connect("localhost", "root" ,"");      
              mysql_select_db("project");
            
            $conn =   mysql_connect("localhost", "root" ,"");
            
            if(! $conn ) {
               die('Could not connect: ' . mysql_error());
            }
            session_start();
            $emp_id = $_POST['emp_id'];
            $Newname = $_POST['Newname'];
               $tempo =$_SESSION['Username'];
             echo $tempo;
            $sql = "UPDATE user ". "SET Name = $Newname ". 
               "WHERE id = 6" ;
            mysql_select_db('test_db');
            $retval = mysql_query( $sql, $conn );
            
            if(! $retval ) {
               die('Could not update data: ' . mysql_error());
            }
            echo "Updated data successfully\n";
            
            mysql_close($conn);
         }else {
            ?>
               <form method = "post" action = "edit.php">
                  <table width = "400" border =" 0" cellspacing = "1" 
                     cellpadding = "2">
                  
                     <tr>
                        <td width = "100">Employee ID</td>
                        <td><input name = "emp_id" type = "text" 
                           ></td>
                     </tr>
                  
                     <tr>
                        <td width = "100">Employee Salary</td>
                        <td><input name = "Newname" type = "text" 
                         ></td>
                     </tr>
                  
                     <tr>
                        <td width = "100"> </td>
                        <td> </td>
                     </tr>
                  
                     <tr>
                        <td width = "100"> </td>
                        <td>
                           <input name = "update" type = "submit" 
                              value = "Update">
                        </td>
                     </tr>
                  
                  </table>
               </form>
            <?php
         }
      ?>
      
   </body>
</html>

Link to comment
Share on other sites

Link to post
Share on other sites

Does your user table in test_db database have Name field?

 

If you would like to share schema of this table.

 

If it says that there is no such field, then it may be the case.

 

I would recommend you to use at least mysqli (but PDO is better) and prepared statements, mysql functions you're using right now are deprecated, and your current code is exposed to SQL injection.

 

I also wonder why you concatenate strings like that

4 hours ago, WannaBeeComputerGeek said:

"UPDATE user ". "SET Name = $Newname ". "WHERE id = 6" ;

which would have same effect as

"UPDATE user SET Name = $Newname WHERE id = 6" ;

and only concatenation I would consider as needed would look like this:

'UPDATE user SET Name = \''.mysql_real_escape_string($Newname).'\' WHERE id = 6';

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×