Hi,

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;
if(isset($_POST['upd'])){

$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.

Recommended Answers

All 4 Replies

Hi

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.

Greetings
Steen

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.";
        }
            else{
                echo "<table>";
                    echo "<tr>
            <td>ID</td>
            <td>Title</td>
            <td>Folder</td>
            <td>Spot</td>
            <td>Time added</td>
            <td>Update</td>
            <td>Delete</td>
        </tr>";
                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>
        <?php

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)) {
    die($q->getMessage());
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.