请帮助我我是新的在php中我想通过使用php在一个页面中列出,更新和删除记录我写了下面的代码并给我错误,如:
Warning: mysql_query() expects parameter 2 to be resource, string given in C:\Program Files\EasyPHP-5.3.9\www\Authentication1\fixture2.php on line 29
Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\Program Files\EasyPHP-5.3.9\www\Authentication1\fixture2.php on line 30
添加下载
对手:通知:未定义的变量: C:\ Program Files \ EasyPHP-5.3.9 \ www \ Authentication1 \ fixture2.php 中的对手 58
日期:
注意:未定义的变量:日期在 C:\ Program Files \ EasyPHP-5.3.9 \ www \ Authentication1 \ fixture2.php 上 61
地点:
通知:未定义的变量: C:\ Program Files \ EasyPHP-5.3.9 \ www \ Authentication1 \ fixture2.php 上的地点 64
任何人都可以帮助您使用正确的代码
这是代码
<html>
<head>
<title>Update/Delete Test Page</title>
</head>
<body>
<?php
include 'connect.php';
if ( isset($_POST['Submit'] ) ) {
// here if no ID then adding else we're editing
if ( $fixture_id ) {
$sql = "UPDATE fixtures SET opponents='$opponents',date='$date',venue='$venue' WHERE fixture_id=$fixture_id";
} else {
$sql = "INSERT INTO fixtures (opponents,date,venue) VALUES ('$opponents', '$date', '$venue')";
}
// run SQL against the DB
$result = mysql_query( $sql );
echo "Record updated/edited!<p>";
} elseif ( isset( $_POST['delete'] ) ) {
// delete a record
$sql = "DELETE FROM fixture WHERE fixture_id = $fixture_id";
$result = mysql_query( $sql );
echo "$sql Record deleted!<p>";
} else {
// this part happens if we don't press submit
if ( !isset( $_POST['fixture_id'] ) ) {
// print the list if there is not editing
$result = mysql_query("SELECT * FROM fixtures", $db_name);
while ( $myrow = mysql_fetch_array( $result ) ) {
printf("<a href=\"%s?fixture_id=%s\">%s %s</a> \n", $PHP_SELF, $myrow["fixture_id"], $myrow["fixture_id"], $myrow["opponents"]);
printf("<a href=\"%s?fixture_id=%s&delete=yes\">(DELETE)</a><br>", $PHP_SELF, $myrow["fixture_id"]);
}
}
?>
<P>
<a href="<?php echo $PHP_SELF?>">ADD A DOWNLOAD</a>
<P>
<form method="post" action="<?php echo $PHP_SELF?>">
<?php
if ( isset( $_POST['fixture_id'] ) ) {
// editing so select a record
$sql = "SELECT * FROM fixtures WHERE fixture_id = $fixture_id";
$result = mysql_query( $sql );
$myrow = mysql_fetch_array( $result );
$fixture_id = $myrow["fixture_id"];
$opponents = $myrow["opponents"];
$date = $myrow["date"];
$venue = $myrow["venue"];
// print the id for editing
?>
<input type=hidden name="fixture_id" value="<?php echo $fixture_id ?>">
<?php
}
?>
opponents:
<textarea name="opponents" cols="50" rows="1"><?php echo $opponents ?></textarea>
<br>
date:
<textarea name="date" cols="50" rows="5"><?php echo $date ?></textarea>
<br>
venue:
<textarea name="venue" cols="50" rows="1"><?php echo $venue ?></textarea>
<br>
<input type="submit" name="submit" value="Edit">
</form>
<?php
}
?>
</body>
</html>
答案 0 :(得分:1)
我无法确切地理解你想要做什么。我建议你使用正确的代码约定/编码标准。如果您愿意,我可以为您找到一些资源,您可以回复。
无论如何,我会尽力帮助您解决错误。
在使用变量之前,必须为其赋值。例如,在你的第一个$ sql值中,你使用的是你没有给予值的变量,比如$ opponents。如果您指的是FORM中的值,那么您应该像这样访问它:$ _POST ['input name']。例如,使用$ _POST ['opponents']
而不是使用$ opponents当提交应用程序中的表单时,表单数据将发送到页面,值将在全局$ _POST数组中发送。
例如,你应该这样做:
if (isset($_POST['Submit'])) {
$newOpponents=$_POST['opponents'];
$newDate=$_POST['date'];
$newVenue=$_POST['venue'];
if ($_POST['fixture_id']) {
$newFixtureID=$_POST['fixture_id'];
$sql = "UPDATE fixtures SET opponents='$newOpponents',date='$newDate',venue='$newVenue' WHERE fixture_id=$newFixtureID";
}
/*.. etc (use $_POST['input name'] to access values passed from your form (via $_POST array) */