Selecting a Row then editing the content of two fields

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Greg19
Forum Newbie
Posts: 23
Joined: Sun Dec 07, 2008 12:47 pm

Selecting a Row then editing the content of two fields

Post by Greg19 »

hey everyone, I'm trying to select a row out of a mysql database and edit the content of two fields.
What I've tried to do (which doesn't appear to work): made a drop down menu, the user selects a question and then the script selects the answer in the same row, the question is then displayed in one textarea and the answer in the other, the user would then be able to edit and hit update to change the entree in the database. However the textarea where the answer is supposed to be is blank, and nothing happens when I hit update.

here is my php:

Code: Select all

<?php
session_start(); 
if(!isset($_SESSION['adminctrl'])){ 
    header('Location: admin.php'); die('<a href="admin.php">Login first!</a>');
   }
$access = mysql_connect("***********", "***", "***") or die(mysql_error());
mysql_select_db('Support', $access) or die(mysql_error());
 
$error = array();
if(isset($_POST['update'])) {
$question_new = ($_POST['update_q']);
$answer_new = ($_POST['update_a']);
$result = @mysql_query("SELECT question FROM `q_a` WHERE question = '$question'"); 
@mysql_query("UPDATE message SET question='$question_new' AND answer='$answer_new' WHERE question='$question'");
echo"Update was successful.";
}
?> 
 <form action="editfaq.php" method="post">
<?php 
         $conn = "SELECT question FROM `q_a`" ;
         $result = mysql_query($conn,$access) or die("Error: ". mysql_error(). " with query ". $conn);
         while($row=mysql_fetch_assoc($result))
          {
           $question[] = $row['question'];
          }
          
         echo "<select name='question'>\n";
         foreach($question as $y){
                 echo "<option value='$y'>\n" .$y."</option>\n";
                }
         echo "</select>\n";
?>
<input type="submit" name="submit" value="Update!" />
</form>
 
<?php
if (isset($_POST['question'])){
$question = ($_POST['question']);
$r = mysql_query("SELECT answer FROM `q_a` WHERE question = '$question'");
$A = mysql_fetch_assoc($r);
$answer = $A['answer'];
echo "<form method='post' action='editfaq.php'>";
echo "<textarea rows='8' name='update_q' cols='30'>" .$question. "</textarea>";
echo "<br/>";
echo "<textarea rows='8' name='update_a' cols='30'>" .$answer. "</textarea>";
echo "<br/>";
echo "<input type='submit' name='submit' value='Update!' />";
echo "</form>";
}
?>
 
Thanks for any help. -cheers
sujithtomy
Forum Commoner
Posts: 46
Joined: Tue Mar 24, 2009 4:43 am

Re: Selecting a Row then editing the content of two fields

Post by sujithtomy »

Hello,

Try this

Code: Select all

<?php
session_start();
if(!isset($_SESSION['adminctrl'])){
    header('Location: admin.php'); die('<a href="admin.php">Login first!</a>');
   }
$access = mysql_connect("***********", "***", "***") or die(mysql_error());
mysql_select_db('Support', $access) or die(mysql_error());
 
$error = array();
if(isset($_POST['update'])) {
$question_new = $_POST['update_q'];
$answer_new = $_POST['update_a'];
$question = $_POST['question'];
$result = @mysql_query("SELECT question FROM `q_a` WHERE question = '$question'");
@mysql_query("UPDATE message SET question='$question_new' AND answer='$answer_new' WHERE question='$question'");
echo"Update was successful.";
}
?>
 <form action="editfaq.php" method="post">
<?php
         $conn = "SELECT question FROM `q_a`" ;
         $result = mysql_query($conn,$access) or die("Error: ". mysql_error(). " with query ". $conn);
         while($row=mysql_fetch_assoc($result))
          {
           $question[] = $row['question'];
          }
         
         echo "<select name='question'>\n";
         foreach($question as $y){
                 echo "<option value='$y'>\n" .$y."</option>\n";
                }
         echo "</select>\n";
?>
<input type="submit" name="submit" value="Update!" />
</form>
 
<?php
if (isset($_POST['question'])){
$question = ($_POST['question']);
$r = mysql_query("SELECT answer FROM `q_a` WHERE question = '$question'");
$A = mysql_fetch_assoc($r);
$answer = $A['answer'];
echo "<form method='post' action='editfaq.php'>";
echo "<textarea rows='8' name='update_q' cols='30'>" .$question. "</textarea>";
echo "<br/>";
echo "<textarea rows='8' name='update_a' cols='30'>" .$answer. "</textarea>";
echo "<br/>";
echo "<input type='hidden' name='question' value='$question' />";
echo "<input type='submit' name='submit' value='Update!' />";
echo "</form>";
}
?>
Greg19
Forum Newbie
Posts: 23
Joined: Sun Dec 07, 2008 12:47 pm

Re: Selecting a Row then editing the content of two fields

Post by Greg19 »

thanks, however its still suffering from the same problem
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Selecting a Row then editing the content of two fields

Post by php_east »

line 15 doesn't look right. it should be

Code: Select all

@mysql_query("UPDATE message SET (question='$question_new',answer='$answer_new') WHERE question='$question'"); 
i think. the AND confuses mysql.
Greg19
Forum Newbie
Posts: 23
Joined: Sun Dec 07, 2008 12:47 pm

Re: Selecting a Row then editing the content of two fields

Post by Greg19 »

thanks for the replies, but still no dice.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Selecting a Row then editing the content of two fields

Post by php_east »

you can simulate the question by setting the post var to one of your questions in line 38.

Code: Select all

$_POST['question']='MY QUESTION';
see if that part displays your answer. if it doesn't you know where to look.

37 <?php
38 if (isset($_POST['question'])){
39 $question = ($_POST['question']);
Post Reply