0

I have a really hard time cracking this nut.

MySQL database consist of the following columnnames: id, stores, results.

I have a button inside a form. When I click the button "Select", the columnname records should update with +1. Everytime I click the button I get the error:

Notice: Undefined index: id in /Applications/MAMP/htdocs/practiceProject/updaterecords.php on line 8 Updated Succesfull

I was reading on stackoverflow that it is because I am not getting the value of the id. Therefore I added $id = $_POST['id'];, but still getting the same error.

Does anybody have an idea what there is going wrong here?

index.php

<form action="updaterecords.php" method="post">
   <button type="submit" name="selectstore" >Select</button>
    <?php include 'updaterecords.php' ?>
  <button type="button" data-dismiss="modal">Close</button>
</form>

updaterecords.php

<?php error_reporting(E_ALL); ini_set('display_errors', 1);

if (mysqli_connect_errno()) { echo "Error: no connexion allowed : " . mysqli_connect_error($mysqli); } 
?>
<?php
include 'dbconnection.php';

if(isset($_POST['selectstore'])) {
$id = $_POST['id']; // Line 8

    $stmt = $mysqli->prepare("UPDATE stores SET records = records + 1 WHERE id= ?");

    $stmt->bind_param('i', $id);   

    if ($stmt->execute()) { 
        $success = true;
    }

    $stmt->close();

    $mysqli->close();   
    if($success) {
        echo "Updated Succesfull";
    } else {
        echo "Failed: " .  $stmt->error;
      }
    }

?>
NMkdk4
  • 13
  • 7
  • Does your form have a field with `name="id"`? If it doesn't `$_POST['id']` will never be populated. – Jay Blanchard May 05 '17 at 19:11
  • Thank you for the comment. My field in the form on this question, or when I insert data, or? – NMkdk4 May 05 '17 at 19:36
  • 1
    The form you show above has no input named `id`, so `$_POST['id']` is not set. And why are you including a file in the middle of the form? – Jay Blanchard May 05 '17 at 19:38

2 Answers2

0

You are not posting a value for your id. How should your php script know which row to update? You don't have an input field (or something similar) in your html, so $_POST['id'] is not defined.

maja
  • 17,250
  • 17
  • 82
  • 125
  • Thank you for the answer. I am using $_POST['id'] in around 25 different documents within this project. Everything is working, except this one. – NMkdk4 May 05 '17 at 19:17
-1

Your bind param should start with :id also make sure $id = $_POST['id']; is being posted

<form action="updaterecords.php" method="post">
<!-- test the id -->
<input type="hidden" value="333" name="id" />
   <button type="submit" name="selectstore" >Select</button>
   <!-- removed php include don't need it here -->
  <button type="button" data-dismiss="modal">Close</button>
</form>

updaterecords.php

        <?php error_reporting(E_ALL); ini_set('display_errors', 1);

        if (mysqli_connect_errno()) { echo "Error: no connexion allowed : " . mysqli_connect_error($mysqli); } 
        ?>
        <?php
        include 'dbconnection.php';

        if(isset($_POST['selectstore'])) {
        $id = $_POST['id']; // Line 8

            $stmt = $mysqli->prepare("UPDATE stores SET records = records + 1 WHERE id = :id");

                $stmt->bindParam(':id', $id);   

            if ($stmt->execute()) { 
                $success = true;
            }

            $stmt->close();

            $mysqli->close();   
            if($success) {
                echo "Updated Succesfull";
            } else {
                echo "Failed: " .  $stmt->error;
              }
            }

        ?>
Otávio Barreto
  • 1,536
  • 3
  • 16
  • 35
  • Starting a placeholder with a colon *is not* required. http://stackoverflow.com/questions/17386469/pdo-prepared-statement-what-are-colons-in-parameter-names-used-for You also do not have to use named placeholders, as `?` is a *perfectly acceptable* placeholder. – Jay Blanchard May 05 '17 at 19:08
  • Hello. Thank you a lot for your answer. I just testing your code, but my database is still not updating with +1. But when I click the select button I do not get any error, just a blank page on `updaterecords.php`. My query is working fine in myPhpAdmin if I use an id number. Fx: `UPDATE stores SET records = records + 1 WHERE id = 333`; – NMkdk4 May 05 '17 at 19:08
  • But it's better than ? because it's easier to identify the param – Otávio Barreto May 05 '17 at 19:09
  • It may be better, but it isn't required and doesn't fix the OP's problem. – Jay Blanchard May 05 '17 at 19:09
  • it's probally a error on your query or nomenclature error let me check all database tables used for this – Otávio Barreto May 05 '17 at 19:11
  • 1
    You should remove your comment under the OP's question. – Jay Blanchard May 05 '17 at 19:11
  • also make sure `$id = $_POST['id'];` is being posted – Otávio Barreto May 05 '17 at 19:13
  • Hello. Still the same problem with your code :-( – NMkdk4 May 05 '17 at 19:18
  • Test my code now I updated see if you get the id value – Otávio Barreto May 05 '17 at 19:19
  • Thank you a lot for your efford. Unfortuanlly when I click select I keep getting a whitepage with no errors, but the number is still not updated in the database. – NMkdk4 May 05 '17 at 19:28
  • 1
    I removed the php include in your form now , maybe this is what is causing the problem – Otávio Barreto May 05 '17 at 19:29
  • There must be something totally else wrong. In all my other documents that include `$id = $_POST['id'];` is working perfectly fine. It is only this document. I still get the same blank page with no update in mysql. – NMkdk4 May 05 '17 at 19:33
  • You have no `name="id"` in your code so I added a hidden input id just for test your code `` please test all again copy all the code in my answer and test – Otávio Barreto May 05 '17 at 19:37
  • In your code you are writing: "this is how your php would be ". Is it the below code you mean? There is quite some dublicate code in it. – NMkdk4 May 05 '17 at 19:43
  • no I made changes in all code – Otávio Barreto May 05 '17 at 19:44
  • But if you look the code you posted just above there is the text : " this is how your php would be". There is 9 lines of code there is dublicates – NMkdk4 May 05 '17 at 19:47
  • Thank you a lot. Ok now something is starting to happening :-) The only place I get the error I get now is on line 13: `$stmt->bind_param(':id', $id);`- `Uncaught Error: Call to a member function bind_param() on boolean in` – NMkdk4 May 05 '17 at 20:05
  • change `bind_param` to `bindParam` I will update the answer – Otávio Barreto May 05 '17 at 20:12
  • 1
    Thank you a lot for your help. I get a blank page and no update in the database. But at least I am not getting the error like before. I will accept your answer, when I figured out the rest, so I can write it here. You used way to much time on my question now, so I will let you :-) – NMkdk4 May 05 '17 at 20:25
  • don't know what Is causing it I should see all code to understand, probally it's a data type issue in database because `id is a string` but in database `id is numeric` – Otávio Barreto May 05 '17 at 20:30
  • I thought it was ´d´ for digit in the database? I have less than 15 points, so I cannot accept your answer yet. But I promise I will accept the answer :) – NMkdk4 May 05 '17 at 20:32
  • show me your data base columns and tables – Otávio Barreto May 05 '17 at 20:32
  • Ok cool. It is here: https://ibb.co/c5VQ1Q – NMkdk4 May 05 '17 at 20:39
  • change **id type to text** and see how it goes – Otávio Barreto May 05 '17 at 22:37