Page 1 of 1

Selecting a Row then editing the content of two fields

Posted: Fri Mar 27, 2009 3:11 am
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

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

Posted: Fri Mar 27, 2009 5:05 am
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>";
}
?>

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

Posted: Sun Mar 29, 2009 3:14 am
by Greg19
thanks, however its still suffering from the same problem

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

Posted: Sun Mar 29, 2009 4:15 am
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.

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

Posted: Mon Mar 30, 2009 12:06 am
by Greg19
thanks for the replies, but still no dice.

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

Posted: Mon Mar 30, 2009 4:20 am
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']);