0

我正在尝试为常见问题解答 CMS。

我有一个页面,可以将问题和答案加载到具有独特想法的文本区域中,并且计划也编辑这些文本区域,然后将它们插入数据库中。

我也希望能够将新的常见问题解答添加到数据库中。

现在我正在尝试使用 a 来完成此操作,ON DUPLICATE KEY UPDATE..但它无法正常工作。

索引.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>

常见问题解答.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>';

接下来我应该尝试什么?

更新代码。还是行不通。

4

3 回答 3

0

看起来你的标记有一些问题。

我认为您的 while 循环中的输出应该看起来更像这样:

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>
';

然后在 faqsql.php 中,你的代码看起来更像这样:

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 />";
}

此外,您应该避免使用 mysql_* 函数,因为它们已被弃用。而是查找mysqliPDO

于 2013-06-12T02:35:56.170 回答
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>

例如:</p>

<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>
于 2014-03-05T06:14:48.607 回答
-1

要将值加载到 textarea,只需更改< textarea value = anysomething >< /textarea><textarea > something < /textarea>

于 2013-12-10T11:10:04.333 回答