0

I have a form. Users are supposed to enter sql statement into the form textarea box. The sql statement will generate results. However, when I insert an sql statement into the form, it displays results to the previous SQL statement inserted into the form. It does not show the current SQL statement inserted results. Any help for this?

PHP Code

<?php
    if (isset($_POST['submit']))
    {
        $sqlinput =  $_POST['sql_input'];
        $title = $_POST['title'];
        $description = $_POST['description'];
        $assigneduser = $_POST['assigned_user'];

        $new = "INSERT INTO `new_default_reports`(`sql_statement`, `description`, `graph_title`, `assigned_user`) VALUES ('$sqlinput' , '$description', '$title' , '$assigneduser')";
        $resultnew = $conn->query($new);

        $query = "SELECT * FROM `new_default_reports` ORDER BY `id` DESC ";
        $result = $conn->query($query);
        $row = $result->fetch_assoc();
        $id_report = $row['id'];
        header('Location: previewgraphs.php?notify=yes&id='.$id_report);
    }

Form

<form method="post" action="">

    <label>Graph Title</label>
    <input type="text" id= "title" name="title" class="form-control" value =<?php if (isset($_POST['submit'])){ echo $_POST['title'];} ?>

    <label>Description</label>
    <textarea class="form-control" name = "description" rows="3" id="desc" placeholder="Graph Description"><?php if (isset($_POST['submit'])){ echo $_POST['description'];} ?></textarea>

    <div class="form-group">
    <label> Query Generator </label>
    <textarea class="form-control" name="sql_input" rows="5" id="comment" placeholder="Select statement input here..."><?php if (isset($_POST['submit'])){ echo $_POST['sql_input'];} ?></textarea>

    <button type="submit" name="submit" class="btn btn-primary">Generate Graph</button>
</form>

Previewgraph.php

<?php
    $id =  $_GET['id'];
    $query = "SELECT * FROM new_default_reports WHERE id = '$id'";
    $result = $conn->query($query);
    $row = $result->fetch_assoc();
    $query = $row['sql_statement'];
    $result = $conn_temp->query($query);


    <div class="col-md-12">
        <?php if(isset($_REQUEST['notify'])){ ?>
        <div class="alert alert-success fade in">
        <a href="#" class="close" data-dismiss="alert">&times;</a>
        A New Graph <?php echo $row['graph_title']; ?> has been created.
    </div>
    <?php } ?>

    <?php
    $query = $row['sql_statement'];
    $result = $conn_temp->query($query);
?>
catzilla
  • 1,901
  • 18
  • 31
DivyaK
  • 33
  • 1
  • 9

0 Answers0