form using names from table and input areas

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
djs1971
Forum Newbie
Posts: 14
Joined: Tue Jun 01, 2010 4:33 pm

form using names from table and input areas

Post by djs1971 »

Trying to create a form for recording scores which pulls names from a database table and places an input area next to each name for the user to type in the students score.

Here's what i've got so far which creates an html table with the right number of rows/columns etc and pulls in the names correctly BUT I'm stuck creating the input area under the scores column.

Code: Select all

	<form>
	
	<?php
	$con = mysql_connect("localhost","root","");
	if (!$con)
	  {
	  die('Could not connect: ' . mysql_error());
	  }
	
	mysql_select_db("lakeside", $con);
	
	$result = mysql_query("SELECT * FROM student_details WHERE year_id=7");
	
	echo "<table border='1'>
	<tr>
	<th>Firstname</th>
	<th>Lastname</th>
	<th>score</th>
	</tr>";
	
	while($row = mysql_fetch_array($result))
	  {
	  echo "<tr>";
	  echo "<td>" . $row['first_name'] . "</td>";
	  echo "<td>" . $row['surname'] . "</td>";
	  echo "<td>" . $row['<input type="text" name="scores" />'] . "</td>";
	  echo "</tr>";
	  }
	echo "</table>";
	
	mysql_close($con);
	?>
	
	</form>

I'm sure the issue is with the line where I try to add in the input area to the html table using: echo "<td>" . $row['<input type="text" name="scores" />'] . "</td>"; biut have no idea how to achieve this.

Any thoughts that might help an enthusiastic newbie would be gratefully received!
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: form using names from table and input areas

Post by markusn00b »

Unless you've got a database table field named '<input type="text" name="scores" />', then this isn't going to work.

Code: Select all

echo "<td><input type='text' name='scores' /></td>";
Further more, if you'd enabled PHP's error reporting (which you should be doing for development), you'd have been informed of this issue. To enable PHP's error reporting, either edit you php.ini config file to have the line error_reporting = -1 or stick error_reporting(-1); at the beginning of your page.

Mark.
djs1971
Forum Newbie
Posts: 14
Joined: Tue Jun 01, 2010 4:33 pm

Re: form using names from table and input areas

Post by djs1971 »

Mark -many thanks for the reply, particularly the heads up on error reporting during development - it's all so new and strange at the moment!

I do have a database table with a field name called scores however it is a different table to the one I am pulling in the students names from in my current query - I assume the way to sort this would be change my query to involve both tables - something like a join in the query? Am I on the right tracks!

Many thanks again for taking the time to reply.

Duncan
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: form using names from table and input areas

Post by markusn00b »

No problem.

Let's get it straight: you want the input text box next to the user's name to be populated with their score which is in a different table? Is this correct? :)
djs1971
Forum Newbie
Posts: 14
Joined: Tue Jun 01, 2010 4:33 pm

Re: form using names from table and input areas

Post by djs1971 »

Hi Mark,
To explain - I'm after a form on the screen which shows the students names (currently stored in a table called student_details) and next to each name is an input box where staff can enter a score (1-5) for the student which will be stored in different table (currently called scores - this will simply hold the students admin number and score).

Hope this makes sense - I may be going about this all completely wrong in which case feel free to tell me I'm an idiot and to go back to the drawing board :D

Here's the big picture - My ultimate aim is to develop a system for scoring 7 lessons per day for a school of 70 special needs students. Each day students have each of their 7 lessons scored 1-5 - the daily and then weekly totals are generated from this and reported back to parents each week. It's all currently done on paper and I'm interested to see if i can use my web design skills (html & css) and learn php to try and create an online way of doing it. I'm just biting off one chunk at a time here to build up my skills!

Cheers for your time and interest,

Duncan
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: form using names from table and input areas

Post by markusn00b »

Don't sweat it, Duncan. Happy to help. Sorry about my absence - I had to run out.

Anyway, the question I was trying to ask (which you didn't answer - no biggie) was: should the input box that is shown next to the student's name contain a previously entered score from the database? That is, if the user already has a score in your database, should it be shown in this input when the page is loaded? If not, you don't need to update your SQL query. You simply need to, as I have already shown, echo the HTML input box. Otherwise, you will have to update your SQL for a join query.
djs1971
Forum Newbie
Posts: 14
Joined: Tue Jun 01, 2010 4:33 pm

Re: form using names from table and input areas

Post by djs1971 »

Aaah - sorry - I see where your question was coming from, I thought I had not explained what I was trying to do properly hence the question and my long rambling answer!

I guess the answer is yes - if there is already a score in the database for the student it ought to show so staff don't overwrite other staff's scores by accident.
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: form using names from table and input areas

Post by markusn00b »

No worries :)

Righteo - then you'll want an SQL join (beginners tutorial in PHP). Basically, what you do is join (hence the name ;)) 2 tables using a relation field. I assume, in both of your tables you will have a student_id field, or something like that? Well, you will use this field to join the tables. Then you will have access to the student information and the students score.

Let me know how you get on.

Mark.
djs1971
Forum Newbie
Posts: 14
Joined: Tue Jun 01, 2010 4:33 pm

Re: form using names from table and input areas

Post by djs1971 »

Thanks - followed that advice and have created query with a join as shown in code below.

Code: Select all

<form>
	
	<?php

        error_reporting(-1);
	$con = mysql_connect("localhost","root","");
	if (!$con)
	  {
	  die('Could not connect: ' . mysql_error());
	  }
	
	mysql_select_db("lakeside", $con);
	
	$result = mysql_query("SELECT * FROM students
	INNER JOIN scores
	ON students.admin=scores.admin
	WHERE year='7'");
	
	echo "<table border='0'>
	<tr>
	<th>Firstname</th>
	<th>Lastname</th>
	<th>score</th>
	</tr>";
	
	while($row = mysql_fetch_array($result))
	  {
	
	  
	  echo "<tr>";
	  echo "<td>" . $row['fname'] . "</td>";
	  echo "<td>" . $row['surname'] . "</td>";
	echo "<td>" . $row['score']  . "</td>";
	  echo "</tr>";
	  }
	echo "</table>";
	
	mysql_close($con);
	?>
	
	</form>

Using this code I can display the student fname & surname and any current scores. in a 3 column table.
However, if I change:

Code: Select all

echo "<td>" . $row['score']  . "</td>";
to try and make this part of the table a place to input scores by altering the code to:

Code: Select all

echo "<td>" . $row['<input type="text" name="score" >']  . "</td>";
I get empty cells in the table where the scores where instead of being able to type in a score.

I'm obviously cocking this bit up somehow - will try to dig around and see what I've done wrong!
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: form using names from table and input areas

Post by markusn00b »

No, no. You've regressed back to your original problem now.

I'll try to explain: When you use subscript (with an index) syntax, that is, those ['index'] after a variable, you are implying that that array has an element inside it named 'index'. As you can see, your code suggests your $row array has some crazy HTML name - I highly doubt it does, as I said before.

What I think you're wanting is:

Code: Select all

echo "<td><input type='text' name='score' value='{$row['score']}' /></td>";
Mark.
djs1971
Forum Newbie
Posts: 14
Joined: Tue Jun 01, 2010 4:33 pm

Re: form using names from table and input areas

Post by djs1971 »

Magic! That sorted it for me. I've altered some of the field names etc and set it up again and it's passing data.

The code I ended up with is as follows:

Code: Select all

<form action="savescores.php" method="get">
	
	<?php
	
	error_reporting(-1);
	$con = mysql_connect("localhost","root","");
	if (!$con)
	  {
	  die('Could not connect: ' . mysql_error());
	  }
	
	mysql_select_db("lakeside", $con);
	
	$result = mysql_query("SELECT * FROM students
	WHERE year='7'");
	
	echo "<table border='0'>
	<tr>
	<th>Admin</th>
	<th>Firstname</th>
	<th>Lastname</th>
	<th>score</th>
	</tr>";
	
	while($row = mysql_fetch_array($result))
	  {
	
	  
	  echo "<tr>";
	  echo "<td>" . $row['admin'] . "</td>";
	  echo "<td>" . $row['fname'] . "</td>";
	  echo "<td>" . $row['surname'] . "</td>";
	  echo "<td><input type='text' name='score' value='{$row['score']}' /></td>";
	  echo "</tr>";
	  }
	echo "</table>";
	
	mysql_close($con);
	?>
	
	<input type="submit" />
	
	</form>
Thank you so much for your help - really is appreciated hugely.
I'll start on the .php to handle the data passed from the form now :-)
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: form using names from table and input areas

Post by markusn00b »

Coolios. Be sure to post back if you need anymore help :)
djs1971
Forum Newbie
Posts: 14
Joined: Tue Jun 01, 2010 4:33 pm

Re: form using names from table and input areas

Post by djs1971 »

Hi Mark - one final question (I hope!).

When it comes to inserting the data from the form into the database table, the way I have this set up presently only inserts one record.

Using my test data in the database table for students I have 3 records.

When I view the form it is perfect - all 3 records show up and I can enter scores for all 3 students.

This data is obviously being passed to the .php page handing the INSERT as I am using GET and can see the data for all 3 students in the url.

However, only the last of the 3 records is saved into the scores table.

I've spent a couple of hours scouring the net looking for info on inserting multiple records but all the examples I find have the data 'hard coded' eg:

Code: Select all

$sql = "INSERT INTO beautiful (name, age)
  VALUES
  ('Helen', 24),
  ('Katrina', 21),
  ('Samia', 22),
  ('Hui Ling', 25),
  ('Yumie', 29)";
However I cannot find a method for inserting multiple records in a situation like mine where the number of potential records passed from the form to the script running the INSERT is not set in stone.

I wondered if you have any suggestions - not sure from my research if this should be handled with some form of loop or IF/ELSE statement or some other method?

For info, the form passes the data to this PHP at the moment:

Code: Select all

	<?php
	$con = mysql_connect("localhost","root","");
	if (!$con)
	  {
	  die('Could not connect: ' . mysql_error());
	  }
	
	mysql_select_db("lakeside", $con);
	
	$sql="INSERT INTO scores (score, admin)
	VALUES
	('$_GET[score]','$_GET[admin]')";
	
	if (!mysql_query($sql,$con))
	  {
	  die('Error: ' . mysql_error());
	  }
	echo "Records added";
	
	mysql_close($con)
	?>
		
Post Reply