以下代码正在运行(它将数据发送到数据库)。但是我一打开页面就会得到“通知:未定义的索引”,它还在数据库中创建了一个空行(我的意思是,每个字段的 onde 空值)。如果我填写这些字段并提交它会起作用,但是它们会保存在数据库中。我想我需要一个代码来告诉“提交”不要将数据发送到空的数据库。那是什么代码?我不熟悉mysqli。
<?php
$con=mysqli_connect("localhost","root","","activitytest1");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO activitytest1 (type, description, author, outcome, pre, whileact, postact, path, books, grouping, time)
VALUES
('$_POST[type]','$_POST[description]','$_POST[author]','$_POST[outcome]','$_POST[pre]','$_POST[whileact]','$_POST[postact]','$_POST[path]','$_POST[books]','$_POST[grouping]','$_POST[time]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
$result = mysqli_query($con,"SELECT * FROM activitytest1");
while($row = mysqli_fetch_array($result))
{
echo "<br/>
<table width='909' border='1' align='center' cellpadding='5' cellspacing='0'>
<tr>
<td width='125'>" . $row['type'] . "</th>
<td width='680'>" . $row['description'] . "</th>
<td width='120'>" . $row['author'] . "</th>
</tr></table> <br/>" . $row['pre'] . "<br/>" . $row['whileact'] . "<br/>" . $row['postact'] . "<br/>" . $row['outcome'] . "<br/>" . $row['path'] . "<br/>" . $row['books'] . "<br/>" . $row['grouping'] . "<br/>" . $row['time'] . "<hr size='1'/>";
}
mysqli_close($con);
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test 1</title>
</head>
<body>
<br>
<table width="909" border="1" align="center" cellpadding="5" cellspacing="0">
<tr>
<th width="125" scope="col">Type</th>
<th width="680" scope="col">Description</th>
<th width="120" scope="col">Author</th>
</tr></table>
<br>
<form action="index.php" method="POST">
<table width="909" border="1" align="center" cellpadding="5" cellspacing="0">
<td width="125"><label>
</label>
<select name="type" id="type" onChange="display(this,'Task','Semi-task','Practice','Exercise','Lead-in');">
<option value="Unselected" selected="selected">Choose one:</option>
<option value="Task">Task</option>
<option value="Semi-task">Semi-task</option>
<option value="Practice">Practice</option>
<option value="Exercise">Exercise</option>
<option value="Lead-in">Lead-in</option>
<option value="Game">Game</option>
<option value="Video">Video</option>
<option value="Song">Song</option>
<option value="Mimio">Mimio</option>
<option value="Other">Other</option>
</select></td>
<td width="680"><div id="Semi-task" style="display: none;">
Pre:<br>
</div>
<div id="Exercise" style="display: none;">whileact:</div>
<br>
<div id="Practice" style="display: none;">
Post:<br>
<br>
</div>
<div id="Task" style="display: none;"> Outcome: <br />
<textarea name="description" cols="70" rows="2"></textarea>
<br />
</div>
<div id="Lead-in" style="display: none;">
Link/Path:<br>
<br>
</div></td>
<td width="120"><input name="author" type="text" size="12" maxlength="25" /></td>
</tr>
<tr>
<td colspan="3"> <textarea name="pre" id="Pre" cols="45" rows="5"></textarea>
<br /><textarea name="whileact" id="whileact" cols="45" rows="5"></textarea>
<br /><textarea name="postact" id="Post" cols="45" rows="5"></textarea>
<br /><textarea name="outcome" id="Outcome" cols="45" rows="5"></textarea>
<br /><input type="text" name="path" id="Path" />
<br /><input type="text" name="books" id="Books" />
<br /><input type="text" name="grouping" id="Grouping" />
<br /><input type="text" name="time" id="Time" />
</td>
</tr>
<tr>
<td colspan="3"><input type="submit" name="submit" value="Comment" /></td>
</tr>
</table>
</form>
</body>
</html>