0

Description of problem: I want to get value from HTML input with php and to run query on mysql database: SELECT * FROM WHERE (value=value of ID in HTML input)

WHAT I TRY TO DO: I write HTML input:

  <input id="akt_djubrenje" type="text" value="1">

THEN I WRITE PHP:

try {
      /* Establish the database connection */
      $conn = new PDO("mysql:host=localhost;dbname=$dbname", $username, $password);
      $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

     $result = $conn->query("SELECT * FROM akt_djubrenje WHERE ID_akt = :akt_djubrenje");
     $result->execute(array(':akt_djubrenje' => $_POST['akt_djubrenje']));
...
...
etc.

ALSO I give a php file data of input fields and call php file with AJAX:

    function tabela() {

    $.ajax({
            url: 'getdjubrenje.php', // make this url point to the data file
            dataType: 'json',
            data:{akt_djubrenje:$("#akt_djubrenje").val()},
            async: false,
            success:function(json){
            var data = new google.visualization.DataTable(json);
            alert (json);
  // Create and draw the visualization.
  visualization = new google.visualization.Table(document.getElementById('tabela_djubrenje'));
  visualization.draw(data, {'allowHtml': true});
            }
        });
}

My problem is with HOW TO GET INPUT field VALUE from HTML and fetch with php/ajax

When I type exactly value etc. "1" instead $_POST ... all works fine, so problem is with this code. Anybody have some idea to solve my problem?

16
  • "When I type exactly value etc. "1" instead $_POST ... all works fine". You don't use $_POST anywhere. If you submit your form with POST you need to change the $_GET param to a $_POST param. Commented Mar 2, 2014 at 16:18
  • sorry I change that but again dont work Commented Mar 2, 2014 at 16:19
  • I get this error: ERROR: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':akt_djubrenje' at line 1 Commented Mar 2, 2014 at 16:21
  • Then your code isn't what you posted. In the code you posted you use :ajdi. Commented Mar 2, 2014 at 16:22
  • in browser console i dont get any error Commented Mar 2, 2014 at 16:22

2 Answers 2

2

Your problem is that you use PDO::query() and then try to bind variables. That's now how query() works:

PDO::query() executes an SQL statement in a single function call, returning the result set (if any) returned by the statement as a PDOStatement object.

Hence it tries to execute the query right away, not waiting for your execute() call. This is why you get "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near :akt_djubrenje at line 1.".

What you should do is use PDO::prepare() in order to prepare queries and bind variables:

 $result = $conn->prepare("SELECT * FROM akt_djubrenje WHERE ID_akt = :akt_djubrenje");
 //               ^^^^^^
 $result->execute(array(':akt_djubrenje' => $_POST['akt_djubrenje']));
Sign up to request clarification or add additional context in comments.

13 Comments

I try both solution but again is the same - dont work... thanks for helping. I really dont know what can be issue here!
@gmaestro Look in your network tab. Is it being transfered as the correct type (GET/POST). Are the variables inside the request?
IMG of network tab: i.imgur.com/KHdFyuy.png, also you can see network tab at agroagro.com/aktivnosti1.html
@gmaestro You need to click on the POST request to see the headers being sent/received.
can you do that on my example: agroagro.com/aktivnosti1.html and see what can be a problem? please help
|
1

I think you are submitting your request as GET instead of POST. Because you haven't included the sending type as POST in $.ajax. Please correct it.

add option type="POST" in your $.ajax({...,});

instead of

$result = $conn->query("SELECT * FROM akt_djubrenje WHERE ID_akt = :akt_djubrenje");
$result->execute(array(':akt_djubrenje' => $_POST['akt_djubrenje']));

add

 $result = $conn->prepare("SELECT * FROM akt_djubrenje WHERE ID_akt = ?");
 $result->execute(array($_POST['akt_djubrenje']));

4 Comments

how, where i need to type this?
your php code accept post data because you used $_POST in php
dont work i again get the same error: ERROR: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':akt_djubrenje' at line 1
I quit, again dont work, soory for boring you, and thanks for heping me

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.