Page 1 of 1

Adding number to list

Posted: Wed Feb 11, 2009 11:57 am
by Greg19
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: :arrow: 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: :arrow: Posting Code in the Forums to learn how to do it too.

Re: Adding number to list

Posted: Wed Feb 11, 2009 3:54 pm
by Drachlen
Hi Greg,

Here is a solution to your problem.

First, here are the lines that are important to evaluate:

Code: Select all

$number = @mysql_query('SELECT COUNT(*) FROM q_a');
$count = $number + 1;
The first step to take is to figure out if $number is returning anything, so we do this by adding a quick echo or die message within the code, for example:

Code: Select all

$number = @mysql_query('SELECT COUNT(*) FROM q_a');
die($number);
$count = $number + 1;
This should print out something like:

Code: Select all

Resource id #3
Now already we've realized something: mysql_query returns resources. In other situations, mysql_query might return true or false, but that is not the case here.

So what do we do?

You have plenty of options, here's an example of one:

Code: Select all

$number = mysql_query('SELECT COUNT(*) FROM q_a');
$realnumber = mysql_fetch_array($number);
$count = $realnumber[0] + 1;
And another:

Code: Select all

$number = mysql_query("SELECT * FROM q_a");
$count = mysql_num_rows($number) + 1;
Both of these take the initial resources and extract specific data from them. In our case, we're extracting the row count. It's important to realize that a resource often contains a lot more data than just what we require of it, so it's necessary to specify exactly what we need.

I hope this helps give you a bit more understanding. MySQL can be confusing at first, but always remember, if your variables are not returning the right numbers, print them all out so you can isolate the issue.

Good luck.

Re: Adding number to list

Posted: Fri Feb 20, 2009 1:12 pm
by Greg19
Thanks a ton, that was really helpful and works like a charm. Sorry it took me so long to reply, exams all last week.