PHP-SQL创建条目
所以我正在开发一个基于php和sql的小型库存管理系统。create.php无法按预期工作 代码如下:PHP-SQL创建条目,php,mysql,Php,Mysql,所以我正在开发一个基于php和sql的小型库存管理系统。create.php无法按预期工作 代码如下: <?php // Include config file require_once "config.php"; // Define variables and initialize with empty values $SKU = $Bezeichnung = $EK = $VK = $Beschreibung = ""; $SKU_err = $Bezeichnung_err
<?php
// Include config file
require_once "config.php";
// Define variables and initialize with empty values
$SKU = $Bezeichnung = $EK = $VK = $Beschreibung = "";
$SKU_err = $Bezeichnung_err = $EK_err = $VK_err = $Beschreibung_err = "";
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
// Validate SKU
$input_SKU = trim($_POST["SKU"]);
if(empty($input_SKU)){
$SKU_err = "Hier die Artikelnummer eintragen";
} else{
$SKU = $input_SKU;
}
// Validate Bezeichnung
$input_Bezeichnung = trim($_POST["Bezeichnung"]);
if(empty($input_Bezeichnung)){
$Bezeichnung_err = "Bezeichnung";
} else{
$Bezeichnung = $input_Bezeichnung;
}
// Validate EK
$input_EK = trim($_POST["EK"]);
if(empty($input_EK)){
$EK_err = "EK";
} else{
$EK = $input_EK;
}
// Validate VK
$input_VK = trim($_POST["VK"]);
if(empty($input_VK)){
$EK_err = "VK";
} else{
$EK = $input_VK;
}
// Validate Beschreibung
$input_Beschreibung = trim($_POST["Beschreibung"]);
if(empty($input_Beschreibung)){
$EK_err = "Beschreibung";
} else{
$EK = $input_Beschreibung;
}
// Check input errors before inserting in database
if(empty($SKU_err) && empty($Bezeichnung_err) && empty($EK_err) && empty($VK_err) && empty($Beschreibung_err)){
// Prepare an insert statement
$sql = "INSERT INTO mytable (SKU, Bezeichnung, EK, VK, Beschreibung) VALUES (?, ?, ?, ?, ?)";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "sssss", $param_SKU, $param_Bezeichnung, $param_EK, $param_VK, $param_Beschreibung);
// Set parameters
$param_SKU = $SKU;
$param_Bezeichnung = $Bezeichnung;
$param_EK = $EK;
$param_VK = $VK;
$param_Beschreibung = $Beschreibung;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Records created successfully. Redirect to landing page
header("location: index.php");
exit();
} else{
echo "Something went wrong. Please try again later.";
}
}
// Close statement
mysqli_stmt_close($stmt);
}
// Close connection
mysqli_close($link);
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>Create Record</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style type="text/css">
.wrapper{
width: 100%;
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>Create Record</h2>
</div>
<p>Please fill this form and submit to add employee record to the database.</p>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="form-group <?php echo (!empty($SKU_err)) ? 'has-error' : ''; ?>">
<label>Artikelnummer</label>
<input type="text" name="SKU" class="form-control" value="<?php echo $SKU; ?>">
<span class="help-block"><?php echo $SKU_err;?></span>
</div>
<div class="form-group <?php echo (!empty($Bezeichnung_err)) ? 'has-error' : ''; ?>">
<label>Bezeichnung</label>
<input type="text" name="Bezeichnung" class="form-control" value="<?php echo $Bezeichnung; ?>">
<span class="help-block"><?php echo $Bezeichnung_err;?></span>
</div>
<div class="form-group <?php echo (!empty($EK_err)) ? 'has-error' : ''; ?>">
<label>EK</label>
<input type="text" name="EK" class="form-control" value="<?php echo $EK; ?>">
<span class="help-block"><?php echo $EK_err;?></span>
</div>
<div class="form-group <?php echo (!empty($VK_err)) ? 'has-error' : ''; ?>">
<label>VK</label>
<input type="text" name="VK" class="form-control" value="<?php echo $VK; ?>">
<span class="help-block"><?php echo $VK_err;?></span>
</div>
<div class="form-group <?php echo (!empty($Beschreibung_err)) ? 'has-error' : ''; ?>">
<label>Beschreibung</label>
<input type="text" name="Beschreibung" class="form-control" value="<?php echo $Beschreibung; ?>">
<span class="help-block"><?php echo $Beschreibung_err;?></span>
</div>
<input type="submit" class="btn btn-primary" value="Submit">
<a href="index.php" class="btn btn-default">Cancel</a>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
创建记录
.包装纸{
宽度:100%;
保证金:0自动;
}
创建记录
请填写此表格并提交,以便将员工记录添加到数据库中
我已经修正了你的密码。让我们向您解释一下,错误在这一行:
mysqli_stmt_bind_param($stmt, "sss", $param_SKU, $param_Bezeichnung, $param_EK, $param_, $param_VK, $param_Beschreibung);
您正在传递6个参数,而查询只需要5个参数,您必须将其更改为
mysqli_stmt_bind_param($stmt, "sssss", $param_SKU, $param_Bezeichnung, $param_EK, $param_VK, $param_Beschreibung);
这是完整的代码
<?php
// Include config file
require_once "config.php";
// Define variables and initialize with empty values
$SKU = $Bezeichnung = $EK = $VK = $Beschreibung = "";
$SKU_err = $Bezeichnung_err = $EK_err = $VK_err = $Beschreibung_err = "";
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
// Validate SKU
$input_SKU = trim($_POST["SKU"]);
if(empty($input_SKU)){
$SKU_err = "Hier die Artikelnummer eintragen";
} else{
$SKU = $input_SKU;
}
// Validate Bezeichnung
$input_Bezeichnung = trim($_POST["Bezeichnung"]);
if(empty($input_Bezeichnung)){
$Bezeichnung_err = "Bezeichnung";
} else{
$Bezeichnung = $input_Bezeichnung;
}
// Validate EK
$input_EK = trim($_POST["EK"]);
if(empty($input_EK)){
$EK_err = "EK";
} else{
$EK = $input_EK;
}
// Validate VK
$input_VK = trim($_POST["VK"]);
if(empty($input_VK)){
$EK_err = "VK";
} else{
$EK = $input_VK;
}
// Validate Beschreibung
$input_Beschreibung = trim($_POST["Beschreibung"]);
if(empty($input_Beschreibung)){
$EK_err = "Beschreibung";
} else{
$EK = $input_Beschreibung;
}
// Check input errors before inserting in database
if(empty($SKU_err) && empty($Bezeichnung_err) && empty($EK_err) && empty($VK_err) && empty($Beschreibung_err)){
// Prepare an insert statement
$sql = "INSERT INTO mytable (SKU, Bezeichnung, EK, VK, Beschreibung) VALUES (?, ?, ?, ?, ?)";
if($stmt = mysqli_prepare($link, $sql)){
// Set parameters
$param_SKU = $SKU;
$param_Bezeichnung = $Bezeichnung;
$param_EK = $EK;
$param_VK = $VK;
$param_Beschreibung = $Beschreibung;
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "sssss", $param_SKU, $param_Bezeichnung, $param_EK, $param_VK, $param_Beschreibung);
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Records created successfully. Redirect to landing page
header("location: index.php");
exit();
} else{
echo "Something went wrong. Please try again later.";
}
}
// Close statement
mysqli_stmt_close($stmt);
}
// Close connection
mysqli_close($link);
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>Create Record</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style type="text/css">
.wrapper{
width: 100%;
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>Create Record</h2>
</div>
<p>Please fill this form and submit to add employee record to the database.</p>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="form-group <?php echo (!empty($SKU_err)) ? 'has-error' : ''; ?>">
<label>Artikelnummer</label>
<input type="text" name="SKU" class="form-control" value="<?php echo $SKU; ?>">
<span class="help-block"><?php echo $SKU_err;?></span>
</div>
<div class="form-group <?php echo (!empty($Bezeichnung_err)) ? 'has-error' : ''; ?>">
<label>Bezeichnung</label>
<input type="text" name="Bezeichnung" class="form-control" value="<?php echo $Bezeichnung; ?>">
<span class="help-block"><?php echo $Bezeichnung_err;?></span>
</div>
<div class="form-group <?php echo (!empty($EK_err)) ? 'has-error' : ''; ?>">
<label>EK</label>
<input type="text" name="EK" class="form-control" value="<?php echo $EK; ?>">
<span class="help-block"><?php echo $EK_err;?></span>
</div>
<div class="form-group <?php echo (!empty($VK_err)) ? 'has-error' : ''; ?>">
<label>VK</label>
<input type="text" name="VK" class="form-control" value="<?php echo $VK; ?>">
<span class="help-block"><?php echo $VK_err;?></span>
</div>
<div class="form-group <?php echo (!empty($Beschreibung_err)) ? 'has-error' : ''; ?>">
<label>Beschreibung</label>
<input type="text" name="Beschreibung" class="form-control" value="<?php echo $Beschreibung; ?>">
<span class="help-block"><?php echo $Beschreibung_err;?></span>
</div>
<input type="submit" class="btn btn-primary" value="Submit">
<a href="index.php" class="btn btn-default">Cancel</a>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
创建记录
.包装纸{
宽度:100%;
保证金:0自动;
}
创建记录
请填写此表格并提交,以便将员工记录添加到数据库中
您遇到了什么错误?出错了您的insert有5列,您编写的“sss”为3,并添加了7个变量。这并不意味着“出了问题”不是错误。您应该修改参数绑定并设置mysqli_stmt_bind_param($stmt,“ssss”)、$param_SKU、$param_bezeichung、$param_EK、$param_VK、$param_Beschreibung);非常感谢。但还有一个问题——EK和VK没有写入数据库,这是因为您在mysqli_stmt_bind_param()下面设置了参数。请检查以上代码,我有更新。