Adding number to list
Posted: Wed Feb 11, 2009 11:57 am
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
Hello all, I have a form that updates an FAQ page, but I would like to add numbers to it, I created a new field, 'number,' next to the fields question and answer. what I'm trying to do is count the number of rows then add one when the database is updated with a new Q&A. Below is the code I'm using, everything work except it only counts the rows, it will not add one, so end up with my Q&A table in the database looking like this:
1 Q1 A1
1 Q2 A2
2 Q3 A3
3 Q4 A4
code:
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
Hello all, I have a form that updates an FAQ page, but I would like to add numbers to it, I created a new field, 'number,' next to the fields question and answer. what I'm trying to do is count the number of rows then add one when the database is updated with a new Q&A. Below is the code I'm using, everything work except it only counts the rows, it will not add one, so end up with my Q&A table in the database looking like this:
1 Q1 A1
1 Q2 A2
2 Q3 A3
3 Q4 A4
code:
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('*******', $access) or die(mysql_error());
$error = array();
if(isset($_POST['question'])) {
$result = @mysql_query('SELECT question FROM `q_a` WHERE question = \''.mysql_real_escape_string($_POST['question']).'\'');
$number = @mysql_query('SELECT COUNT(*) FROM q_a');
$count = $number + 1;
if($row = @mysql_fetch_row($result)) {
array_push($error, 'already in the Database. Please write a another.');
}
$len = strlen($_POST['question']);
$len = strlen($_POST['answer']);
@mysql_query('INSERT INTO `q_a` (question, answer, number) VALUES (\''.mysql_real_escape_string($_POST['question']).'\', \''.mysql_real_escape_string($_POST['answer']).'\', \''. $count .'\')');
if(!$error) {
echo"Update was successful.";
echo $count;
}
}
<form method="post" action="Q_A.php">
<textarea rows="8" name="question" cols="30">Question
</textarea>
<br/>
<textarea rows="8" name="answer" cols="30">Answer
</textarea>
<br/>
<input type="submit" name="submit" value="Update!" />
?>pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: