提问人:Patrick KME 提问时间:1/3/2023 最后编辑:RiggsFollyPatrick KME 更新时间:1/3/2023 访问量:33
如何将多个值提取到选择表单中,然后再次将它们添加到新表中?
How to extract multiple values into select form and then add them again into new table?
问:
//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、名称和值放入表惩罚中。它打印选择没有问题,但不会向新表添加任何内容
我现在想做一段时间,但没有运气。我会做一些解释
答: 暂无答案
评论
penalty
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
mysqli_connect()
new mysqli()
WHERE penalty.user_id = 1
$stmt->bind_param("s", $_SESSION["user_id"]);