We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,318 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

PHP MySQL SELECT & Update

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
2 Hours
Discussion Span
9 Months Ago
Last Updated
3
Views
MattD00
Newbie Poster
6 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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; ?>
hr.Ziggurat
Newbie Poster
21 posts since Aug 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 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...

veedeoo
Master Poster
764 posts since Oct 2011
Reputation Points: 298
Solved Threads: 133
Skill Endorsements: 13

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0591 seconds using 2.7MB