【问题标题】:How to optimise my php code and get everything to work by itself?如何优化我的 php 代码并让一切自行工作?
【发布时间】:2021-02-14 14:52:07
【问题描述】:

我需要帮助来优化我构建的表单的数据处理。

使用表单页面上的 ajax 脚本将数据发送到处理页面。

这里是代码:

include('pdo-connect.php'); // This file only contains the connection to my database

$html = htmlspecialchars($_POST['HTML']);
$css = htmlspecialchars($_POST['CSS']);
$javascript = htmlspecialchars($_POST['JAVASCRIPT']);
$php = htmlspecialchars($_POST['PHP']);
$ajax = htmlspecialchars($_POST['AJAX']);
$jquery = htmlspecialchars($_POST['JQUERY']);
$responsive = htmlspecialchars($_POST['RESPONSIVE']);
$sql = htmlspecialchars($_POST['SQL']);
$composer = htmlspecialchars($_POST['COMPOSER']);
$symfony = htmlspecialchars($_POST['SYMFONY']);
$doctrine = htmlspecialchars($_POST['DOCTRINE']);
$twig = htmlspecialchars($_POST['TWIG']);
$agile = htmlspecialchars($_POST['AGILE']);
$git = htmlspecialchars($_POST['GIT']);
$python = htmlspecialchars($_POST['PYTHON']);
$seo = htmlspecialchars($_POST['SEO']);
$rgpd = htmlspecialchars($_POST['RGPD']);
$user = htmlspecialchars($_POST['USER']);

$matieres = array(
    1 => $html,
    $css,
    $javascript,
    $php,
    $ajax,
    $jquery,
    $responsive,
    $sql,
    $composer,
    $symfony,
    $doctrine,
    $twig,
    $agile,
    $git,
    $python,
    $seo,
    $rgpd
);

$insert = $bdd->prepare('INSERT INTO Resultats(ID_USER, ID_MATIERE, RESULTAT) VALUES (:user, :matiere, :resultat)');

foreach($matieres as $key => $value) {
    $insert->bindParam(':user', $user);
    $insert->bindParam(':matiere', $key);
    $insert->bindParam(':resultat', $value);
    $insert->execute();
}

$feedback = "Your results have been added";

if(isset($feedback) && !empty($feedback)) {

    echo $feedback;

}

首先,我想删除包含所有 $_POST 数据的所有已声明变量。 我需要自动获取 $_post 数据的所有变量,所以如果我在表中添加另一行,我不必在这里添加另一个变量

我想构建一个可以为我完成这项工作的函数,但我不知道如何继续,因为我尝试的所有方法都不起作用。

我需要存储的输入的每个名称都与我的数据库中的一个表中的行 ['Name'] 具有相同的名称。

所以我认为我们可以做一个简单的 SQL 查询,但我无法让它像那样工作。

(我也尝试使用 $_POST 构建一个 foreach 函数,但也没有用)

然后,一旦完成,我需要将包含用户输入的变量存储在一个数组中,就像我在上面所做的那样

注意: 我说的sql查询是这个:

$q = $bdd->query('SELECT Nom FROM Matieres WHERE Active = 1');
while($infos = $q->fetch()) {

    echo $infos['Nom'];

}

foreach 类型的作品。但是当我尝试将键和值放入数组时,事情开始变得复杂:

$matieres = array();

    foreach($_POST as $key => $value) {

        $matieres = array(
            $key => $value
        );

    }

    var_dump($matieres); // print only one key and one value

    print_r($matieres); // same here

    print($matieres); // only print the keyword "array"

同时,如果我在我的 for each 中回显每个 $post 的键和值而不将它们放入数组中,我可以获得用户的每个输入...

我可能找到了一些东西!

$matieres = array();

foreach($_POST as $key => $value) {

    array_push($matieres, $value, $key);

}

print_r($matieres); // print an array containing the name of all of the input and the value the user put in it but they all have their own keys ... :(

我终于设法让我的代码工作了!

<?php

include('pdo-connect.php');

$results = array();

foreach($_POST as $key => $value) {

    array_push($results, $value);

}

$results = array_combine(range(1, count($results)), array_values($results));

array_pop($results);

$user = $_POST['USER'];

$insert = $bdd->prepare('INSERT INTO Resultats(ID_USER, ID_MATIERE, RESULTAT) VALUES (:user, :matiere, :resultat)');

foreach($results as $key => $value) {
    $insert->bindParam(':user', $user);
    $insert->bindParam(':matiere', $key);
    $insert->bindParam(':resultat', $value);
    $insert->execute();
}
$feedback = "Votre auto-évaluation a bien été envoyé ! Vous allez 

maintenant être redirigé vers l'accueil.";

if(isset($feedback) && !empty($feedback)) {

    echo $feedback;

}

?>

【问题讨论】:

  • 我编辑了我的第一条消息,来回答你
  • 最好编辑您的问题以添加更多信息或更正,而不是添加 cmets。 PDO 也不需要 htmlspecialchars,它会为您处理转义和引用字段。
  • 这行不通,因为您创建$matieres array 的方式只有一个键,即1,并且在您尝试的foreach 上拥有的不止一个,因为每次它都会再次添加 +1 键
  • @DaveS 他发表评论是因为我问了一个问题,但后来删除了,因为这是一个愚蠢的问题,抱歉
  • 好的,我删除了 htmlspecialchars。但我仍然坚持我的阵列。我真的不明白我应该如何在 foreach 中构建她

标签: php sql arrays forms post


【解决方案1】:

关于任务:

<?php
$inputs = ['HTML', 'CSS', 'JAVASCRIPT'];
$dirty  = [];

foreach($inputs as $input) {
    $dirty[strtolower($input)] = $_POST[$input] ?? '';
}

然后,您可以使用命名占位符并使用给定数组执行语句。

$stmt = $pdo->prepare("INSERT INTO foo (html, css, javascript) VALUES (:html, :css, :javascript)");
$stmt->execute($dirty);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-18
    • 2021-06-26
    • 2020-07-27
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    • 2021-07-29
    • 2012-03-08
    相关资源
    最近更新 更多