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.