私はnbabdl_spiller
という名前のテーブルを持っています。プレイヤーテーブルがあります。データベースを編集してもコードが追加されない
spiller_id
spiller_navn (player name)
spiller_position
spiller_ligahold (player league team)
spiller_alder (player age)
spiller_lon1 (player salary year 1)
spiller_lon2
spiller_lon3.
私はうまく選手を作成することができます - しかし、フィールドspiller_id
、spiller_navn
、spiller_position
とspiller_alder
が使用されている場合、私は唯一のプレーヤーを編集することができます - 私はbdl_update.php
に多くを追加しようとすると、それを変更を加えずにbdl_index.php
に返信してください。
私は間違っていますか?私はmysqli_stmt_bind_param
について読んでみましたが、それは私を助けませんでした。
まず、ものではなく、最終的にはあなたがフィールド/変数を参照することができますbdl_index.php
1その後、働いているコード:
<?php
// Include config file
require_once 'bdl_config.php';
// Define variables and initialize with empty values
$spiller_navn = $spiller_position = $spiller_alder = "";
$spiller_navn_err = $spiller_position_err = $spiller_alder_err = "";
// Processing form data when form is submitted
if(isset($_POST["spiller_id"]) && !empty($_POST["spiller_id"])){
// Get hidden input value
$spiller_id = $_POST["spiller_id"];
// Validate Navn
$input_spiller_navn = trim($_POST["spiller_navn"]);
if(empty($input_spiller_navn)){
$spiller_navn_err = "Angiv venligst et navn.";
} elseif(!filter_var(trim($_POST["spiller_navn"]), FILTER_VALIDATE_REGEXP,
array("options"=>array("regexp"=>"/^[a-zA-Z'-.\s ]+$/")))){
$spiller_navn_err = 'Angiv venligst et gyldigt navn.';
} else{
$spiller_navn = $input_spiller_navn;
}
// Validate Position
$input_spiller_position = trim($_POST["spiller_position"]);
if(empty($input_spiller_position)){
$spiller_position_err = 'Angiv venligst en position.';
} else{
$spiller_position = $input_spiller_position;
}
// Validate Alder
$input_spiller_alder = trim($_POST["spiller_alder"]);
if(empty($input_spiller_alder)){
$spiller_alder_err = "Angiv venligst en alder";
} elseif(!ctype_digit($input_spiller_alder)){
$spiller_alder_err = 'Angiv venligst et tal';
} else{
$spiller_alder = $input_spiller_alder;
}
// Check input errors before inserting in database
if(empty($spiller_navn_err) && empty($spiller_position_err) && empty($spiller_alder_err)){
// Prepare an insert statement
$sql = "UPDATE nbabdl_spiller SET spiller_navn=?, spiller_position=?, spiller_alder=? WHERE spiller_id=?";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "ssii", $param_spiller_navn, $param_spiller_position, $param_spiller_alder, $param_spiller_id);
// Set parameters
$param_spiller_navn = $spiller_navn;
$param_spiller_position = $spiller_position;
$param_spiller_alder = $spiller_alder;
$param_spiller_id = $spiller_id;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Records updated successfully. Redirect to landing page
header("location: bdl_index.php");
exit();
} else{
echo "Ups. Noget gik galt - prøv igen.";
}
}
// Close statement
mysqli_stmt_close($stmt);
}
// Close connection
mysqli_close($link);
} else{
// Check existence of id parameter before processing further
if(isset($_GET["spiller_id"]) && !empty(trim($_GET["spiller_id"]))){
// Get URL parameter
$spiller_id = trim($_GET["spiller_id"]);
// Prepare a select statement
$sql = "SELECT * FROM nbabdl_spiller WHERE spiller_id = ?";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "i", $param_spiller_id);
// Set parameters
$param_spiller_id = $spiller_id;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
$result = mysqli_stmt_get_result($stmt);
if(mysqli_num_rows($result) == 1){
/* Fetch result row as an associative array. Since the result set contains only one row, we don't need to use while loop */
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
// Retrieve individual field value
$spiller_navn = $row["spiller_navn"];
$spiller_position = $row["spiller_position"];
$spiller_alder = $row["spiller_alder"];
} else{
// URL doesn't contain valid id. Redirect to error page
header("location: bdl_error.php");
exit();
}
} else{
echo "Ups! Noget gik galt - prøv igen.";
}
}
// Close statement
mysqli_stmt_close($stmt);
// Close connection
mysqli_close($link);
} else{
// URL doesn't contain id parameter. Redirect to error page
header("location: bdl_error.php");
exit();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Opdater spiller</title>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style type="text/css">
.wrapper{
width: 500px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="page-header">
<h2>Opdater spiller</h2>
</div>
<p>Opdater spiller her</p>
<form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
<div class="form-group <?php echo (!empty($spiller_navn_err)) ? 'has-error' : ''; ?>">
<label>Navn</label>
<input type="text" name="spiller_navn" class="form-control" value="<?php echo $spiller_navn; ?>">
<span class="help-block"><?php echo $spiller_navn_err;?></span>
</div>
<div class="form-group <?php echo (!empty($spiller_position_err)) ? 'has-error' : ''; ?>">
<label>Position</label>
<textarea name="spiller_position" class="form-control"><?php echo $spiller_position; ?></textarea>
<span class="help-block"><?php echo $spiller_position_err;?></span>
</div>
<div class="form-group <?php echo (!empty($spiller_alder_err)) ? 'has-error' : ''; ?>">
<label>Alder</label>
<input type="text" name="spiller_alder" class="form-control" value="<?php echo $spiller_alder; ?>">
<span class="help-block"><?php echo $spiller_alder_err;?></span>
</div>
<input type="hidden" name="spiller_id" value="<?php echo $spiller_id; ?>"/>
<input type="submit" class="btn btn-primary" value="Submit">
<a href="bdl_index.php" class="btn btn-default">Nulstil</a>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
そして今付加して、動作していないコードをspiller_ligahold:
<?php
// Include config file
require_once 'bdl_config.php';
// Define variables and initialize with empty values
$spiller_navn = $spiller_position = $spiller_alder = $spiller_ligahold = "";
$spiller_navn_err = $spiller_position_err = $spiller_alder_err =
$spiller_ligahold_err = "";
// Processing form data when form is submitted
if(isset($_POST["spiller_id"]) && !empty($_POST["spiller_id"])){
// Get hidden input value
$spiller_id = $_POST["spiller_id"];
// Validate Navn
$input_spiller_navn = trim($_POST["spiller_navn"]);
if(empty($input_spiller_navn)){
$spiller_navn_err = "Angiv venligst et navn.";
} elseif(!filter_var(trim($_POST["spiller_navn"]), FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z'-.\s ]+$/")))){
$spiller_navn_err = 'Angiv venligst et gyldigt navn.';
} else{
$spiller_navn = $input_spiller_navn;
}
// Validate Position
$input_spiller_position = trim($_POST["spiller_position"]);
if(empty($input_spiller_position)){
$spiller_position_err = 'Angiv venligst en position.';
} else{
$spiller_position = $input_spiller_position;
}
// Validate Alder
$input_spiller_alder = trim($_POST["spiller_alder"]);
if(empty($input_spiller_alder)){
$spiller_alder_err = "Angiv venligst en alder";
} elseif(!ctype_digit($input_spiller_alder)){
$spiller_alder_err = 'Angiv venligst et tal';
} else{
$spiller_alder = $input_spiller_alder;
}
// Validate Hold
$input_spiller_ligahold = trim($_POST["spiller_ligahold"]);
if(empty($input_spiller_ligahold)){
$spiller_ligahold_err = 'Angiv venligst et hold.';
} else{
$spiller_ligahold = $input_spiller_ligahold;
}
// Check input errors before inserting in database
if(empty($spiller_navn_err) && empty($spiller_position_err) && empty($spiller_alder_err) && empty($spiller_ligahold_err)){
// Prepare an insert statement
$sql = "UPDATE nbabdl_spiller SET spiller_navn=?, spiller_position=?, spiller_alder=?, spiller_ligahold=? WHERE spiller_id=?";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "ssiis", $param_spiller_navn, $param_spiller_position, $param_spiller_alder, $param_spiller_id, $param_spiller_ligahold);
// Set parameters
$param_spiller_navn = $spiller_navn;
$param_spiller_position = $spiller_position;
$param_spiller_alder = $spiller_alder;
$param_spiller_id = $spiller_id;
$param_spiller_ligahold = $spiller_ligahold;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Records updated successfully. Redirect to landing page
header("location: bdl_index.php");
exit();
} else{
echo "Ups. Noget gik galt - prøv igen.";
}
}
// Close statement
mysqli_stmt_close($stmt);
}
// Close connection
mysqli_close($link);
} else{
// Check existence of id parameter before processing further
if(isset($_GET["spiller_id"]) && !empty(trim($_GET["spiller_id"]))){
// Get URL parameter
$spiller_id = trim($_GET["spiller_id"]);
// Prepare a select statement
$sql = "SELECT * FROM nbabdl_spiller WHERE spiller_id = ?";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "i", $param_spiller_id);
// Set parameters
$param_spiller_id = $spiller_id;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
$result = mysqli_stmt_get_result($stmt);
if(mysqli_num_rows($result) == 1){
/* Fetch result row as an associative array. Since the result set contains only one row, we don't need to use while loop */
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
// Retrieve individual field value
$spiller_navn = $row["spiller_navn"];
$spiller_position = $row["spiller_position"];
$spiller_alder = $row["spiller_alder"];
$spiller_ligahold = $row["spiller_ligahold"];
} else{
// URL doesn't contain valid id. Redirect to error page
header("location: bdl_error.php");
exit();
}
} else{
echo "Ups! Noget gik galt - prøv igen.";
}
}
// Close statement
mysqli_stmt_close($stmt);
// Close connection
mysqli_close($link);
} else{
// URL doesn't contain id parameter. Redirect to error page
header("location: bdl_error.php");
exit();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Opdater spiller</title>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style type="text/css">
.wrapper{
width: 500px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="page-header">
<h2>Opdater spiller</h2>
</div>
<p>Opdater spiller her</p>
<form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
<div class="form-group <?php echo (!empty($spiller_navn_err)) ? 'has-error' : ''; ?>">
<label>Navn</label>
<input type="text" name="spiller_navn" class="form-control" value="<?php echo $spiller_navn; ?>">
<span class="help-block"><?php echo $spiller_navn_err;?></span>
</div>
<div class="form-group <?php echo (!empty($spiller_position_err)) ? 'has-error' : ''; ?>">
<label>Position</label>
<textarea name="spiller_position" class="form-control"><?php echo $spiller_position; ?></textarea>
<span class="help-block"><?php echo $spiller_position_err;?></span>
</div>
<div class="form-group <?php echo (!empty($spiller_alder_err)) ? 'has-error' : ''; ?>">
<label>Alder</label>
<input type="text" name="spiller_alder" class="form-control" value="<?php echo $spiller_alder; ?>">
<span class="help-block"><?php echo $spiller_alder_err;?></span>
</div>
<div class="form-group <?php echo (!empty($spiller_ligahold_err)) ? 'has-error' : ''; ?>">
<label>Tilhører</label>
<input type="text" name="spiller_ligahold" class="form-control" value="<?php echo $spiller_ligahold; ?>">
<span class="help-block"><?php echo $spiller_ligahold_err;?></span>
</div>
<input type="hidden" name="spiller_id" value="<?php echo $spiller_id; ?>"/>
<input type="submit" class="btn btn-primary" value="Submit">
<a href="bdl_index.php" class="btn btn-default">Tilbage</a>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
bdl_index.php:
<?php
echo "<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<title>Dashboard</title>
<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css'>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js'>
</script>
<script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.js'></script>
<style type='text/css'>
.wrapper{
width: 650px;
margin: 0 auto;
}
.page-header h2{
margin-top: 0;
}
table tr td:last-child a{
margin-right: 15px;
}
</style>
<script type='text/javascript'>
$(document).ready(function(){
$('[data-toggle='tooltip']').tooltip();
});
</script>
</head>
<body>
<div class='wrapper'>
<div class='container-fluid'>
<div class='row'>
<div class='col-md-12'>
<div class='page-header clearfix'>
<h2 class='pull-left'>NBABDL SPILLERE</h2>
<a href='bdl_create.php' class='btn btn-success pull-right'>Tilføj ny spiller</a>
</div>";
?>
<?php
// Include config file
require_once 'bdl_config.php';
// Attempt select query execution
$sql = "SELECT * FROM nbabdl_spiller ORDER BY spiller_navn";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table class='table table-bordered table-striped'>";
echo "<thead>";
echo "<tr>";
echo "<th>Navn</th>";
echo "<th>Pos</th>";
echo "<th>Alder</th>";
echo "<th>Tilhører</th>";
echo "<th>17-18</th>";
echo "<th>18-19</th>";
echo "<th>19-20</th>";
echo "<th>Rediger</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['spiller_navn'] . "</td>";
echo "<td>" . $row['spiller_position'] . "</td>";
echo "<td>" . $row['spiller_alder'] . "</td>";
echo "<td>" . $row['spiller_ligahold'] . "</td>";
echo "<td>" . number_format($row['spiller_lon1']) . "</td>";
echo "<td>" . number_format($row['spiller_lon2']) . "</td>";
echo "<td>" . number_format($row['spiller_lon3']) . "</td>";
echo "<td>";
echo "<a href='bdl_read.php?spiller_id=". $row['spiller_id'] ."' title='Vis spiller' data-toggle='tooltip'><span class='glyphicon glyphicon-eye-open'></span></a>";
echo "<a href='bdl_update.php?spiller_id=". $row['spiller_id'] ."' title='Opdater spiller' data-toggle='tooltip'><span class='glyphicon glyphicon-pencil'></span></a>";
echo "<a href='bdl_delete.php?spiller_id=". $row['spiller_id'] ."' title='Slet spiller' data-toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "<p class='lead'><em>Ingen nye spillere fundet.</em></p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
</div>
</div>
</div>
</div>
</body>
</html>
だとかなりまともな量のコード。実際の問題に関係のないものはすべて取り除く必要があります。問題コードがどこにあるのかを試してみるために、多くの人々がそれをすべて通過することはありません。現在、「Where's Waldo」のコードバージョンです。 –
貼り付けたコードの多くは、この質問とは関係ありません。適切なコードを整理してください。また、すべてのことをエコーしないでください。それは維持することが難しいだけの混乱です。必要に応じて生のHTMLを使用し、サーバーでphpを使用しますが、クライアント(javascript)を使用して必要なデータを取得して投稿することが好ましいです。 – Jonast92
ようこそスタックオーバーフロー - あなたを持っていいです。 [良い質問をするにはどうすればいいですか?](https://stackoverflow.com/help/how-to-ask)と[最小限の完全かつ検証可能なサンプルを作成する方法](https:// stackoverflow。 com/help/mcve)を使用して、Stack Overflowsのコンテンツを可能な限り高いレベルに保ち、適切な答えを得る機会を増やします。 – Axel