I'm new to php and i'm trying to build simple database and I having a problem with getting id value with $_GET here is snippet of my code: 

$id = (int)$_GET['id'];
echo $id;

$sql = "UPDATE dvds SET title = ?, folder = ?, spot = ? WHERE id = ?";
$q= $db->prepare($sql);
$q->execute(array($_POST['title'],$_POST['folder'],$_POST['spot'], $id));

after last echo i am getting value that i want
But when i want to pass it to my query variable $id is empty I dont know what i am missing here

Thanks in advance.

5 Years
Discussion Span
Last Post by broj1


The good (or bad...) news is that your code works like a charm. I've created a table and tested it on a mysql database - no problem.

You may have an access problem, when connecting to the database. Try checking the line where you create the $db variable. Make sure you really get a connection to the database. The code should look like this:

$db = new pdo("mysql:host=localhost;dbname=test","DbUser","DbPassword"); var_dump($db); exit;

The output should be something like "object(PDO)#1 (0) { }" if the connection is created.

This is tested against an mysql database. Other bases may different output.



Also where do you get the $_GET['id'] from? Maybe you should test for it whether it contains a valid value like this:

if(isset($_GET['id']) and is_numeric($_GET['id'])) {
    $id = (int)$_GET['id'];
} else {
    die('Error, no ID passed over.');

I'm taking ID from select and passing though the link here is the code

if (isset($_POST['search'])) {
    $result = $db->select_query("SELECT id, title, folder, spot, time FROM dvds WHERE title LIKE '%$searchh%'");
    // This will print column 'somecolumn' from table 'blog_posts': rows 17-24
        if ($result['1'] == 'false'){ 
            echo "Incorrect select query. Check your syntax.";
                echo "<table>";
                    echo "<tr>
            <td>Time added</td>
                foreach ($result as $rows)
                echo "<tr>";
            echo "<td>".$rows['id']."</td>";
                echo "<td>".$rows['title']."</td>";
                echo "<td>".$rows['folder']."</td>";
                echo "<td>".$rows['spot']."</td>";
                echo "<td>".$rows['time']."</td>";
            <td><a href="update.php?id=<?php echo $rows['id']; ?>">update</a></td>
            <td><a href="delete.php?id=<?php echo $rows['id']; ?>">delete</a></td>

Well, if everything is OK including echo $id; (line 6 in your first post displays correct ID) then something must be wrong with connection / query preparing. Have you tried what Steen proposed? if you use PDO I am not much faniliar with it. I use MDB2 from PEAR framework for stuff like this. In case I have to check the prepared querie I use

if (PEAR::isError($q)) {

or just print_r($mdb2); if $mdb2 is the MDB2 object.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.