0

I want to be able to update my database in order to change some dynamic content on my site which contains movie reviews.

First Step I need to be able to select which movie review I want to update by selecting it from a dropdown menu which is dynamically populated by the database.

The second step is to populate a set of forms which related to the movie that was selected from the dropdown box so the information can be edited.

The third step is to then run an update query to alter the databse.

This is the code I have written to populate the dropdown menu with the movie titles stored in the database which is working.

<form action="" method="POST">
<select name="title">
<option value="">Select Movie Title</option>             
<?php 
$sql="SELECT * FROM review ORDER BY title ASC";
    $result=mysql_query($sql);
while ($row=mysql_fetch_assoc($result)) {

echo "<option value=" . $row['title'] . ">" . $row['title'] . "</option>";
}
echo"</select>";
?> 
<input type="submit" name="select" value="Select">

I am having trouble in figuring out how to populate the forms which will display the inforamtion related to the dropdown selection.

<?php 
 if(isset($_POST['select'])){
     $query = mysql_query("SELECT * FROM REVIEW WHERE title = '$title'");
     while ($fetch=mysql_fetch_assoc($query)){
 ?>  
<p>Title:<input type="text" size="35" name="title" value="<?php echo $_POST['title']; ?>"></p>
    <p>Source:<input type="text" size="35" name="source" value="<?php echo $_POST['source']; ?>"></p>
    <p>Rating:<input type="text" size="5" name="rating" value="<?php echo $_POST['rating']; ?>"></p>
    Review:<br>
    <textarea name="review" cols="40" rows="7">
    <?php echo $_POST['review']; ?>
3
Contributors
2
Replies
9
Views
5 Years
Discussion Span
Last Post by veedeoo
0

which data are taken from table in while loop? u don't use from '$fetch'.
and you need only to have review field (every movie title there is only one time ). therefore while loop not necessary.
u did not explain source and rating. but i think this will help u:

<?php 
 if(isset($_POST['select'])){
    $title = $_POST['title'];
    $query = mysql_query("SELECT * FROM REVIEW WHERE title = '$title'");
    $review = @mysql_result($query,0);
 ?>  
<p>Title:<input type="text" size="35" name="title" value="<?php echo $title; ?>"></p>
    <p>Source:<input type="text" size="35" name="source" value="<?php echo $_POST['source']; ?>"></p>
    <p>Rating:<input type="text" size="5" name="rating" value="<?php echo $_POST['rating']; ?>"></p>
    Review:<br>
    <textarea name="review" cols="40" rows="7">
    <?php echo $review; ?>
0

Hi,

Did you try adding a name atribute in your <option>? Like this..

   echo '<option name= "title" value=" '. $row['title'] .' ">" '. $row['title'] .' "</option>';   

Your query can be modified to something like this..

$query = mysql_query("SELECT * FROM REVIEW WHERE title = '". $_POST['title'] ."'");

!WARNING! Be careful when adding form inputted data in your database, make sure to sanitize these data.. otherwise your site will be highly vulnerable.

Don't use fetch as your variable name.. this variable name can and may have a naming colision issues later on. There are many PHP reserved words that we should avoid. Use $row instead. I know the $fetch is not an actual reserved words, but FETCH does.. it is safer to just stay away from them, rather than getting at them at close range.. I hope you understand what I am trying to say...

Like ..

while ($row=mysql_fetch_assoc($query)){

Don't use this

<p>Title:<input type="text" size="35" name="title" value="<?php echo $_POST['title']; ?>"></p>

and use this instead..

<p>Title:<input type="text" size="35" name="title" value="<?php echo $row['title']; ?>"></p>

Do the changes for the rest of your codes.. and if you want to update your database base on the inputted values from the form, then you must add another form above your query..

something like this...

if(isset($_POST['select'])){
 echo '<form name="updateFor" action="post" method="update.php">';

 ## put the rest of your codes here..



 } // this must be the end of your while loop

 ## add the submit form input

 echo '<input type="submit" name="update" value="update">';
 echo '</form>';

Now on the update.php, you can do something like this...

if(isset($_POST['update'])){

## prepare your update query..
## !IMPORTANT! make sure to santize everything before adding it to your database.

$update_query = ("UPDATE REVIEW SET title = '". $_POST['title'] ."', source = '". $_POST['source'] ."', rating='". $_POST['rating'] ."', review = '". $_POST['review'] ."'  WHERE title = ". $_POST['title']) ;

Once again, MAKE SURE to sanitize all the data coming from the form, before allowing it to be posted on your database...

Edited by veedeoo: more info. added

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.