gpt4 book ai didi

php - 用 PHP 插入

转载 作者:行者123 更新时间:2023-11-28 23:11:18 25 4
gpt4 key购买 nike

我尝试将表单中的数据插入数据库,但没有成功。我创建了一个名为“vc”的表:

CREATE TABLE `vc` (
`ID_VC` int(20) NOT NULL,
`VC_ID_VC` int(20) DEFAULT NULL,
`ID_USER` int(20) NOT NULL,
`ID_VC_STATUS` int(2) NOT NULL,
`ID_VC_VERSION` int(2) NOT NULL DEFAULT '1',
`VALUECHAIN_NAME` varchar(45) NOT NULL,
`VC_DESCRIPTION` varchar(400) NOT NULL,
`VC_CREATION_DATE` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`VC_LAST_UPDATE` timestamp NULL DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

ALTER TABLE `vc`
ADD PRIMARY KEY (`ID_VC`),
ADD KEY `FK_USER_VALUE_CHAIN_AUTHOR` (`ID_USER`),
ADD KEY `FK_VC_PARENT_TRANS` (`VC_ID_VC`),
ADD KEY `FK_VC_STATUS` (`ID_VC_STATUS`),
ADD KEY `FK_VC_VERSION` (`ID_VC_VERSION`);

我的表单正在使用其他表中的数据,我将它们显示到表单元素中。我的表单如下所示:

<?php
include 'include/dbconnect.php';
$pdo = Database::connect();
$userFields = 'SELECT ID_USER,USER_NAME,USER_SURNAME FROM user;';
$vcstatusFields ='SELECT ID_VC_STATUS, VC_STATUS_NAME FROM vc_status;';
?>
<form method="post" action="create.php">
<label>Nom</label>
<input name="name" id="name" class="form-control input-lg" type="text">
<label class="col-md-3 control-label">Description</label>
<textarea name="description" id="description" rows="3"></textarea>
<label class="col-md-3 control-label">Auteur</label>
<select name="author" id="author" class="form-control input-lg">
<?php
foreach ($pdo->query($userFields) as $row) {
echo '<option value="' .$row['ID_USER'] . '" id="' .$row['ID_USER'] . '"> ' . $row['USER_NAME'] . ' ' . $row['USER_SURNAME'] . '</option>';
}
?>
</select>
<label class="col-md-3 control-label">Statut</label>
<select name="status" id="status">
<?php
foreach ($pdo->query($vcstatusFields) as $row) {
echo '<option value="' .$row['ID_VC_STATUS'] . '" id="' .$row['ID_VC_STATUS'] . '">' . $row['VC_STATUS_NAME'] . '</option>';
}
?>
</select>
<input type="submit" value="Valider" />
</form>

最后,我必须在数据库中创建一个插入,但它似乎不起作用并且没有出现错误消息。

<?php
include 'include/dbconnect.php';
$pdo = Database::connect();

// Attempt insert query execution
try{
// create prepared statement
$vcSQL = "INSERT INTO vc (VALUECHAIN_NAME, VC_DESCRIPTION, ID_USER, ID_VC_STATUS) VALUES (:name,:description,:author,:status)";
$stmt = $pdo->prepare($vcSQL);

// bind parameters to statement
$stmt->bindParam(':name', $_REQUEST['VALUECHAIN_NAME']);
$stmt->bindParam(':description', $_REQUEST['VC_DESCRIPTION']);
$stmt->bindParam(':author', $_REQUEST['ID_USER']);
$stmt->bindParam(':status', $_REQUEST['ID_VC_STATUS']);

// execute the prepared statement
$stmt->execute();
echo "Records inserted successfully.";
}
catch(PDOException $e){
die("ERROR: Could not able to execute $sql. " . $e->getMessage());
}
// Close connection
unset($pdo);
?>

理想情况下,我想在我放置表单的同一个文件中进行成型处理......实际上(因为我开始使用 PHP 不到一周)。欢迎所有帮助。

非常感谢!

最佳答案

在您的插入中使用表单中的名称而不是 $_REQUEST['column_names']。

$stmt->bindParam(':name', $_REQUEST['name']);
$stmt->bindParam(':description', $_REQUEST['description']);
$stmt->bindParam(':author', $_REQUEST['author']);
$stmt->bindParam(':status', $_REQUEST['status']);

关于php - 用 PHP 插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45903103/

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