gpt4 book ai didi

php - 使用 PHP 编辑 MySQL 条目

转载 作者:行者123 更新时间:2023-11-29 20:50:55 25 4
gpt4 key购买 nike

我一直在寻找这个问题和类似问题的答案,但没有成功。我有一个数据库,以及一个允许我编辑和删除数据库中的条目的选项,我可以完美地删除条目,但我似乎无法编辑它们,我已附加代码,如果有人有任何想法,这将是惊人的,非常感谢。

<?php
require_once('includes/connection.php');
// initialize flags
$OK = false;
$done = false;
// get details of selected record
//if (isset($_GET['band_id']) && !$_POST) {
if (isset($_GET['band_id'])) {
// prepare SQL query
$sql = 'SELECT band_id, email, bandname, bio, state, genre, link, sound FROM submit
WHERE band_id = ?';
$stmt = $conn->prepare($sql);
// bind the results using numbers to reference the columns used in the select statement
$stmt->bindColumn(1, $band_id);
$stmt->bindColumn(2, $email);
$stmt->bindColumn(3, $bandname);
$stmt->bindColumn(4, $bio);
$stmt->bindColumn(5, $state);
$stmt->bindColumn(6, $genre);
$stmt->bindColumn(7, $link);
$stmt->bindColumn(8, $sound);
// execute query by passing array of variables
$OK = $stmt->execute(array($_GET['band_id']));
$stmt->fetch();
}
// if form has been submitted, update record
if (isset($_POST['update'])) {
// prepare update query
$sql = 'UPDATE submit SET email = ?, bandname = ?, bio = ?, state = ?, genre = ?, link = ?, sound = ?
WHERE band_id = ?';
$stmt = $conn->prepare($sql);
// execute query by passing array of variables
$stmt->execute(array($_POST['band_id'], $_POST['email'], $_POST['bandname'], $_POST['bio'], $_POST['state'], $_POST['genre'], $_POST['link'], $_POST['sound']));
$done = $stmt->rowCount();
}
// redirect if $_GET['band_id'] not defined
if ($done || !isset($_GET['band_id'])) {
header('Location: http://localhost/giggedin/submit.php');
exit;
}
// display error message if query fails
if (isset($stmt) && !$OK && !$done) {
$error = $stmt->errorInfo();
if (isset($error[2])) {
$error = $error[2];
}
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="Find a support band for you band with us">
<meta name="author" content="Chris Beechey, Copyright 2016">
<meta name="keywords" content="Music, Australia, Band, Show, Gig">
<link rel="icon" href="">
<title>GIGGEDIN - FIND A BAND</title>

<!--- BOOTSTRAP ------->
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" >
<!--- CSS FOR WHOLE PAGE STYLE -->
<link rel="stylesheet" type="text/css" href="css/styles.css">
<!--- CSS FOR NAV BAR -->
<link rel="stylesheet" type="text/css" href="css/nav.css">
<link rel="stylesheet" type="text/css" href="css/form.css">
<!--- FONTS ------------->
<link href='https://fonts.googleapis.com/css?family=Open+Sans|Oswald|Architects+Daughter' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="css/ionicons.min.css">

<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<header>
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
<a href="index.php">Home</a>
<a href="submit.php">Submit Band</a>
<a href="search.php">Search Bands</a>


<div class="social">
<h4>Follow us</h4>
<i class="ion-social-facebook"></i>
<i class="ion-social-twitter"></i>
<i class="ion-social-instagram"></i>
</div>
</div>
<span style="font-size:30px;cursor:pointer;color:antiquewhite;" onclick="openNav()">&#9776; MENU</span>
</header>
<body>
<div class="content">
<div class="container">
<h3>admin only - Edit</h3>

<?php
if (isset($error)) {
echo "<p class='warning'>Error: $error</p>";
}
if($band_id == 0) { ?>
<p class="warning">Invalid request: record does not exist.</p>
<?php } else { ?>

<form role="form" method="post" action="search.php">
<fieldset>
<div class="form-group row">
<label for="email" class="col-sm-2 form-control-label">Band ID</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="band_id" name="band_id" value="<?php echo htmlentities($band_id); ?>" />
</div>
</div>
<div class="form-group row">
<label for="email" class="col-sm-2 form-control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" value="<?php echo htmlentities($email); ?>" />
</div>
</div>
<div class="form-group row">
<label for="bandname" class="col-sm-2 form-control-label">Band Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="bandname" name="bandname" value="<?php echo htmlentities($bandname); ?>" />
</div>
</div>
<div class="form-group row">
<label for="bio" class="col-sm-2 form-control-label">Brief Bio</label>
<div class="col-sm-10">
<textarea name="bio" class="form-control" id="bio" rows="5" value="<?php echo htmlentities($bio); ?>" /></textarea>
</div>
</div>
<div class="form-group row">
<label for="state" class="col-sm-2 form-control-label">State</label>
<div class="col-sm-10">
<select name="state" class="form-control" id="state" value="<?php echo htmlentities($state); ?>" />
<option value="QLD">QLD</option>
<option value="SA">SA</option>
<option value="VIC">VIC</option>
<option value="WA">WA</option>
<option value="TAS">TAS</option>
<option value="NSW">NSW</option>
<option value="NT">NT</option>
<option value="ACT">ACT</option>
</select>
</div>
</div>
<div class="form-group row">
<label for="genre" class="col-sm-2 form-control-label">Genre</label>
<div class="col-sm-10">
<select name="genre" class="form-control" id="genre" value="<?php echo htmlentities($genre); ?>" />
<option value="rock">ROCK</option>
<option value="punk">PUNK</option>
<option value="blues">BLUES</option>
<option value="bluesrock">BLUES/ROCK</option>
<option value="metal">METAL</option>
<option value="jazz">JAZZ</option>
<option value="acoustic">ACOUSTIC</option>
<option value="solo">SOLO</option>
</select>
</div>
</div>

<div class="form-group row">
<label for="link" class="col-sm-2 form-control-label">FB/Website Link:</label>
<div class="col-sm-10">
<input type="url" class="form-control" id="link" name="link" size="30" value="<?php echo htmlentities($link); ?>" />
</div>
</div>

<div class="form-group row">
<label for="sound"class="col-sm-2 form-control-label">Sound/Video Link:</label>
<div class="col-sm-10">
<input type="url" class="form-control" id="sound" name="sound" size="30" value="<?php echo htmlentities($sound); ?>" />
</div>
</div>
<input type="submit" name="update" value="update" class="submit" id="update" />
</fieldset>
</form>
<?php } ?>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="js/scripts.js"></script>
</body>
</html>

最佳答案

你会希望这些的顺序匹配

 $sql = 'UPDATE submit SET email = ?, bandname = ?, bio = ?, state = ?, genre = ?, link = ?, sound = ?
WHERE band_id = ?';
$stmt = $conn->prepare($sql);
// execute query by passing array of variables
$stmt->execute(array($_POST['band_id'], $_POST['email'], $_POST['bandname'], $_POST['bio'], $_POST['state'], $_POST['genre'], $_POST['link'], $_POST['sound']));

具体

 $_POST['band_id']

应该是列表中的最后一个而不是第一个。所以只需将其更改为:

 $stmt->execute(array($_POST['email'], $_POST['bandname'], $_POST['bio'], $_POST['state'], $_POST['genre'], $_POST['link'], $_POST['sound'], $_POST['band_id']));

这是 PDO 吗?或 mysqli。如果它的 pdo (看起来像 pdo,我大约 3 年没有使用 mysqli )你可以使用命名参数并避免这个问题。

  $sql = 'UPDATE submit SET email = :email, bandname = :bandname , bio = :bio, state = :state, genre = :genre, link = :link, sound = :sound
WHERE band_id = :band_id';
$stmt = $conn->prepare($sql);
// execute query by passing array of variables
$stmt->execute(array($_POST[':band_id'], $_POST[':email'], $_POST[':bandname'], $_POST[':bio'], $_POST[':state'], $_POST[':genre'], $_POST[':link'], $_POST[':sound']));

如果是 PDO,您只需添加 :

关于php - 使用 PHP 编辑 MySQL 条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38067953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com