Select to view content in your preferred language

Edit Form PHP PDO

7476
4
09-01-2016 08:31 AM
SarojThapa1
Frequent Contributor

I have a table in SQL Server database. I created an "edit.php" where we can update the database. The database connection is established using PDO. The issue is: I have a lot of Null values in the table. When I click "Edit" option, the form pops out. If I completely fill the records, the database gets updated. Otherwise I get this error message. SQLSTATE[42000]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Error converting data type nvarchar to numeric.

The codes for my edit.php are included below:

require_once('database.php');
if (isset($_POST['btn_submit'])) {        
   $id = $_POST['txt_id'];        
   $site = $_POST['txt_site_code'];        
   $location = $_POST['txt_location_name'];  

try {       
$stmt = $conn->prepare("UPDATE MATRIX SET Site_Code=:site, 
               Location_name=:location WHERE OBJECTID =:id");
$stmt->execute(array(':site' => $site, ':location' => $location,':id' => $id));
if ($stmt) {                     
   header('Location:index.php');                
}             
} catch (PDOException $e) {               
echo $e->getMessage();            
}
}     
$object_id = '';    
$site = '';    
$location = '';  

if (isset($_GET['id'])) {        
      $id = $_GET['id'];        
      $stmt = $conn->prepare("SELECT * FROM MATRIX WHERE OBJECTID=:id");        
      $stmt->execute(array(':id' => $id));        
      $row = $stmt->fetch();        
      $object_id = $row['OBJECTID'];        
      $site = $row['Site_Code'];        
      $location = $row['Location_name'];
}
?>    
<h2>Edit the records</h2>     
<form action="" method="post">        
   <table border="3px" cellpadding="5px">             

   <tr>                
      <td>Site Code</td>                
      <td><input type="text" name="txt_site_code"  value="<?= $site; ?>"></td>            
   </tr>             

   <tr>                
      <td>Location Name</td>                
      <td><input type="text" name="txt_location_name" value="<?= $location; ?>"></td>            
   </tr>              

   <tr>                
   <td><input type="hidden" name="txt_id" value="<?= $object_id; ?>"></td>                
   <td><input type="submit" name="btn_submit" value="Submit"></td>            
   </tr>
</table>
</form>

Thanks

0 Kudos
4 Replies
KristenVarney
New Contributor

Saroj Thapa‌ Did you ever figure this out? How has it been working for you?

0 Kudos
SarojThapa1
Frequent Contributor

Hi Kristen, I did figure this one out. It's been a while, I even forgot about this post. Will find how I solved it and share it here shortly.

0 Kudos
SarojThapa1
Frequent Contributor

I added empty strings "" with else clause. Something like this:

if (isset($_POST['btn_submit'])) {
    if (isset($_POST['txt_id'])) {
        $id = $_POST['txt_id'];
    } else {
        $id = '';
    }
}
0 Kudos
KristenVarney
New Contributor

Thanks for your reply!

0 Kudos