我的php / mysql UPDATE查询无法到达我的数据库时遇到问题。我知道这可能有一个简单的修复,我似乎无法找到我的错误,这是我的代码:
这是我用来发送数据的表单:
<?php
if(!($stmt = $mysqli->prepare("SELECT bowl_games.id, bowl_games.name, stadiums.name, bowl_games.inaugural_year FROM bowl_games
INNER JOIN stadiums ON stadiums.id = bowl_games.stadium_id
WHERE bowl_games.id = ? "))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!($stmt->bind_param("i", $_POST['bowl_game']))){
echo "Bind failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->bind_result($id, $name, $stadium, $inauguralyear)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
while($stmt->fetch()){
}
?>
<div class="container">
<form method="post" action="update_bowl_game_2.php">
<fieldset> <legend>Update Bowl Game</legend>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Name</label>
<div class="col-sm-10">
<input type="text", class="form-control", name="Name", value="<?php echo $name?>"/>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Stadium</label>
<div class="col-sm-10">
<select name="Stadium">
<?php
if(!($stmt = $mysqli->prepare("SELECT id, name FROM stadiums ORDER BY name"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->bind_result($id, $sname)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
while($stmt->fetch()){
if($sname === $stadium){
echo "<option value=\"" . $id . "\" selected>" . $sname . "</option>";
} else {
echo "<option value=\"" . $id . "\">" . $sname . "</option>";
}
}
$stmt->close();
?>
</select>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Inaugural Year</label>
<div class="col-sm-10">
<input type="number", class="form-control", name="InauguralYear", value="<?php echo $inauguralyear?>"/>
</div>
</div>
<input type="hidden" name="id" value="<?php echo $id?>"/>
<div class="form-group row">
<div class="offset-sm-2 col-sm-10">
<button type="submit" class="btn btn-primary">Update Bowl Game</button>
</div>
</div>
</fieldset>
</form>
</div>
<?php
$mysqli = "SELECT bowl_games.id, bowl_games.name, stadiums.name, bowl_games.inaugural_year FROM bowl_games
INNER JOIN stadiums ON stadiums.id = bowl_games.stadium_id"
?>
这是应该更新数据库中条目的PHP代码:
<?php
//Turn on error reporting
ini_set('display_errors', 'On');
//Connects to the database
$mysqli = new mysqli("oniddb.cws.oregonstate.edu","dejarnen-db","*hidden*","dejarnen-db");
if(!$mysqli || $mysqli->connect_errno){
echo "Connection error " . $mysqli->connect_errno . " " . $mysqli- >connect_error;
}
if(!($stmt = $mysqli->prepare("UPDATE bowl_games SET name=?, stadium_id=?, inaugural_year=? WHERE id= ?"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!($stmt- >bind_param("siii",$_POST['Name'],$_POST['Stadium'],$_POST['InauguralYear'],$_POST['id']))){
echo "Bind failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $stmt->errno . " " . $stmt->error;
} else {
echo "Updated " . $stmt->affected_rows . " rows in bowl games.";
}
?>
当我提交表单时,如果所选条目已成功更新,我应该看到消息&#34;在碗游戏中更新了1行。&#34;相反,我得到了消息&#34;在碗游戏中更新了0行。&#34;
任何人都可以指出我正朝这个问题迈进的方向吗?谢谢
答案 0 :(得分:1)
在表单中,您将名为"use strict";
var fs = require("fs");
var path = require("path");
var Sequelize = require('sequelize')
, sequelize = new Sequelize(process.env.MYSQL_DB, process.env.MYSQL_USER, process.env.MYSQL_PASSWORD, {
dialect: "mysql", // or 'sqlite', 'postgres', 'mariadb'
port: 3306, // or 5432 (for postgres)
});
var db = {};
fs
.readdirSync(__dirname)
.filter(function(file) {
return (file.indexOf(".") !== 0) && (file !== "index.js");
})
.forEach(function(file) {
var model = sequelize.import(path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(function(modelName) {
if ("associate" in db[modelName]) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
的变量用于两个不同的目的:
首先,您将游戏的ID检索到变量中,然后将体育场的ID检索到变量中,然后使用该变量为游戏ID创建隐藏的输入。
当你编写隐藏的输入时,$ id变量包含一个体育场的id。
一种可能的解决方案:列出体育馆时,请使用单独的变量名称:
$id
通常,使用特定的变量名称是个好主意。因此,使用&#34; $ stadium_name&#34;而不是&#34; $ name&#34;等等。该规则的唯一例外是当函数中的局部变量非常短时。
另一种可能的解决方案是在填充体育场馆的选择之前,先写下隐藏的输入。
答案 1 :(得分:-1)
首先检查$_POST
数据不是null
,然后使用绑定参数sssi
。
//example
if(!($stmt- >bind_param("sssi", $_POST['Name'], $_POST['Stadium'], $_POST['InauguralYear'], $_POST['id']))){
echo "Bind failed: " . $stmt->errno . " " . $stmt->error;