user2606735
user2606735

Reputation: 1

PHP Script, Table data populating from form

<html><head></head><body>
<?php
$db_hostname = "mysql";
$db_database = "u1da";
$db_username = "u1da";
$db_password = "1234";

$con = mysql_connect($db_hostname ,$db_username ,$db_password);
if (!$con) die ("Unable to connect to MySQL: ".mysql_error ());
mysql_select_db ( $db_database ) ||
die (" Unable to select database : ". mysql_error());

$query = "select * from students";
$result = mysql_query($query);

$result || die ("Database access failed: ".mysql_error());

$rows = mysql_num_rows($result);
echo "<table border=1>";
echo "<tr><td><p><b>Name</b></p></td></tr>";
for ($j = 0; $j < $rows ; $j ++) {
echo "<tr><td>", mysql_result($result,$j,'name') ,"</td></tr>";
}
echo "</table><br />";


$query2 = "select * from groups";
$result2 = mysql_query($query2);
$result2 || die ("Database access failed: ".mysql_error());
$rows2 = mysql_num_rows($result2);
echo "<table border=1>";
echo "<tr><td><p><b>Tutorial Group</b></p></td><td><p><b>Capacity</b></p></td></tr>";
for ($i = 0; $i < $rows2 ; $i ++) {
echo "<tr><td>", mysql_result($result2,$i,'Tutorial_Group') ,"</td><td>", mysql_result($result2,$i,'Capacity') ,"</td></tr>";
}
echo "</table>";

echo "<form method='post' action='' enctype="multipart/form-data">";

$query3 = "select * from students";
$result3 = mysql_query($query3);
echo "<br /><br /><select name='name'>";
while($name = mysql_fetch_array($result3)) {
echo "<option value='$name[Name]' > $name[Name] </option>"."<BR>";
}
echo "</select><br />";

$query4 = "select Tutorial_Group from groups";
$result4 = mysql_query($query4);
echo "<select name = 'group'>";
while($grp = mysql_fetch_array($result4)){
echo "<option value='$grp[Tutorial_Group]'>$grp[Tutorial_Group]</option>";
}
echo "</select><br />";
echo "Student ID: ";
echo '<input type="text" name="SID"><br />';
echo "Email: ";
echo '<input type="text" name="email"><br />';
echo '<input type="submit" name="submit" value="Submit">';
echo "</form>";
?>

Here is my current script code. I have to make a query which will get the value from the 2 drop-down menus and 2 text fields and insert them into a table. Table is called assg and have columns: Name, Student_ID, email, s_group. I have tried some different ways, but it didn't worked out. Please help.

Upvotes: 0

Views: 669

Answers (2)

Dubas
Dubas

Reputation: 2876

Your first problem is that you are using mysql_fetch_array to get an array of elements into $grp variable but next you try to use it with a associative array to get values like $grp[Tutorial_Group]

Using mysql_fetch_array you can get $grp[0] ... $grp[1] but not $grp[Tutorial_Group]

You need to use mysql_fetch_assoc to get associative arrays like $grp[Tutorial_Group]

Your second problem is using complex php vars incorrectly example

Incorrect:

echo "<option value='$grp[Tutorial_Group]'>$grp[Tutorial_Group]</option>";

Correct:

echo '<option value="'.$grp['Tutorial_Group'].'">'.$grp['Tutorial_Group'].'</option>';

Or

echo "<option value='{$grp['Tutorial_Group']}'>{$grp['Tutorial_Group']}</option>";

Also the code only has the part to view the tables and the form. The insert part of the data is missing in the example. Probably this part has also some errors.

Upvotes: 1

Done
Done

Reputation: 69

No indentation, html in echos, i don't even understand what you want.
By dropdown you mean select ?
What about $_POST['nameOfTheField'] ?
And why multipart ? There's no files to send here.

Upvotes: 0

Related Questions