-1

I have received an ID from a get request and put it into new variable $id. I am trying to include this variable in an SQL query but it doesn't work. It does however work when I hardcode the ID.

This works fine.

$query = "UPDATE products SET p_name = " . "'TEST' WHERE p_id=000007;";

The following code does NOT work. Can anyone explain it?

$query = "UPDATE products SET p_name = " . "'TEST' WHERE p_id=" . $id . ";";

All of my code if anyone can help:

        <?php

        // Connection file
        //require 'db.php';

        // Create connection
        $conn = mysqli_connect($servername, $username, $password, $dbname);

        // Check Connection
        if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
        }

            $id = $_GET['id'];
            echo $id;


                if (isset($_POST["updateSubmit"])) {



                $query = "UPDATE products SET p_name = " . "'TEST' WHERE p_id=" . $id . ";";


                $result = mysqli_query($conn, $query);
            }


            ?>


    <div>
        <form id="updateForm" name="updateForm" action="update.php" method="post">
            <label>Product name:</label><br>
                <input type="text" name="updateProductName"><br>
            <label>Product type</label><br>
                <select name="updateProductType">
                    <option value="Jackets/coats">Jackets/coats</option>
                    <option value="Accessories">Accessories</option>
                    <option value="Shirts">Shirts</option>
                    <option value="Jeans">Jeans</option>
                    <option value="Trousers">Trousers</option>
                    <option value="Shoes">Shoes</option>
                    <option value="Suits">Suits</option>
                </select>
            <p>Product description:</p>
                <textarea name="updateProductDesc" rows="10" cols="30"></textarea><br>
            <label>Product price:</label><br>
                <input type="text" name="updateProductPrice"><br>
            <label>Stock level:</label><br>
                <input type="text" name="updateProductStock"><br>
                <input type="submit" name="updateSubmit" value="Submit">
        </form>
    </div>

    <?php





     ?>

I have used this link from another page which contains the ID.

<a href="update.php?mode=update&id=<?php echo $productDetails['p_id']; ?>"
                       title="Update <?php echo $productDetails['p_name']; ?>">Update</a>
JB2000
  • 7
  • 5
  • 4
    @JB2000-$_GET['id']?there is no $_GET value in your form – Shanu k k Dec 08 '16 at 06:25
  • U have to pass id as hidden value or u pass id with url – Shanu k k Dec 08 '16 at 06:27
  • I have passed the id from another page. On this page I put that ID into new variable $id and then echo to check it worked. – JB2000 Dec 08 '16 at 06:30
  • `error_reporting(E_ALL);` would very likely tell you "*Undefined index 'id'...*" from the `$_GET['id']`. When you submit the form, it only sends data to `action="update.php"`, so the GET parameters you had before will be removed. You'll need something like `action="update.php?id=7"` – Qirel Dec 08 '16 at 06:32
  • 1
    Can you please echo your $query?and check it directly with database – Shanu k k Dec 08 '16 at 06:33
  • UPDATE products SET p_name = 'TEST' WHERE p_id=; – JB2000 Dec 08 '16 at 06:42
  • @JB2000-echo your $query and show us i din't get you logic.are you using POST value from form and id from another page? – Shanu k k Dec 08 '16 at 06:42
  • @JB2000 so u didn't get you id right? – Shanu k k Dec 08 '16 at 06:43
  • Yes I am using GET to get ID from another page, the form isn't in use yet I just want the query to work first. – JB2000 Dec 08 '16 at 06:46

2 Answers2

2

You didn't get id because there is no id in your url

<form id="updateForm" name="updateForm" action="update.php?id=<?php echo $id; ?>" method="post">

or pass id as hidden like

<input type="hidden" name="id" id="your_id"/>
Shanu k k
  • 1,235
  • 2
  • 18
  • 43
0

Change your query to

$query = "UPDATE products SET p_name = 'TEST' WHERE p_id=". $id;
Md. Sahadat Hossain
  • 3,210
  • 4
  • 32
  • 55