Jump to content

Recommended Posts

hey,

 

I'm developing a HTML form that will send the data to my db and one of the fields is a link to the details of the selected film. I have the following as the default value in the form.

 

http://www.mydomain.com/mymovies/details.php?id=

 

what I need help with, is to populate the above with the filmID of the record that is being submitted, so the form will be pre-populated with something like

 

http://www.phatjoints.com/mymovies/details.php?id=2

(assuming the last record in the db is 1)

 

thanks

Well without any code I am just going on a hunch that this is what you want

 

<?php

$newLink = '<a href="
http://www.phatjoints.com/mymovies/details.php?id=' . $_POST['id'] . '">Movie Details</a>';

echo $newLink;
?>

 

If not please post some code or be much more specific.

ah yeah the code would help.

 

it's just HTML atm and sends the data to the update.php form that does the rest.

 

<html>
<head>
<title>update database</title>
</head>
<body>
<form method="post" action="update.php">
Title: <input type="text" name="title" size="70" /><br />
Genre: <select name="genre">
  <option value="BLANK"> </option>
  <option value="Action">Action</option>
  <option value="Adventure">Adventure</option>
  <option value="Animation">Animation</option>
  <option value="Biography">Biography</option>
  <option value="Comedy">Comedy</option>
  <option value="Crime">Crime</option>
  <option value="Documentary">Documentary</option>
  <option value="Drama">Drama</option>
  <option value="Fantasy">Fantasy</option>
  <option value="Horror">Horror</option>
  <option value="Martial Arts">Martial Arts</option>
  <option value="Musical">Musical</option>
  <option value="Mystery">Mystery</option>
  <option value="Romance">Romance</option>
  <option value="Sci-Fi">Sci-Fi</option>
  <option value="Thriller">Thriller</option>
  <option value="Western">Western</option>
  <option value="War">War</option>
</select><br />
Year: <input type="text" name="year" size="20" /><br />
Stars: <select name="stars">
  <option value="0"> </option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
</select><br />
Country: <input type="text" name="country" size="30" /><br />
Link: <input type="text" name="link" size="60" value="http://www.mydomain.com/mymovies/details.php?id=" /><br />
Director: <input type="text" name="director" size="40" /><br />
Plot:<br /> <textarea name="plot" cols="50" rows="12"></textarea><br />
Image: <input type="text" name="image" size="30" value="covers/new" /><br />
Trailer: <input type="text" name="trailer" size="30" value="trailers/new" /><br />
Rating: <input type="text" name="rating" size="5" /><br />
<input type="submit" value="Update Database" /><br />
</body>
</html>

 

 

So what you will need is something like this:

 

<?php
// initiate DB connection

// pull the id for the last film entered into the movies table 
$lastFilm = mysql_fetch_assoc(mysql_query("SELECT id FROM movies ORDER BY id DESC LIMIT 1"));
$lastFilmID = $lastFilm['id'];

?>
<html>
<head>
<title>update database</title>
</head>
<body>
<form method="post" action="update.php">
Title: <input type="text" name="title" size="70" /><br />
Genre: <select name="genre">
  <option value="BLANK"> </option>
  <option value="Action">Action</option>
  <option value="Adventure">Adventure</option>
  <option value="Animation">Animation</option>
  <option value="Biography">Biography</option>
  <option value="Comedy">Comedy</option>
  <option value="Crime">Crime</option>
  <option value="Documentary">Documentary</option>
  <option value="Drama">Drama</option>
  <option value="Fantasy">Fantasy</option>
  <option value="Horror">Horror</option>
  <option value="Martial Arts">Martial Arts</option>
  <option value="Musical">Musical</option>
  <option value="Mystery">Mystery</option>
  <option value="Romance">Romance</option>
  <option value="Sci-Fi">Sci-Fi</option>
  <option value="Thriller">Thriller</option>
  <option value="Western">Western</option>
  <option value="War">War</option>
</select><br />
Year: <input type="text" name="year" size="20" /><br />
Stars: <select name="stars">
  <option value="0"> </option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
</select><br />
Country: <input type="text" name="country" size="30" /><br />
Link: <input type="text" name="link" size="60" value="http://www.mydomain.com/mymovies/details.php?id=<?php echo $lastFilmID; ?>" /><br />
Director: <input type="text" name="director" size="40" /><br />
Plot:<br /> <textarea name="plot" cols="50" rows="12"></textarea><br />
Image: <input type="text" name="image" size="30" value="covers/new" /><br />
Trailer: <input type="text" name="trailer" size="30" value="trailers/new" /><br />
Rating: <input type="text" name="rating" size="5" /><br />
<input type="submit" value="Update Database" /><br />
</body>
</html>

 

You will have to convert the above page to .php for this to work and have a mysql_connection active.

So what you will need is something like this:

 

<?php
// initiate DB connection

// pull the id for the last film entered into the movies table 
$lastFilm = mysql_fetch_assoc(mysql_query("SELECT id FROM movies ORDER BY id DESC LIMIT 1"));
$lastFilmID = $lastFilm['id'];

?>
<html>
<head>
<title>update database</title>
</head>
<body>
<form method="post" action="update.php">
Title: <input type="text" name="title" size="70" /><br />
Genre: <select name="genre">
  <option value="BLANK"> </option>
  <option value="Action">Action</option>
  <option value="Adventure">Adventure</option>
  <option value="Animation">Animation</option>
  <option value="Biography">Biography</option>
  <option value="Comedy">Comedy</option>
  <option value="Crime">Crime</option>
  <option value="Documentary">Documentary</option>
  <option value="Drama">Drama</option>
  <option value="Fantasy">Fantasy</option>
  <option value="Horror">Horror</option>
  <option value="Martial Arts">Martial Arts</option>
  <option value="Musical">Musical</option>
  <option value="Mystery">Mystery</option>
  <option value="Romance">Romance</option>
  <option value="Sci-Fi">Sci-Fi</option>
  <option value="Thriller">Thriller</option>
  <option value="Western">Western</option>
  <option value="War">War</option>
</select><br />
Year: <input type="text" name="year" size="20" /><br />
Stars: <select name="stars">
  <option value="0"> </option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
</select><br />
Country: <input type="text" name="country" size="30" /><br />
Link: <input type="text" name="link" size="60" value="http://www.mydomain.com/mymovies/details.php?id=<?php echo $lastFilmID; ?>" /><br />
Director: <input type="text" name="director" size="40" /><br />
Plot:<br /> <textarea name="plot" cols="50" rows="12"></textarea><br />
Image: <input type="text" name="image" size="30" value="covers/new" /><br />
Trailer: <input type="text" name="trailer" size="30" value="trailers/new" /><br />
Rating: <input type="text" name="rating" size="5" /><br />
<input type="submit" value="Update Database" /><br />
</body>
</html>

 

You will have to convert the above page to .php for this to work and have a mysql_connection active.

 

thnaks premiso I'll give it a go

I'm having difficulties getting it to work.

 

This is what I have now

 

<?php
// initiate DB connection
mysql_connect ("localhost", "USERNAME", "PASSWORD") or die 

('error: ' .mysql_error());
mysql_select_db ("phatjoin_mymovies");

// pull the id for the last film entered into the movies table 
$lastFilm = mysql_fetch_assoc(mysql_query("SELECT filmID FROM mymovies ORDER BY id DESC LIMIT 1"));
$lastFilmID = $lastFilm['id'];

?>
<html>
<head>
<title>update database</title>
</head>
<body>
<form method="post" action="update.php">
Title: <input type="text" name="title" size="70" /><br />
Genre: <select name="genre">
  <option value="BLANK"> </option>
  <option value="Action">Action</option>
  <option value="Adventure">Adventure</option>
  <option value="Animation">Animation</option>
  <option value="Biography">Biography</option>
  <option value="Comedy">Comedy</option>
  <option value="Crime">Crime</option>
  <option value="Documentary">Documentary</option>
  <option value="Drama">Drama</option>
  <option value="Fantasy">Fantasy</option>
  <option value="Horror">Horror</option>
  <option value="Martial Arts">Martial Arts</option>
  <option value="Musical">Musical</option>
  <option value="Mystery">Mystery</option>
  <option value="Romance">Romance</option>
  <option value="Sci-Fi">Sci-Fi</option>
  <option value="Thriller">Thriller</option>
  <option value="Western">Western</option>
  <option value="War">War</option>
</select><br />
Year: <input type="text" name="year" size="20" /><br />
Stars: <select name="stars">
  <option value="0"> </option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
</select><br />
Country: <input type="text" name="country" size="30" /><br />
Link: <input type="text" name="link" size="60" value="http://www.mydomain.com/mymovies/details.php?id=<?php echo $lastFilmID; ?>" /><br />
Director: <input type="text" name="director" size="40" /><br />
Plot:<br /> <textarea name="plot" cols="50" rows="12"></textarea><br />
Image: <input type="text" name="image" size="30" value="covers/new" /><br />
Trailer: <input type="text" name="trailer" size="30" value="trailers/new" /><br />
Rating: <input type="text" name="rating" size="5" /><br />
<input type="submit" value="Update Database" /><br />
</body>
</html>

 

This is the update.php

 

<?php
$title = $_POST['title'];
$genre = $_POST['genre'];
$year = $_POST['year'];
$stars = $_POST['stars'];
$country = $_POST['country'];
$link = $_POST['link'];
$director = $_POST['director'];
$plot = $_POST['plot'];
$image = $_POST['image'];
$trailer = $_POST['trailer'];
$rating = $_POST['rating'];

mysql_connect ("localhost", "USERNAME", "PASSWORD") or die 

('error: ' .mysql_error());
mysql_select_db ("phatjoin_mymovies");

$query="INSERT INTO mymovies (title, genre, year, stars, country, link, director, plot, image, trailer, rating) VALUES ('".$title."', '".$genre."', '".$year."', '".$stars."', '".$country."', '".$link."', '".$director."', '".$plot."', '".$image."', '".$trailer."', '".$rating."')";

mysql_query($query) or die ('Error updating database');

echo "" ;

?>
<html>
<head>
<title>Updated Database</title>
</head>
<body>
Database Updated<br />
<a href="http://www.mydomain.com/mymovies/form.php">Enter New Movie Information</a></body>
</html>

 

I dont understand what you mean by "you may want to add 1 to the ID pulled from the DB so it is a new ID" ?

 

thanks

<?php
// initiate DB connection
mysql_connect ("localhost", "USERNAME", "PASSWORD") or die ('error: ' .mysql_error());
mysql_select_db ("phatjoin_mymovies");

<?php
$title = $_POST['title'];
$genre = $_POST['genre'];
$year = $_POST['year'];
$stars = $_POST['stars'];
$country = $_POST['country'];
$link = $_POST['link'];
$director = $_POST['director'];
$plot = $_POST['plot'];
$image = $_POST['image'];
$trailer = $_POST['trailer'];
$rating = $_POST['rating'];

mysql_connect ("localhost", "USERNAME", "PASSWORD") or die ('error: ' .mysql_error());
mysql_select_db ("phatjoin_mymovies");

// pull the id for the last film entered into the movies table 
$lastFilm = mysql_fetch_assoc(mysql_query("SELECT filmID FROM mymovies ORDER BY id DESC LIMIT 1"));
$link .= ($lastFilm['id'] + 1); // appened the last id plus one to the link before inserting...


$query="INSERT INTO mymovies (title, genre, year, stars, country, link, director, plot, image, trailer, rating) VALUES ('".$title."', '".$genre."', '".$year."', '".$stars."', '".$country."', '".$link."', '".$director."', '".$plot."', '".$image."', '".$trailer."', '".$rating."')";

mysql_query($query) or die ('Error updating database');

echo "" ;

?>
<html>
<head>
<title>Updated Database</title>
</head>
<body>
Database Updated<br />
<a href="http://www.mydomain.com/mymovies/form.php">Enter New Movie Information</a></body>
</html>
?>

 

I would actually put the linkid portion on the page where the data gets entered into the DB that way you know that the lastID+1 will be a legitimate one and will not be a duplicate.

 

Adding one to the ID would make it a new ID, since the lastID would already have been taken, adding one would ensure that it is a new ID.

 

Hope this helps.

I think I'm getting more confused

 

I get what you mean about adding 1 to the id but I can't see how it fits together.

 

The last code you posted put all the code into the update.php page so are you saying that I don't need any php in the form.php page and that the code you've done will increment the link with the next id?

 

i.e. all I enter into the form is http://www.mydomain.com/mymovies/details.php?id= and the code will do the same?

 

sorry I am new to php ;)

Yep that is correct, if you wanted to you could just put that link on the update.php page in a string and do not worry about having in the form itself.

 

IE:

 

<?php
// initiate DB connection
mysql_connect ("localhost", "USERNAME", "PASSWORD") or die ('error: ' .mysql_error());
mysql_select_db ("phatjoin_mymovies");

$title = $_POST['title'];
$genre = $_POST['genre'];
$year = $_POST['year'];
$stars = $_POST['stars'];
$country = $_POST['country'];
//$link = $_POST['link'];
$director = $_POST['director'];
$plot = $_POST['plot'];
$image = $_POST['image'];
$trailer = $_POST['trailer'];
$rating = $_POST['rating'];

mysql_connect ("localhost", "USERNAME", "PASSWORD") or die ('error: ' .mysql_error());
mysql_select_db ("phatjoin_mymovies");

// pull the id for the last film entered into the movies table 
$lastFilm = mysql_fetch_assoc(mysql_query("SELECT filmID FROM mymovies ORDER BY id DESC LIMIT 1"));
$link = "http://www.mydomain.com/mymovies/details.php?id=" . ($lastFilm['id'] + 1); // append the last id plus one to the link before inserting...


$query="INSERT INTO mymovies (title, genre, year, stars, country, link, director, plot, image, trailer, rating) VALUES ('".$title."', '".$genre."', '".$year."', '".$stars."', '".$country."', '".$link."', '".$director."', '".$plot."', '".$image."', '".$trailer."', '".$rating."')";

mysql_query($query) or die ('Error updating database');

echo "" ;

?>
<html>
<head>
<title>Updated Database</title>
</head>
<body>
Database Updated<br />
<a href="http://www.mydomain.com/mymovies/form.php">Enter New Movie Information</a></body>
</html>

thanks, I went ahead and used the code you gave me and tested it and for some reason when the record was entered it only had a value of '1' so for some reason it didn't pull the last record ID and increment it by 1?

 

is it to do with the naming?

 

$lastFilm = mysql_fetch_assoc(mysql_query("SELECT filmID FROM mymovies ORDER BY id DESC LIMIT 1"));

 

In the above what does $lastFilm relate to? all the other I understand and is correct. And the below again the $lastFilm part, should this relate to the filmID field in the database?

 

$link .= ($lastFilm['id'] + 1);

 

thanks

The lastfilm is just the variable name I chose to hold the data array.

 

change this

 

$link .= ($lastFilm['id'] + 1);

to

$link = "http://www.mydomain.com/mymovies/details.php?id=" . ($lastFilm['filmID'] + 1);

tried that but it didn't seem to help?

 

for now I'm going to leave the link info in the form, just until I can get a better idea of how it's all working.

 

it just doesn't seem to be pulling the id or if it is it's not using it?

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.