Jump to content

[SOLVED] From dynamic form TO mysql database


mikeabe

Recommended Posts

I'd be very greatful for help with the following:

I'm working on a form for the teachers at my school to report tardies, absenses, etc. I don't know much about PHP.

 

I'm collecting the info into a MYSQL database.

 

The form consists of the following:

1) Teacher drop down populated FROM a MYSQL database

2) Student drop down populated FROM a MYSQL database

3) Date field

4) Tardy check box

5) Absense check box

6) Comment text area

 

When they click the Submit button, the following WORKS FINE (mysql collects the data.):

3) Date field

4) Tardy check box

5) Absense check box

6) Comment text area

 

The following drop down populates fine but it DOES NOT send  the seleted name to MYSQL:

1) Teacher drop down populated FROM a MYSQL database

2) Student drop down populated FROM a MYSQL database

 

My problem is getting the selected name from the drop down form INTO MYSQL

 

The drop down below works fine and is populated from MYSQL with the teacher names, but the selected teacher name won't go INTO MYSQL. This is a snippet of code from to-web.php. The full code is below the snippet.

 

/*LOADS TEACHER NAMES*/

$query="SELECT tnames FROM tchnames";

/* You can add order by clause to the sql statement if the names are to be displayed in 

alphabetical order */

$result = mysql_query ($query);
echo "<select name=Teacher>";
// printing the list box select command

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[id]>$nt[tnames]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box 


?>

 

Here is the complete code for to-web.php:

 

<html>
<body>
<h1>Discipline/Tardy Form</h1>
<h5><br><h5>
<h4>This form can be used for:<h4>
<br>
1) Tardies
<br>
2) Discipline/Praise
<br>
3) Or both at the same time.
<br>
Just fill in the appropriate information.
<br>
BE SURE to click the Submit button when finished.
<br>
Remember to also submit POSITIVE comments.
<br><br>


<br>
Click in the Teacher name field and choose your name
<br><br>

</body>
</html>

<form action="incoming.php" method="post">


<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error 

connecting to mysql');

$dbname = 'teacher';
mysql_select_db($dbname);


/*LOADS TEACHER NAMES*/

$query="SELECT tnames FROM tchnames";

/* You can add order by clause to the sql statement if the names are to be displayed in 

alphabetical order */

$result = mysql_query ($query);
echo "<select name=Teacher>";
// printing the list box select command

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[id]>$nt[tnames]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box 


?>



<html>
<body>
<br><br><br>


Click in the Student name field and choose student
<br><br>


</body>
</html>



<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error 

connecting to mysql');

$dbname = 'Student';
mysql_select_db($dbname);



$query="SELECT snames FROM studnames";

/* You can add order by clause to the sql statement if the names are to be displayed in 

alphabetical order */

$result = mysql_query ($query);
echo "<select name=student value=''>Student Name</option>";
// printing the list box select command

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[id]>$nt[snames]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box 


?>



<html><body>

<br><br><br>

Date: <input type="text" name="Date" /> 
<br><br>
Tardy:<input type="checkbox" name="Tardy" />
<br><br>
Absent<input type="checkbox" name="Absent" />
<br><br>
<textarea rows="7" cols="60" name="Comment" wrap="physical"></textarea><br /> 
<br><br>
<input type="submit" />
</form>

</body>
</html>

 

Here's the code for incoming.php that to-web.php calls:

 

<?php
$con = mysql_connect("localhost","root","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("DisciplineIncoming", $con);
$sql="INSERT INTO incoming (Teacher, Student, Date, Tardy, Comment, Absent)
VALUES
('$_POST[Teacher]','$_POST[student]','$_POST[Date]','$_POST[Tardy]','$_POST[Comment]','$_POST[Absent]')";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "<h3>Thanks! <br>Your input is now recorded on the master discipline/tardy spreadsheet. <br>You can close this window.";
mysql_close($con)
?>

 

Thanks, Mike

Hi Mike,

 

First thing i would do i secho your query to the browser like so:

 

echo $sql;

 

this needs to come after this line of code:

 

$sql="INSERT INTO incoming (Teacher, Student, Date, Tardy, Comment, Absent)

VALUES

('$_POST[Teacher]','$_POST[student]','$_POST[Date]','$_POST[Tardy]','$_POST[Comment]','$_POST[Absent]')";

 

this way you can check to see if your query is receiving the values from the form.

 

I have a feeling that this line:

echo "<option value=$nt[id]>$nt[tnames]</option>";

 

should be:

echo '<option value="$nt[id]">$nt[tnames]</option>';

 

 

I could be wrong but i think that you need the quotes around the value of the select tag

 

See how you go ;)

When I changed the code to what you suggested, something very interesting happened. Every teacher name became $nt[id]" and this was sent to MYSQL to the Teacher field:

$nt[id]"

Which i find very encouraging. After MANY hours trying, with your help, at least SOMETHING was sent to the teacher field, which has never happened before. Now if i can figure out how to get an actual teacher's name to go, i'll be in business.

I really appreciate your help.

 

Mike

P.S. That was a good suggestion to put echo $sql; in the code.

HEY!!!

It WORKS.

I changed this value:

echo "<option value=$nt[id]>$nt[tnames]</option>"; 

To this value:

echo "<option value='$nt[tnames]'>$nt[tnames]</option>";

 

You get all the credit for focusing me in on that line of code.

 

Thanks a million,

 

Mike

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.