Jump to content

noob needs help: updating page info via db based on a form selection


sigkill-9

Recommended Posts

First, I'm fairly new to PHP so excuse my noobness lol.

 

I have a MySQL database that contains a list of home movies with fields such as id, title, description, rating, etc.. I'm trying to build a page that displays movie details (fetched from the db) based on a selection from a drop down box.

 

I can connect to the database and pull the info just fine, but I'm having allot of trouble getting the page to update and show the information for the movie selected in the drop down box.  In fact, I really dont know where to begin...

 

Heres the code I have so far. It's a half-baked code I got from a friend (it was code for a simple blog) and am trying to modify it to work for my needs. Dont know, but definitely need some help.

 

<?php

include 'dbconnect.php';

class blog extends db {
    function get_first_row_id() {
	try {
		if(!self::$dbh) $this->connect();
		//code here
		$sql = "SELECT * FROM tblMovies LIMIT 1";
		$sth = self::$dbh->prepare($sql);
		$sth->bindParam(':id', $id, PDO::PARAM_INT);
		$sth->execute();
		$data = $sth->fetch(PDO::FETCH_ASSOC);
		//end here
	}
	catch (PDOException $e) {
		$this->fatal_error($e->getMessage());
	}
	return $data['id'];
}  

function get_row($id) {
	try {
		if(!self::$dbh) $this->connect();
		$sql = "SELECT * FROM tblMovies WHERE id=:id LIMIT 1";
		$sth = self::$dbh->prepare($sql);
		$sth->bindParam(':id', $id, PDO::PARAM_INT);
		$sth->execute();
		$data = $sth->fetch(PDO::FETCH_ASSOC);
	}
	catch (PDOException $e) {
		$this->fatal_error($e->getMessage());
	}
	return $data;
}

//i dont  understand what the variable $where=1 means or what its used for
function get_rows($where=1) {
	try {
		if(!self::$dbh) $this->connect();
		$sql = "SELECT * FROM tblMovies WHERE :where";
		$sth = self::$dbh->prepare($sql);
		$sth->bindParam(':where', $where, PDO::PARAM_INT);
		$sth->execute();
		$data = $sth->fetchAll(PDO::FETCH_ASSOC);
	}
	catch (PDOException $e) {
		$this->fatal_error($e->getMessage());
	}
	return $data;
}  
}

$x = new blog;

//default
$id=$x->get_first_row_id();

//Check
if(isset($_GET['id'])){
$id=$_GET['id'];
}	

$blog_array = $x->get_row($id, $title, $media, $rating, $genre, $description);
$title_array = $x->get_rows();

echo '<div class="container">';
echo '<div>Select a Movie:</div>';
echo '<form method="post" action=?>';
//added hidden inputs in an attempt to get the page to show the values, but doenst work...
echo '<input type="hidden" name="id" value="' . $id . '" >';
echo '<input type="hidden" name="title" value="' . $title . '" >';
echo '<input type="hidden" name="media" value="' . $media . '" >';
echo '<input type="hidden" name="rating" value="' . $rating . '" >';
echo '<input type="hidden" name="genre" value="' . $genre . '" >';
echo '<input type="hidden" name="description" value="' . $description . '" >';
echo '<select class="input" name="id">';
//when user selects a movie, need the page to show info about the selected movie, but how?
//not sure how to get the below variables to be passed to the rest of the page...

foreach($title_array as $value) {
$id = $value['id'];
$title = $value['title'];
$media = $value['media'];
$rating = $value['rating'];
$genre = $value['genre'];
$description = $value['description'];

echo '<option value="' . $id . '">' . $title . '</option>\n';

}
echo '</select><input type="submit" value="Submit"></form></div>';
echo '</div>';
echo '<div class="info">';
echo '<br /><br /><br />';
echo '<div><b><u>Movie Details:</u><b>';
echo '<div><b>ID: </b>' . $_POST['id'] . '<hr /></div>';//not working, why?
echo '<div><b>Title: </b>' . $_POST['title'] . '<hr /></div>';//not working, why?
echo '<div><b>Media: </b>' . $media . '<hr /></div>';//not working, why?
echo '<div><b>Rating: </b>' . $rating . '<hr /></div>';//not working, why?
echo '<div><b>Genre: </b>' . $genre . '<hr /></div>';//not working, why?
echo '<div><b>Description: </b>' . $description . '</div>';//not working, why?
echo '</div>';
echo '</div>';	
?>

 

Also, heres a sample of the movie database array just so you get an idea of what I'm working with:

    [0] => Array
        (
            [id] => 1
            [title] => A.I. Artificial Intelligence
            [media] => DVD
            [rating] => PG13
            [genre] => Action
            [description] => Sometime in the distant future, after the polar icecaps have melted, major flooding has devastated most major cities.....
        )

    [1] => Array
        (
            [id] => 2
            [title] => Shawshank Redemption
            [media] => DVD
            [rating] => R
            [genre] => Drama
            [description] => Andy Dufresne, a mild mannered New England banker, is convicted of murdering his wife and her lover......
        )

    [2] => Array
        (
            [id] => 3
            [title] => Pulp Fiction
            [media] => VHS
            [rating] => R
            [genre] => Crime & Gangster
            [description] => An inside look at a memorable community of criminals. Prizefighter Butch Coolidge has decided to stop payment on a deal he's made with the devil.....
        )

 

 

Link to comment
Share on other sites

Wow...that seems awfully complicated for simple sql queries.

 

It's probably best to write your own script, that way you won't have half of this stuff that you probably don't even need...

 

Could you not just use something like this..?

 

<?php
mysql_connect('server', 'admin', 'pass') or die(mysql_error());
mysql_select_db('database') or die (mysql_error());

$title = $_POST['title'];
$sql = mysql_query("SELECT FROM insert_table_here WHERE title='$title'") or die(mysql_error());
$fetch = mysql_fetch_array($sql);
echo $fetch['title'];
?>

Link to comment
Share on other sites

even in a drop down menu, you have two fields

 

value and content

 

you set the value of the <option> element to the ID of the title you set as the innerHTML of the option

 

for example

 

            [id] => 1

            [title] => A.I. Artificial Intelligence

 

<option value="1">A.I. Artificial Intelligence</option>

 

and just use the value of the $_POST or $_GET for the <select>'s name..

 

$_POST['select_box_name'];

would hold the ID of the selected movie.. then form your query off of that..

Link to comment
Share on other sites

you set the value of the <option> element to the ID of the title you set as the innerHTML of the option

I did that already with this code:

echo '<option value="' . $id . '">' . $title . '</option>\n';

 

and just use the value of the $_POST or $_GET for the <select>'s name..

 

$_POST['select_box_name'];

would hold the ID of the selected movie.. then form your query off of that..

 

And I did that rite here:

echo '<div><b>ID: </b>' . $_POST['id'] . '<hr /></div>';

 

This works for the ID field, it changes the ID printout based on what the user selects, but the ID is the only thing that is changed. The Title, Rating, Genre, Descriptiion, and Media printouts are not changed.

 

How would I base a query on echo $_POST['id'] and have it print out the movie details based on the ID selected?

Link to comment
Share on other sites

ok

 

since you've quoted me..

 

the second quote of me, you misunderstood

 

you're pushing the data received by the script, instead of pushing the data you retrieve from the database..

 

You're going to want to QUERY your DATABASE with the `id` for example..

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

SELECT * FROM `movieTable` WHERE `id` = '{$id}'

 

and then use the data returned by the query to fill the fields

Link to comment
Share on other sites

I figured I misunderstood something. I'm still trying to understand what you're staying though.

 

You said to query the database with id, but didnt I do that here:

	function get_row($id) {
	try {
		if(!self::$dbh) $this->connect();
		$sql = "SELECT * FROM tblMovies WHERE id=:id LIMIT 1";
		$sth = self::$dbh->prepare($sql);
		$sth->bindParam(':id', $id, PDO::PARAM_INT);
		$sth->execute();
		$data = $sth->fetch(PDO::FETCH_ASSOC);
	}
	catch (PDOException $e) {
		$this->fatal_error($e->getMessage());
	}
	return $data;
}

 

But, when I do an echo of $data, it doesnt list anything.

 

Then below in the code I did this:

echo '<div><b>ID: </b>' . $data['id'] . '<hr /></div>';

but the same, it doesnt list anything.

 

I'm lost in the code and really dont understand what I'm doing, but trying to learn.

Link to comment
Share on other sites

ok I've heard about PDO but haven't used it.. all I know is if $data holds the retrieved data, you should use $data everywhere like

 

$data['id']

$data['title']

$data['whatever else']

 

then if the query went thru, and retrieved data, than you could never get anything mixed up because $data should also have the correct data in relation to the id

Link to comment
Share on other sites

Somehow I figured it out. I think it was what you said about returning the data from the database query.

 

At first I had the same train of thought you did: "if $data holds the retrieved data, I should be able to use it". But I did that and it didnt list anything. I used a print_r on $data but it was empty... strange...

 

Then I looked at my get_rows function and realized that if $data was returning all the info it should work. Then I got even more confused as to why $data didnt hold any info when using print_r... Maybe its because the $data variable is inside a class??? I dont know... I'm not too familiar with classes or using them yet...

 

Then I saw this in my code:

$title_array = $x->get_rows();

and figured, "well, if the $id is updating as it should, I'll make a new variable to return all the rest of the info". And I did.

 

I made the new variable:

$movieDetails=$x->get_row($id);

and figured that it should print all the variables, so then I made re-wrote my output code as such:

$id = $_POST['id'];
echo '<br /><br />';
print '<div><b>ID :</b>' . $movieDetails['id'] . '<hr /></div>';
echo '<br />';
print '<div><b>Title :</b>' . $movieDetails['title'] . '<hr /></div>';
echo '<br />';
print '<div><b>Media :</b>' . $movieDetails['media'] . '<hr /></div>';
echo '<br />';
print '<div><b>Rating :</b>' . $movieDetails['rating'] . '<hr /></div>';
echo '<br />';
print '<div><b>Genre :</b>' . $movieDetails['genre'] . '<hr /></div>';
echo '<br />';
print '<div><b>Description :</b>' . $movieDetails['description'] . '<hr /></div>';

and it works! Now I have to study what happened and how I fixed it. Feels kinda strange to fix something, but not know how I fixed it... lol

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.