1

I am having trouble understanding handling variables that are passed through pages when a form submit button is clicked. Basically i have a text area where a user writes an sql query. Then clicks submit. Then on the same page (x.php) , i have to display the results in a table. I figured, when the user clicks the button, i call a function that connects to the database, then runs the query, and outputs the result in a table. The code i have below is a mock, and isnt quite working.But above is essentially what i am trying to do.

In my code, I call the page, and check to see if the proper submit button has been clicked, is that how i am suppose to do it?

Also, I am trying to post the metadata in the code below, but how does the table replace what is already on the page?

<html>
<head> 
<title>CNT 4714 - Project Five Database Client</title>
</head>
<body style="background-color:white">
<center>
<h1 style="color:red">CNT 4714 - Project Five Database Client</h1>
<hr>
<div style="float:left; text-align:left;padding-right:80px; padding-left:80px; ">
<font color="yellow">
<?php

?>
Welcome Back!<br>
<?php echo $_POST["user"];?>
  </font>
</div>
<font color="yellow">
<div style="float:left; text-align:left">
<center>
<h2 style="color:green">Enter Query</h2><br><br>
Please enter a valid SQL Query or update statement. You may also just press<br>
"Submit Query" to run a defualt query against the database!
<form action="" id="sql" method="post">
<br>
  <textarea rows="10" cols="50" name="query" form="sql">Enter text here...</textarea><br><br>
  <input type="submit" name="submit" color="red">
  <input type="submit" name="" color="red" value="Submit Update">
</form>
<?php

if(isset($_POST['submit'])){
    echo "hello";
   query(); //here goes the function call
}
function query()
{
    echo "hello";
    $conn = mysqli_connect("localhost:3306", "root", "*******", "project4");
    $query = $_POST["query"];
    $result = mysqli_query($conn, $query);
    $metadata = mysqli_fetch_fields($result);
    print("<tr>");
    for($i=0; $i<count($metadata);$i++){
        print("<tr");
        printf("%s",$metadata[$i]->name);
        print("</tr>");
    }
}


?>
</center>
</div>
</font>
</center>

</body>
</html>
1
  • Give a name to the submit button as submit and in form tag add method="POST". Commented Dec 1, 2016 at 6:45

2 Answers 2

2

You are trying to get the values of the global variable $_POST while you are posing it to $_GET. The way to fix this is assigning the method into your form element.

Example:

<form id="sql" action="" method="POST">

There are many ways for checking or the form is submitted, one of this ways (the one I am always using) is checking or the $_SERVER['REQUEST_METHOD'] is equal to "POST". This way you can tell the different between a GET, POST, or PUT request.

Example:

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    if(isset($_POST['sql']))
    {
        ....
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

ok thanks, I have updated the code to what i have now, and i cant seem to get the data to print. I got the sql query to run without an error, what could i be doing wrong?
Why not just if (isset($_POST['sql']))? It won't be set if it isn't a post request.
i used that and it works. But i used the name of the button, since i have multiple buttons. DevNiels said there are many ways to do it. I guess his way is one way of doing it.
@MagnusEriksson Like I said in my post, there are many ways to do it, I like this way because I can check or there is a request sent whitch tells me someone is sending data. I am also able to sort out or it's a GET, POST or PUT request.
0

If you're using $_POST, your form request method should be POST.

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

Otherwise, it will submit it with a GET request by default.

In that case, you will have to access the variable using $_GET instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.