Jump to content

[SOLVED] help with forms


jeaker

Recommended Posts

I have a form with 3 drop down boxes, one text field, and a file upload box. I have the 3 drop down boxes pulling from 3 separate tables in my database. I want to be able to save the selection that has been made in the drop down boxes, the text that was enter, and the file that the user has selected all to a different table in the database.

 

<table align="center" border="0" cellpadding="0" cellspacing="0" width="800">
<tbody><tr>
<td height="196">
<form method="post" action="upload.php">
  <table width="528" border="0" cellpadding="2" cellspacing="1" class="box" align="center">
    <tr> 
      <td width="100">Name :</td>
      <td width="417"><?php
  // Query the teacher table and load all of the records
  // into an array.
  $sql = 'SELECT * FROM teacher';
  $res = mysql_query($sql) or die(mysql_error());
  while ($rec = mysql_fetch_assoc($res)) $teacher[] = $rec;
  echo '<SELECT name="teacher">';
  foreach ($teacher as $t)
    echo "<OPTION>{$t['fname']}, {$t['lname']}</OPTION>\n";
  echo '</SELECT>';
  ?></td>
    </tr>
    <tr> 
      <td width="100">Semester :</td>
      <td><?php
  // Query the Semester table and load all of the records
  // into an array.
  $sql = 'SELECT * FROM semester';
  $res = mysql_query($sql) or die(mysql_error());
  while ($rec = mysql_fetch_assoc($res)) $semester[] = $rec;
  
echo '<SELECT name="semester">';
  foreach ($semester as $s)
    echo "<OPTION>{$s['season']}</OPTION>\n";
  echo '</SELECT>';
  ?></td>
    </tr>
    <tr> 
      <td width="100">Year :</td>
      <td><?php
  // Query the year table and load all of the records
  // into an array.
  $sql = 'SELECT * FROM year';
  $res = mysql_query($sql) or die(mysql_error());
  while ($rec = mysql_fetch_assoc($res)) $year[] = $rec;
  
echo '<SELECT name="year">';
  foreach ($year as $y)
    echo "<OPTION>{$y['date']}</OPTION>\n";
  echo '</SELECT>';
  ?></td>
    </tr>
 <tr> 
      <td width="100">Course ID :</td>
      <td><input name="course" type="text" class="box" id="course"></td>
    </tr>
 <tr> 
      <td width="100">Syllabus : </td>
      <td><input type="file" name="data" size="40"></td>
    </tr>
 <tr> 
      <td width="100"> </td>
      <td> </td>
    </tr>
    <tr> 
      <td height="36" colspan="2" align="center"><input name="add" type="submit" class="box" id="update" value="Add Syllabus"></td>
    </tr>
  </table>
</form>
</td>
</tr>
</tbody>
</table>

 

I have made several attempts to write a "upload.php", but have failed miserable each time. If anyone could help it would be greatly appreciated. If I wasn't clear enough in the above description I want to upload all of the information that the user has selected/enter into a new/separate table called, "syllabus".

Thanks in advance for the help.

As always it is GREATLY appreciated.

Link to comment
https://forums.phpfreaks.com/topic/49107-solved-help-with-forms/
Share on other sites

Here is what I have been working on so far. It is not very good and does not work, but it is supposed to add the information entered in the form above into the database. Any help or advice would be greatly appreciated.

Thanks!!

 

<?php
$teacher=$_POST['teacher'];
$semester=$_POST['semester'];
$year=$_POST['year'];
$course=$_POST['course'];

$data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));

mysql_connect("connect", "username", "password") or die(mysql_error());
mysql_select_db("upload") or die(mysql_error());

mysql_query("INSERT INTO `syllabus` VALUES ('$teacher', '$semester', '$year', '$course', '$data','$form_data_name')");
Print "Your information has been successfully added to the database.";
?>

You need to tell the data base where you want the values put, you are just missing part of the query

Snowdog

 

 

mysql_query("INSERT INTO `syllabus` ('teacher', 'semester', 'year', 'course', 'data','form_data_name') VALUES ('$teacher', '$semester', '$year', '$course', '$data','$form_data_name')");

 

I am self taught and I think you might be missing one more part. Here is how I would do it.

$query = "INSERT INTO syllabus ('teacher', 'semester', 'year', 'course', 'data','form_data_name') VALUES ('$teacher', '$semester', '$year', '$course', '$data','$form_data_name')");

$result = mysql_query($query) or die('Query failed: ' . mysql_error());

I have updated the code

<?php
$teacher=$_POST['teacher'];
$semester=$_POST['semester'];
$year=$_POST['year'];
$course=$_POST['course'];

$data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));

mysql_connect("connect", "username", "password") or die(mysql_error());
mysql_select_db("upload") or die(mysql_error());

$query = ("INSERT INTO syllabus ('id', 'teacher', 'semester', 'year', 'course', 'data','filename') VALUES ('', '$teacher', '$semester', '$year', '$course', '$data','$form_data_name')");

$result = mysql_query($query) or die('Query failed: ' . mysql_error());
Print "Your information has been successfully added to the database.";
?>

With snowdogs help and I am now getting a error message for SQL.

The message is...

Query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''id', 'teacher', 'semester', 'year', 'course', 'data','filename'

 

I don't know why it would give this error? all of my SQL syntax appears to be correct.

Try this instead:

$query = "INSERT INTO syllabus ('teacher', 'semester', 'year', 'course', 'data', 'form_data_name') VALUES ($teacher, $semester, $year, $course, $data, $form_data_name)";

 

Or try:

$query = "INSERT INTO syllabus ('teacher', 'semester', 'year', 'course', 'data', 'form_data_name') VALUES (" . $teacher . "," . $semester . "," . $year . "," . $course . "," . $data . "," . $form_data_name . ")";

 

I prefer the second one myself...

I see two potential problems.

 

1) you are inserting into a blank id (see red). If your database is set up for auto increment on the id then take th $id out and the ' ' out.

 

2) I dont know if it matters but take out the parenthisis in red also

 

$query = ("INSERT INTO syllabus ('id', 'teacher', 'semester', 'year', 'course', 'data','filename') VALUES ('', '$teacher', '$semester', '$year', '$course', '$data','$form_data_name')");

 

so I think this is what the query should be. (assuming auto increment on the id)

 

$query = "INSERT INTO syllabus ('teacher', 'semester', 'year', 'course', 'data','filename') VALUES ('$teacher', '$semester', '$year', '$course', '$data','$form_data_name')";

 

Snowdog

 

Finally got it to work. Thank you guys so much for your help. I def couldn't have gotten it to work without you.

 

The final code is...

<?php
$teacher=$_POST['teacher'];
$semester=$_POST['semester'];
$year=$_POST['year'];
$course=$_POST['course'];

$data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));

mysql_connect("connect", "username", "password") or die(mysql_error());
mysql_select_db("upload") or die(mysql_error());

$query = "INSERT INTO syllabus (teacher, semester, year, course, data) VALUES ('$teacher', '$semester', '$year', '$course', '$data')";

$result = mysql_query($query) or die('Query failed: ' . mysql_error());
Print "Your information has been successfully added to the database.";
?>

 

Thanks again you guys for your help!!!

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.