0
<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>";
?>

这是我当前的脚本代码。我必须进行一个查询,该查询将从 2 个下拉菜单和 2 个文本字段中获取值并将它们插入到表中。表称为 assg 并具有列:Name、Student_ID、email、s_group。我尝试了一些不同的方法,但没有成功。请帮忙。

4

2 回答 2

1

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.

于 2013-07-22T12:46:26.683 回答
0

没有缩进,回声中的 html,我什至不明白你想要什么。
下拉你的意思是选择?
怎么样$_POST['nameOfTheField']
为什么是 multipart ?这里没有要发送的文件。

于 2013-07-22T12:26:23.270 回答