Fill in all the required fields. I press ADD RECORD (after which the values ​​should be entered into the table in the database), but nothing happens when I press the ADD RECORD button.

I came to the conclusion that the request is simply not executed. What could be the error?

if(isset($_POST['author']) && isset($_POST['title']) && isset($_POST['category']) && isset($_POST['year']) && isset($_POST['isbn'])) { $stmt->bindParam(':author', $author); $stmt->bindParam(':title', $title); $stmt->bindParam(':category', $category); $stmt->bindParam(':year', $year); $stmt->bindParam(':isbn', $isbn); /*$author = $_POST['author']; $title = $_POST['title']; $category =$_POST['category']; $year = $_POST['year']; $isbn = $_POST['isbn'];*/ // Не знаю нужна ли эта часть кода $stmt = $pdo->prepare("INSERT INTO classics (author,title,category,year,isbn) VALUES(:author,:title,:category,:year,:isbn)"); $stmt->execute(); } 

Closed due to the fact that off-topic participants artoodetoo , GrayHoax , dlarchikov , Darth , Dmitriy Simushev September 3, '15 at 11:12 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - GrayHoax, dlarchikov
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • do you have everything in the code in the same sequence? - toxxxa
  • one
    bindParam must be after prepare . but before that, of course, variables should be defined. if you are wondering if this part of the code is needed, then I have bad news for you))) - toxxxa
  • You declared the request parameters, but did not specify their values. Look carefully at all the examples and comments on PDO :: prepare () php.net/pdo_prepare - artoodetoo
  • @artoodetoo: there is no such reason for closing. - Nick Volynkin
  • 2
    @NickVolynkin, I understand that. but the wording "it is necessary to clarify the question" does not reflect the essence of the problem. And the problem is. The normal situation is when the author knows what he wants to do, but does not know how. And when the author simply copies the fragments in one place and asks to bring to mind in another place - this is an imitation. - artoodetoo

1 answer 1

In short, an error in the sequence of commands executed.

Let's deal with the sequence:

  1. Check for data; a conditional if and a set of checks like isset($_POST['author']) are responsible for this. The isset() function checks if a variable has a value.
  2. Get data from a query into local variables. This is just a block of code that you have commented out of the form $author = $_POST['author'];
  3. Prepare a request. This is the line $stmt = $pdo->prepare("INSERT INTO classics (author,title,category,year,isbn) VALUES(:author,:title,:category,:year,:isbn)"); , the request object is now stored in the $stmt variable
  4. Fill in the query variables like this: $stmt->bindParam(':author', $author); . This line of the :author variable in the request above will assign the value from the $author variable obtained in clause 2.
  5. Execute the query: $stmt->execute();

So your code should look like this:

 if(isset($_POST['author']) && isset($_POST['title']) && isset($_POST['category']) && isset($_POST['year']) && isset($_POST['isbn'])) { $author = $_POST['author']; $title = $_POST['title']; $category =$_POST['category']; $year = $_POST['year']; $isbn = $_POST['isbn']; $stmt = $pdo->prepare("INSERT INTO classics (author,title,category,year,isbn) VALUES(:author,:title,:category,:year,:isbn)"); $stmt->bindParam(':author', $author); $stmt->bindParam(':title', $title); $stmt->bindParam(':category', $category); $stmt->bindParam(':year', $year); $stmt->bindParam(':isbn', $isbn); $stmt->execute(); } 

For the future, I also recommend reading about the filter_* functions.