0

I am trying to make a CMS for FAQs.

I have a page that loads the questions and answers into text-areas with unique ideas and the plan is too edit these text areas and then insert them in the database.

I would like to be able to add new FAQS to the database as well.

Right now I am trying to accomplish this with a ON DUPLICATE KEY UPDATE.. but it does not work properly.

index.php

    <form id='faqadd' action='faqsql.php' method='POST'>                      

                                        <?PHP
    include 'include.php';



$query = 'SELECT * FROM FAQ';
      $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
      $val = 1;
    while($row=mysql_fetch_array($result)){



        echo '<div class="faq['.$val.']"><label> Question </label><input type="hidden" name="id['.$val.']"></input><textarea rows="8" cols="50" id="q['.$val.']" >'.$row["question"].'</textarea><br />
       <label> Answer </label><textarea rows="4" cols="50" name="a['.$val.']"  >'.$row["answer"].'</textarea></div><br />';

        $val++; 
         }
         echo '<h3 style="color:white;">New FAQ </h3>';
  echo '<div class="faq['.$val.']"><label> Question </label><textarea rows="8" cols="50" id="q['.$val.']" ></textarea><br />
       <label> Answer </label><textarea rows="4" cols="50" name="a['.$val.']"  ></textarea></div><br />';


    ?>
        <input type='submit' name='ADD' value='ADD' id='ADD'>
                </form>

faqsql.php

foreach ($_POST['id'] as $id) {
    if ($id) {
        $query = 'INSERT INTO FAQ ("id", "question", "answer") VALUES ("' . $id . '","' . $_POST["q"] . '", "' . $_POST["a"] . '")
ON DUPLICATE KEY UPDATE "question" = "' . $_POST["q"] . '", "' . $_POST["a"] . '"';
        $result = mysql_query($query) or die("Error in query: $query. " . mysql_error());
        echo $id . " has been added </br > ";
    }
}
echo '<a href="editfaq.php">Back FAQ Manager </a>';

What should I try next?

Updated Code. Still not working.

matture
  • 297
  • 5
  • 17
  • What is the main problem? Do you get any error? – Kevin Jun 12 '13 at 02:18
  • Im a little lost on posting arrays. $_POST['faq'] is empty – matture Jun 12 '13 at 02:19
  • 2
    Looks like the form wasn't submitted. You've an extra `single quotation` in the first line inside the `
    ` tag.
    – Kevin Jun 12 '13 at 02:24
  • `faq` is a `
    ` class so it is not posted as a form element.
    – Sean Jun 12 '13 at 02:25
  • 1
    you need to assign a name to the textarea controls ... think the `q[' . $val . ']` is what you mean. – Orangepill Jun 12 '13 at 02:26
  • So i updated the code. The extra quote wasnt the problem and I added a hidden input with the name id. Yet it still doesnt work. Orangepill im not quite following what you mean. Any idea's? Thanks! – matture Jun 12 '13 at 02:34
  • Your form elements are added to the $_POST array only if you provide a name attribute for them, example: – danielperaza Jun 12 '13 at 02:36
  • so this doesnt work? name="a['.$val.']" – matture Jun 12 '13 at 02:38
  • Also, ensure the data you receive in $_POST is properly sanitized. Otherwise, you may fall into SQL Injection, XSS and other security issues. – danielperaza Jun 12 '13 at 02:38
  • Im going to work on the security after I can figure out how to actually make it work haha – matture Jun 12 '13 at 02:39
  • Please don't use `mysql_*` functions in new code. There's good reasons why you should look into using mysqli or PDO instead. See more details here: http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – jcsanyi Jun 12 '13 at 03:22

3 Answers3

0

It looks like you have some problems in your markup.

I think the output in your while-loop should look more like this:

echo '
    <div>
        <label>Question</label>
        <textarea name="faq['. $val .'][q]" rows="8" cols="50"></textarea>
        <br />
        <label>Answer</label>
        <textarea name="faq['. $val .'][a]" rows="4" cols="50"></textarea>
    </div>
';

Then in faqsql.php, your code will look more like this:

foreach ($_POST['faq'] as $id=>$faq) {
    $id = mysqli_real_escape_string($id);
    $question = mysqli_real_escape_string($faq['q']);
    $answer = mysqli_real_escape_string($faq['a']);

    $query = "
        INSERT INTO FAQ (id, question, answer) VALUES ('$id', '$question', '$answer')
        ON DUPLICATE KEY UPDATE question = '$question', answer = '$answer'
    ";
// $link is your DB connection handle, returned by mysqli_connect()
    $result = mysqli_query($link, $query) or die("Error in query: $query. " . mysqli_error($link));
    echo "$id has been added <br />";
}

Also, you should avoid using mysql_* functions, as they're deprecated. Instead look up mysqli or PDO.

Travesty3
  • 14,351
  • 6
  • 61
  • 98
  • Thanks for your response. I got this error but i dont quite understand it – matture Jun 12 '13 at 02:52
  • Error in query: INSERT INTO FAQ ("id", "question", "answer") VALUES ("1","Does it work?", "I hope so!") ON DUPLICATE KEY UPDATE "question" = "Does it work?","answer" = "I hope so!". You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"id", "question", "answer") VALUES ("1","Does it work?", "I hope so!") ON DUPLIC' at line 1 – matture Jun 12 '13 at 02:52
  • Check for the primary key in the table. – Goutam Pal Jun 12 '13 at 05:40
  • 1
    @matture your syntax error is because you are using double quotes around your column names. Either replace them with backtics or just remove them. – Sean Jun 12 '13 at 06:52
  • Thanks for the respone Travesty3, I still get this error after updating with your code. Error in query: INSERT INTO FAQ (id, question, answer) VALUES ('1', 'Does it work?', 'Maybe?') ON DUPLICATE KEY UPDATE question = 'Does it work?', answer = 'Maybe?'. – matture Jun 12 '13 at 21:48
  • @matture: Is there more to that error message? What you've posted says that there was an error and it dumps the query...but it should be followed by the output from `mysqli_error()`, which will tell us what the problem is with the query. It doesn't look like there's any problem with your query syntax ([example](http://sqlfiddle.com/#!2/59917/1)), so it probably has something to do with your table setup. Do you have `id` set up as your table's primary key? – Travesty3 Jun 13 '13 at 02:20
  • @matture: Anything else about the error? [It looks like your SQL syntax is correct](http://sqlfiddle.com/#!2/59917/1). – Travesty3 Jun 13 '13 at 02:41
0
<textarea name=something id=someting>your php code over here(connect to database, select data and print the information that you had select into the textarea)</textarea>

for example:

<textarea name="someting" id="someting" rows="21" cols="70">
         <?php
        //connect to database
        $sql=mysql_query("SELECT data FROM table");     
            if(mysql_num_rows($sql)) {
              $row = mysql_fetch_row($sql);
              echo nl2br($row['0']);
        }
        ?>
 </textarea>
Ian
  • 391
  • 1
  • 17
-1

to load values to textarea, simply change < textarea value = anysomething >< /textarea> to <textarea > something < /textarea>

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170