0

I am doing an update statement into my database. My connection has been done properly. However there is an issue. After $conn-> the remaining of my codes are being displayed out just like echo instead of the update statement updating the database. I have been trying to debug it but nothing seems to work. Unsure of the error. Do help to identify the error.

   <?php//check on the updating
   if (isset($_POST['set'])){
       $query = 'UPDATE default SET sql_statement ="'.$_POST['sql'].'", x_axis = "'.$_POST['x'].'", y_axis = "'.$_POST['y'].'" WHERE id = "'.$id.'" ';
       $result = $conn->query($query);
       if($result){
           header('Location:previewgraphs.php?id='.$id);
           die();
       }
 }
 ?>
Carlos Robles
  • 10,828
  • 3
  • 41
  • 60
DivyaK
  • 33
  • 1
  • 9

3 Answers3

0

Try this:

 <?php//check on the updating
       if (isset($_POST['set'])){
       $query = 'UPDATE default SET sql_statement ="'.$_POST['sql'].'", x_axis = "'.$_POST['x'].'", y_axis = "'.$_POST['y'].'" WHERE id = "'.$id.'" ';
       $result = $conn->query($query);
       if($result){
       header('Location:previewgraphs.php?id='.$id);
       die();
       }
     }
       ?>
Maha Dev
  • 3,915
  • 2
  • 31
  • 50
  • Still dosent work out. All the remaining after the -> is still being shown query($query); if($result){ header('Location:previewgraphs.php?id='.$id); die(); } } ?> – DivyaK Nov 05 '15 at 06:41
  • @NiranjanNRaju, we always need to write die() after header for proper redirection. – Maha Dev Nov 05 '15 at 06:42
0

Some where your php tags have problem. So use " to wrap and also when you are using array index in concatenation, wrap them with {}

<?php 
   if (isset($_POST['set'])){
   $query = "UPDATE default SET sql_statement ='{$_POST['sql']}', x_axis ='{$_POST['x']}', y_axis = '{$_POST['y']}' where id = $id";
   $result = $conn->query($query);
   header('Location:previewgraphs.php?id='.$id);
   }
?>
Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41
  • Thanks! It works. What if I want to do a page refresh after updating instead of redirecting to the page. Any idea how to do it? – DivyaK Nov 05 '15 at 06:53
  • you are on same page?? i mean your php code is in same page with html?? – Niranjan N Raju Nov 05 '15 at 06:56
  • Yes. Its a pop up box on the same page. So once updated, I need to refresh the page so the contents are updated. Any idea how to do it? – DivyaK Nov 05 '15 at 06:58
  • I only want to refresh after doing an update. So the page will just auto refresh when I click the update button – DivyaK Nov 05 '15 at 07:02
  • query($query);header("Refresh: 0; url=previewgraphs.php?id=".$id); } ?> Not refreshing – DivyaK Nov 05 '15 at 07:12
0
<?php//check on the updating
               if (isset($_POST['set'])){
               $query = "UPDATE default SET sql_statement ='".$_POST['sql']."', x_axis = '".$_POST['x']."', y_axis = '".$_POST['y']."' WHERE id = '".$id."'";
               $result = $conn->query($query);
               if($result){
               header('Location:previewgraphs.php?id='.$id);
               die();
               }
             }
             ?>
M3ghana
  • 1,231
  • 1
  • 9
  • 19