0

I can't figure out why my code isn't working. I'm trying to insert a dynamic variable to a table in my database with regular input in a form.

Like this, here is my query code:

if (isset($_POST['submit'])) {
            // Check if movie allready is added.
            $stmt = $dbh->prepare("SELECT * FROM watchlist WHERE movie_title='{$_POST['$title']}'");
            $stmt->bindParam(':movie_title', $_POST[$movie->title]);
            $stmt->execute();

            $rows = $stmt->fetchALL();
            $n = count($rows);

            if($n > 0){
            echo 'Du är redan peppad på filmen!';
            }


            $sql = $dbh->prepare("INSERT INTO watchlist(movie_title, movie_des, movie_link) 
                                 VALUES ('{$_POST['title']}', '{$_POST['description']}', '{$_POST['link']}'"); 
            $sql->bindParam(':title', $_POST['title']);
            $sql->bindParam(':description', $_POST['description']);
            $sql->bindParam(':link', $_POST['link']);
            $sql->execute();
            header("Location: ../index.php");
            exit;
        }

And here is my form:

$title = $movie->title;
    $description = $movie->description;
    $link = $movie->link;
    echo '<div class="view">';
    echo '<h3>' . $title . '</h3>';
    echo  $description . '<br/>';
    echo '<a href="'. $link . '" target="_blank">L&auml;s mer...</a><br/><br/>';
    echo '<a href="index.php">Tillbaka</a>';
    echo '<div class="form_dis"></div>';
    echo '<div class="form_content">';
    echo '<form action="queries/insert.php" method="post">',
         '<input type="hidden" name="title" value="$title">',
         '<input type="hidden" name="description" value="$description">',
         '<input type="hidden" name="link" value="$link">',
         '<input type="submit" name="submit" value="Peppa!">',
         '</form></div>';

I get send back to my index page but nothing has been added to the database and I just can't figure out why. Is there anything wrong with my code that I'm missing?

15
  • Use this answer to get the PDO error: stackoverflow.com/questions/3999850/pdo-error-message Commented Feb 12, 2014 at 13:12
  • Your binds don't match your column names. I.e.: ':title' should be ':movie_title' etc. Commented Feb 12, 2014 at 13:15
  • @Fred-ii- I've tried that but still doesn't work. And @Hast I tried using the code print_r($sql->errorInfo()) after my execute and took the header away, and got no error message, and I got the PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING in my dbconnection. Commented Feb 12, 2014 at 13:16
  • Also try changing '{$_POST['title']}' to '{$_POST[title]}' etc. Commented Feb 12, 2014 at 13:20
  • @Fred-ii- I tried changing it but still no luck. Error message is working now at least so I'll try to debug it. Commented Feb 12, 2014 at 13:24

1 Answer 1

1

Give this a try:

if (isset($_POST['submit'])) {
    $sql_1 = "INSERT INTO watchlist (movie_title,
    movie_des,
    movie_link
    ) VALUES (
    :movie_title, 
    :movie_des, 
    :movie_link)";

    $sql = $dbh->prepare($sql_1);

    $sql->bindParam(':movie_title', $_POST['title']);
    $sql->bindParam(':movie_des', $_POST['description']);
    $sql->bindParam(':movie_link', $_POST['link']);
    $sql->execute(array(':movie_title' => $_POST['title'],':movie_des' => $_POST['description'],':movie_link' => $_POST['link']));

    if($sql != false) {
    echo "Success!";
    } else {
        echo "An error occured saving your data!";
    }

//  header("Location: ../index.php");
//  exit;
}
Sign up to request clarification or add additional context in comments.

11 Comments

This works, but the weird thing is that it inserts the actual variables in my database, and not the data the variable referring to. So instead of for example inserting "Batman the movie" to my database it's inserting $title. Guess this has nothing to do with the code I posted. It must be something else wrong with my application, bughunting it is...
Hm... I think it may have something to do with your hidden values <input type="hidden" name="title" value="$title"> etc. @Bondenn
Can you try changing $sql->bindParam(':movie_title', $_POST['title']); to $sql->bindParam(':movie_title', $_POST['title'], PDO::PARAM_STR); to see if it makes it kick in? @Bondenn and try removing the value="$title" also
I'll try this out and let you know!
Woho I fixed it! By changing the value="$title" to value="'.$title.'" :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.