我在为指定的事件ID实现RecordResults时遇到一些麻烦。它需要能够做到 - 页面底部列出的事件条目应该包含一个 超链接,将管理员转到新的Web表单以记录该事件的结果。该 EventID通过查询字符串参数传递,例如RecordResults.php?事件ID = 4。
目前我有这个代码显示:
将数据记录到数据库
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("clubresults") or die(mysql_error());
if (isset($_POST['submit'])) {
$EventDate = date('D-m-y', $EventDate);
$sql="INSERT INTO events (EventName, EventDate, Location)
VALUES
('".$_POST['EventName']."', '".$EventDate."', '".$_POST['EventLocation']."')";
$add_event = mysql_query($sql);
echo "Successfully Added 1 Event";
}
?>
HTML表单+从MySQL数据库生成表 -
<h2> Add Event To Database </h2>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Event Name: <input type="text" name="EventName" /><br>
Event Date: <input type="text" name="EventDate" /><br>
Event Location: <input type="text" name="EventLocation" /><br>
<input type="submit" name="submit" value="Add Event" />
</form>
<?php
$query = "SELECT * FROM events";
$result = mysql_query($query) or die ('Error in Query');
echo '<table width=100% border=1>';
echo '<tr><td><b>Event ID</b></td><td><b>Event Name</td><td><b>Event Date</b></td><td><b>Event Location</b></td><td><b> Record Results</b></td></tr>';
while ($row=mysql_fetch_row($result))
{
echo '<tr>';
echo '<td>'.$row[0].'</td>';
echo '<td>'.$row[1].'</td>';
echo '<td>'.$row[2].'</td>';
echo '<td>'.$row[3].'</td>';
echo '</tr>';
}
echo '</table>';
mysql_free_result($result);
?>
我也一直在研究这个问题,但是我不知道如何使用它来为表格中传递URL字符串的每一行添加结果超链接。
// Get id from URL
$id = mysql_real_escape_string( $_GET['EventID'] );
// If id is number
if( (int)$id== $id && (int)$id> 0 ) {
// Get record from database
$sql = 'SELECT * FROM Evernts WHERE EventID=' . $id;
$result = mysql_query( $sql, $link );
$row = mysql_fetch_array( $result );
// Show record with HTML here - Need to show it in the table!
print_r( $row );
} else {
echo "Record not found";
}
任何帮助将不胜感激!
谢谢:D
答案 0 :(得分:1)
以下是一些建议的答案。
$EventDate
,有些不像$result
,有些则带有下划线$add_event
。使用camelCase命名变量。您在数据库中使用表列名称也是如此:您有EventName,EventDate,然后是Location ... $EventDate = date('D-m-y', $EventDate);
这样的指令。您不需要声明变量,因此它会将$ EventDate计算为0($EventDate = date('D-m-y', 0)
)。如果您不知道函数的功能或使用方法,请阅读manual。 根据我的理解,您希望在HTML表格中显示数据库中的所有事件 并且可以通过表单添加事件。 您还希望将事件链接到记录结果页面,将事件ID作为URL中的参数传递(您不清楚这一点)。的index.php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("clubresults") or die(mysql_error());
if (isset($_POST['submit']))
{
$eventName = mysql_real_escape_string($_POST['EventName']);
$eventLocation = mysql_real_escape_string($_POST['EventLocation']);
// if you want to retrieve the date via the form
// you have to check the format so that it corresponds to
// what your date column expects (default format is Y-m-d)
$eventDate = date('Y-m-d');
$sql = "
INSERT INTO events (EventName, EventDate, EventLocation)
VALUES (
'" . $eventName . "',
'" . $eventDate . "',
'" . $eventLocation . "'
)";
$addEventQueryResult = mysql_query($sql);
if ($addEventQueryResult)
{
echo "Successfully added 1 event";
}
else
{
echo "Error adding the event";
}
}
?>
<h2>Add Event To Database</h2>
<form action="index.php" method="post">
Event Name: <input type="text" name="EventName" /><br>
Event Date: <input type="text" name="EventDate" /><br>
Event Location: <input type="text" name="EventLocation" /><br>
<input type="submit" name="submit" value="Add Event" />
</form>
<?php
$query = "
SELECT EventId, EventName, EventDate, EventLocation
FROM events";
$result = mysql_query($query) or die ('Error in Query');
echo '<table width="100%" border="1">';
echo '
<tr>
<th>Event ID</th>
<th>Event Name</th>
<th>Event Date</th>
<th>Event Location</th>
<th>Record Results</th>
</tr>';
while ($row = mysql_fetch_assoc($result))
{
$addResultLink =
'<a href="recordResults.php?EventId=' .
$row['EventId'] .
'">Add Results</a>';
echo '<tr>'
. '<td>' . $row['EventId'] . '</td>'
. '<td>' . $row['EventName'] . '</td>'
. '<td>' . $row['EventDate'] . '</td>'
. '<td>' . $row['EventLocation'] . '</td>'
. '<td>' . $addResultLink . '</td>'
. '</tr>';
}
echo '</table>';
mysql_free_result($result);
?>
这是recordResults.php,它根据URL中的事件id检索事件,并使用与上面相同的HTML表显示事件。做你想做的事。注意:此代码应该是重构代码,因此您不会两次编写相同的HTML表。
<?php
date_default_timezone_set('Europe/Paris');
mysql_connect("localhost", "root", "sqlflo54") or die(mysql_error());
mysql_select_db("clubresults") or die(mysql_error());
// Get id from URL
$id = mysql_real_escape_string($_GET['EventId']);
// If id is number
if ($id > 0)
{
// Get record from database
$sql = "
SELECT EventId, EventName, EventDate, EventLocation
FROM events
WHERE EventId = " . $id;
$result = mysql_query($sql);
echo '<table width="100%" border="1">';
echo '
<tr>
<th>Event ID</th>
<th>Event Name</th>
<th>Event Date</th>
<th>Event Location</th>
</tr>';
while ($row = mysql_fetch_assoc($result))
{
echo '<tr>'
. '<td>' . $row['EventId'] . '</td>'
. '<td>' . $row['EventName'] . '</td>'
. '<td>' . $row['EventDate'] . '</td>'
. '<td>' . $row['EventLocation'] . '</td>'
. '</tr>';
}
echo '</table>';
mysql_free_result($result);
}
else
{
echo "Record not found";
}
?>
我希望这最终会帮助您了解您想要做什么。 不要复制和粘贴此代码,因为我更改了一些变量名称以坚持上述建议。