gpt4 book ai didi

javascript - PHP MySQL JavaScript

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

请帮帮我。谢谢我有一个包含函数的 javascript 的表单,并且函数的 javascript 中包含 html 代码。

我的问题是:如何将我的表单发送到数据库中?

这是我的表格:

    <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Créer une facture</title>
<link rel="stylesheet" href="style.css" media="all" />
</head>
<body>

<form action="affiche.php" method="post">

<fieldset>
<legend>Contenu de la facture formation</legend>
<div id="ID_container">
<textarea name="prestation[]" rows="4"
placeholder="Prestation" required></textarea>
<input type="number" placeholder="Nombre de jours" name="nbjours[]" required>
<input type="number" placeholder="Tarif journalier" name="tarifjour[]" required>
</div>
<button onclick="ajout(this);">+ Ajouter une prestation</button>

<br/><br/>
<input type="submit" name="envoyer" value="Envoyer"/>
</fieldset>

</form>
<script src="js/fonct.js"></script>
</body>
</html>

这是函数 JS:

function ajout(element){
var container = document.getElementById('ID_container');

var str ='<span><textarea name="prestation[]" rows="4" type="text" placeholder="Prestation"></textarea> </span><span><input name="nbjours[]" type="number" placeholder="Nombre de jour"> </span><span><input name="tarifjour[]" type="number" placeholder="Tarif journalier"> </span><span><input type="button" onclick="suppression(this)"; value="x"></span><br/>';
var divNewExp = document.createElement("div");
divNewExp.innerHTML = str ;
container.appendChild(divNewExp);

}

function suppression(element){
var container = document.getElementById('ID_container');
container.removeChild(element.parentNode.parentNode);
}

这里我想将这些数据发送到数据库中并显示数据,但是这是行不通的,数据没有发送到数据库中:

    <?php
require_once 'connexion.php';
// On vérifie si la variable existe et sinon elle vaut NULL
$prestation = isset($_POST['prestation']) ? $_POST['prestation'] : NULL;
$nbjours = isset($_POST['nbjours']) ? $_POST['nbjours'] : NULL;
$tarifjour = isset($_POST['tarifjour']) ? $_POST['tarifjour'] : NULL;

//On récupère les différentes valeurs sous forme d'une chaine de caractères séparées par une virgule
$prestation=implode(",", $_POST['prestation']);
$nbjours=implode(",", $_POST['nbjours']);
$tarifjour=implode(",", $_POST['tarifjour']);

$req = $base->prepare('INSERT INTO facturation (prestation, nbjours, tarifjour)
VALUES ("'.$prestation.'", "'.$nbjours.'", "'.$tarifjour.'")');
$req->execute(array( 'prestation'=> $prestation,
'nbjours'=> $nbjours,
'tarifjour'=> $tarifjour));
echo "les données ont bien étés insérées dans la base de données";
$base = null;
?>

<tr>
<?= foreach((array)$req as $presta) ?>
<td class="desc"><?php echo $presta['prestation'] ?></td>
<td class="qty"><?php echo $presta['nbjours'] ?></td>
<td class="unit"><?php echo $presta['tarifjour'] ?></td>
<td class="total"><?php echo $presta['tarifjour'] * $presta['nbjours'] ?></td>
</tr>

最佳答案

您正在尝试从 INSERT 请求获取数据,但没有正确使用准备/执行。它只是不能按照你的方式工作。正如 aynber 所说,你应该阅读 PDO documentation

以下提示可以帮助您:

1。修复你的准备语句/执行

$req = $base->prepare('INSERT INTO facturation (prestation, nbjours, tarifjour) VALUES (:prestation, :nbjours, :tarifjour)');
$req->execute(array(
':prestation' => $prestation,
':nbjours' => $nbjours,
':tarifjour' => $tarifjour
));

我想您需要为每个 prestation 添加一行,而不是将每个添加的 prestation 保存在一行中,因此您必须更新查询以反射(reflect)这一点

// generate request params
$params = [];
$values = '';
foreach ($_POST['prestation'] as $key => $prestation) {
$params[':prestation' . $key] = $prestation;
$params[':nbjours' . $key] = $_POST['nbjours'][$key];
$params[':tarifjour' . $key] = $_POST['tarifjour'][$key];

$values .= '(:prestation' . $key . ', :nbjours' . $key . ', :tarifjour' . $key . '),';
}

// remove trailing ","
$values = rtrim($values, ',');
$req = $base->prepare('INSERT INTO facturation (prestation, nbjours, tarifjour) VALUES ' . $values);
// insert every rows in DB
$req->execute($params);

2。从发布的数据生成行或从数据库获取它们

在此示例中,我将仅向您展示第一个选项

// prepare facturation data
$facturation[] = [
'prestation' => $prestation,
'nbjours' => $_POST['nbjours'][$key],
'tarifjour' => $_POST['tarifjour'][$key],
];

<?php foreach ($facturation as $presta) : ?>
<tr>
<td class="desc"><?= $presta['prestation'] ?></td>
<td class="qty"><?= $presta['nbjours'] ?></td>
<td class="unit"><?= $presta['tarifjour'] ?></td>
<td class="total"><?= $presta['tarifjour'] * $presta['nbjours'] ?></td>
</tr>
<?php endforeach; ?>

3。修复按钮以防止使用 +x 按钮时提交表单

正如 Magnus Eriksson 所说,您需要在按钮元素中添加 type="button"

在您的index 文件中,使用

<button type="button" onclick="ajout(this);">+ Ajouter une prestation</button>

而不是

<button onclick="ajout(this);">+ Ajouter une prestation</button>

font.js中,使用

<button type="button" onclick="suppression(this)">x</button>

而不是

<input type="button" onclick="suppression(this)"; value="x">

4。在执行其他所有步骤之前检查是否存在所有需要的数据,以防止错误

这是我完整的工作 affiche.php 文件(没有变量检查):

<?php
require_once 'connexion.php';
// On vérifie si la variable existe et sinon elle vaut null
$prestation = isset($_POST['prestation']) ? $_POST['prestation'] : null;
$nbjours = isset($_POST['nbjours']) ? $_POST['nbjours'] : null;
$tarifjour = isset($_POST['tarifjour']) ? $_POST['tarifjour'] : null;

//On récupère les différentes valeurs sous forme d'une chaine de caractères séparées par une virgule
$prestation=implode(",", $_POST['prestation']);
$nbjours=implode(",", $_POST['nbjours']);
$tarifjour=implode(",", $_POST['tarifjour']);

// generate request params
$params = [];
$values = '';
$facturation = [];
foreach ($_POST['prestation'] as $key => $prestation) {
$params[':prestation' . $key] = $prestation;
$params[':nbjours' . $key] = $_POST['nbjours'][$key];
$params[':tarifjour' . $key] = $_POST['tarifjour'][$key];

$values .= '(:prestation' . $key . ', :nbjours' . $key . ', :tarifjour' . $key . '),';

// prepare facturation data
$facturation[] = [
'prestation' => $prestation,
'nbjours' => $_POST['nbjours'][$key],
'tarifjour' => $_POST['tarifjour'][$key],
];
}
// remove trailing ","
$values = rtrim($values, ',');
$req = $base->prepare('INSERT INTO facturation (prestation, nbjours, tarifjour) VALUES ' . $values);
$req->execute($params);
echo "les données ont bien étés insérées dans la base de données";
$base = null;
?>

<table>
<thead>
<tr>
<th>Prestation</th>
<th>Nb jours</th>
<th>Tarif /jour</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<?php foreach ($facturation as $presta) : ?>
<tr>
<td class="desc"><?= $presta['prestation'] ?></td>
<td class="qty"><?= $presta['nbjours'] ?></td>
<td class="unit"><?= $presta['tarifjour'] ?></td>
<td class="total"><?= $presta['tarifjour'] * $presta['nbjours'] ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

希望对你有帮助!

关于javascript - PHP MySQL JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49258134/

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