如何将多个值提取到选择表单中,然后再次将它们添加到新表中?

How to extract multiple values into select form and then add them again into new table?

提问人:Patrick KME 提问时间:1/3/2023 最后编辑:RiggsFollyPatrick KME 更新时间:1/3/2023 访问量:33

问:

//Connects to the database
$mysqli = new mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_DB);
if($mysqli->connect_errno){
    echo "Connection error " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}

if (isset($_POST['points']) & isset($_POST['user'])) {
    if(!($stmt = $mysqli->prepare("INSERT INTO penalty (user_id, first_name, last_name, name, value) VALUES (?, ?, ?, ?, ?);"))){
        echo "Prepare failed: "  . $stmt->errno . " " . $stmt->error;
    }

    if(!($stmt->bind_param("sssss",$_POST['points'],$_POST['user']))){
        echo "Bind failed: " . $stmt->errno . " " . $stmt->error;
    }

    if(!$stmt->execute()){
        echo "Execute failed: " . $stmt->errno . " " . $stmt->error;
    } else {
        $stmt->close();
        header('Location: GivePoints1.php');
        echo "Added successfully";
        exit();
    }

    $stmt->close();

}
<form class="form-horizontal" action="GivePoints1.php" method="post">

    <div class="form-group">
        <label class="control-label col-sm-2">Penalty:</label>
        <select name="points[]" id="points">
            <?php

            //Prepare SELECT statement for user's name
            if(!($stmt = $mysqli->prepare("SELECT name, value FROM points"))){
                echo "Prepare failed: "  . $stmt->errno . " " . $stmt->error;
            }

          
            //Execute the SELECT statement
            if(!$stmt->execute()){
                echo "Execute failed: "  . $mysqli->connect_errno . " " . $mysqli->connect_error;
            }

            //Bind values to variables
            if(!$stmt->bind_result($name, $value)){
                echo "Bind failed: "  . $mysqli->connect_errno . " " . $mysqli->connect_error;
            }

            //Output name
            while($stmt->fetch()){
                echo'<option value=" '.$name.' "> '.$name.' - ' . $value .' Pts </option>';
            }

            $stmt->close();
            ?>
            </select>
    </div>

    <div class="form-group">
         <label class="control-label col-sm-2">Give To:</label>
        <select name="user[]" id="user">
            <?php

            //Prepare SELECT statement for user's name
            if(!($stmt = $mysqli->prepare("SELECT id, first_name, last_name FROM award_user"))){
                echo "Prepare failed: "  . $stmt->errno . " " . $stmt->error;
            }

          
            //Execute the SELECT statement
            if(!$stmt->execute()){
                echo "Execute failed: "  . $mysqli->connect_errno . " " . $mysqli->connect_error;
            }

            //Bind values to variables
            if(!$stmt->bind_result($id, $first_name, $last_name)){
                echo "Bind failed: "  . $mysqli->connect_errno . " " . $mysqli->connect_error;
            }

            //Output name
            while($stmt->fetch()){
                echo'<option value=" '.$id.' "> '.$first_name.' ' . $last_name .' </option>';
            }

            $stmt->close();
            ?>
            </select>
    </div>

    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-lg btn-primary ">Submit</button>
        </div>
    </div>
</form>

有人可以引导我走上正确的道路吗?

我需要将这些值添加到新表中,其中 id 将user_id(从表中提取award_user id 更改为惩罚表中的user_id)

我需要将 id - 作为 user_id、first_name、last_name、名称和值放入表惩罚中。它打印选择没有问题,但不会向新表添加任何内容

我现在想做一段时间,但没有运气。我会做一些解释

PHP 数据库 表单 MySQLI

评论

1赞 ADyson 1/3/2023
为什么需要在表中插入名字和姓氏?只需插入用户 ID,就足以识别用户。您已经知道存储在用户表中的它们的详细信息。每当您想要查看组合信息时,都可以在查询中将表联接在一起。这是关系数据库设计的基本原则 - 不要重复任何数据。请改用预期的方式使用外键关系。我可以再次提出同样的建议,就您从积分表中插入的内容而言 - 即。只需将 ID 插入 penalty 即可。penalty
1赞 ADyson 1/3/2023
P.S. 顺便说一句,请把你的错误处理带入21世纪。在 (or ) 命令之前添加,这将确保 SQL 查询的错误自动正确报告给 PHP。这样,您就不需要用重复的代码使脚本混乱,以便在每个 mysqli 命令后不断检查错误。而且,您永远不应该故意回显错误数据,它很容易意外地向攻击者泄露敏感信息。mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);mysqli_connect()new mysqli()
0赞 Patrick KME 1/3/2023
@ADyson 谢谢你,我在你的帮助下把它整理好了!但是,我现在遇到了不同的问题:''''' if(!($stmt = $mysqli->prepare(“SELECT name, value FROM 惩罚 INNER JOIN award_user ON penalty.user_id = award_user.id INNER JOIN POINTS ON penalty.points_id = points.id WHERE penalty.user_id = award_user.id;”))){ ''''我需要显示仅与用户关联的结果,例如,如果用户 ID = 1,则只会向用户显示哪个 ID 为 1,依此类推。 使用该语句,我可以看到分配给所有用户的所有点。你能帮忙吗?
0赞 ADyson 1/3/2023
怎么样??WHERE penalty.user_id = 1
1赞 Patrick KME 1/3/2023
@ADyson 知道了,我添加了,id = ?引擎盖里一切都很好$stmt->bind_param("s", $_SESSION["user_id"]);

答: 暂无答案