gpt4 book ai didi

php - 从 PHP 表更新 MySQL 表

转载 作者:行者123 更新时间:2023-11-29 09:53:07 25 4
gpt4 key购买 nike

这只是我的第二个项目,非常新,所以请原谅我的零碎代码。我正在尝试从 MySQL 生成一个带有附加输入列的表。我希望能够在这一列中输入一个数字,如果有意义的话,该数字将被添加到原始 MySQL 表“分数”中。由于不同的玩家参与,每次新游戏开始(不是手牌)时,该表都会被删除和创建。在牌 table 被丢弃之前,每手牌的输入最多可能发生 50 次。查看代码可能更有意义,也可能没有。应该很简单,我只是现在无法理解它。

<?php // defined variables to remove errors
$submit = 'add_scores';
$name = $message = "";
$hand = 0;
$array = $index = "";
$datatable = "cards_players"; // MySQL table name
?>

<?php
if (isset($_POST[$submit])) {
//this is where my insert statement will go
}

?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" class="formAcc">
<ul style= "text-align: center">

<article>
<li>
<label>Current Standing</label>
<?php
$datatable = "cards_data"; // MySQL table name

$sql = "SELECT * FROM ".$datatable." WHERE player IS not NULL ORDER BY id ASC";
$rs_result = $connection->query($sql);

// Check connection
if ($connection->connect_error) {
die("Connection failed: " . $connection->connect_error);
}
?>

<?php
echo '<table id="db_results">
<colgroup>
<col class="col15" />
<col class="col15" />
<col class="col15" />
<col class="col20" />
</colgroup>
<tr>
<th>Name</th>
<th>Previous Score</th>
<th>Current Score</th>
<th>This Hand</th>
</tr>';

while($row = $rs_result->fetch_assoc()) {
echo "<tr class=\"center\">
<td>". $row["player"] . "</td>
<td>". $row["previous_score"] . "</td>
<td>". $row["score"] . "</td>
<td>"//. retain_fill ('text','hand','',$hand,'0') ."
. '<input type="text" name="'.$row["player"].'" placeholder="';
if (!empty($hand)) {echo $hand. '" value="'.$hand.'"/>';
}
else {echo 0 . '" value=""/>';}
"</td>
</tr>";
}
echo "</table>";
?>
</li>
<li>
<input type="submit" name="<?php echo $submit?>" value="Input Scores" style="margin-top: 5px;"/>

目前我什至不确定如何开始为每个用户插入语句,因为我知道这将是某种循环。任何帮助表示赞赏:-)

最佳答案

感谢@kry的指导和很好的回答。需要进行 1 处更正,最终查询语句如下:第一次更正 =

while($row = $rs_result->fetch_assoc()) {
echo "<tr class=\"center\">
<td>". $row["player"] . "</td>
<td>". $row["previous_score"] . "</td>
<td>". $row["score"] . "</td>
<td>"//. retain_fill ('text','hand','',$hand,'0') ."
. '<input type="text" name="'.$row["player"].'" placeholder="';
if (!empty($hand)) {echo $hand. '" value="'.$hand.'"/>';
}
else {echo 0 . '" value=""/>';}
"</td>
</tr>";
}

应该是

while($row = $rs_result->fetch_assoc()) {

echo "<tr class=\"center\">
<td>". $row["player"] . "</td>
<td>". $row["previous_score"] . "</td>
<td>". $row["score"] . "</td>
<td>". '<input type="text" name="player['.$row['player'].']" placeholder="0" value=""/>';
}

然后数据库查询结果为:

foreach ($_POST['player'] as $player=>$hand){   
$query = "Update cards_data SET player = '$player', previous_score = '$hand' WHERE player = '$player' LIMIT 1;";
$result = mysqli_query($connection, $query);
}

关于php - 从 PHP 表更新 MySQL 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54442419/

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