某些情况:我正在尝试在php上创建一个愿望清单,该清单将所有愿望项目保存到数据库中。
我正在尝试通过for循环和数组来做到这一点。您在下面看到的代码用于wishlist.php。
代码:
<?php
session_start();
$emailadress = $_SESSION["emailadress"]; // email adress got stored through a form on the previous page
$conn = mysqli_connect('*','*','*','*'); // Yes, I did put my actual data in here instead of '*'
// the goal of this query is to retrieve the 'presentamount',
// AKA the max amount of money a user can spend on a present.
// This is done in order to remind the user of said limit.
$result1 = mysqli_query($conn, "
SELECT
presentamount
FROM
participants
WHERE
emailadress = '$emailadress'
");
$count = mysqli_fetch_array($result1, MYSQLI_NUM);
?>
//makes the amount of wish-item slots
<form action="wishlist.php" method="post">
Enter the amount of wish-items here: <input type="number" name="howmanytimes" min="1" max="20"> <br>
<input type="submit">
</form>
<?php
$howmanytimes = $_POST['howmanytimes'];
echo '<form action="verlanglijst.php" method="post">';
for ($i = 1; $i <= $howmanytimes; $i++){
echo "<input type='text' name='wishlist[]' placeholder='Enter wish-item $i here' min='1' max='20'> <br>";
}
echo "<br>Keep the presentamount in mind: $count[0]<br>";
echo '<input type="submit">';
$wishlist = $_POST['wishlist'];
//function with a for-loop with a query in it.
function forloop($howmanytimes){
for ($i = 0; $i < $howmanytimes; $i++){
$wishlisti = $wishlist[$i];
$conn = mysqli_connect('*','*','*','*');
$updatequery = mysqli_query($conn, "
UPDATE participants
SET wishlist = wishlist + '$wishlisti'
WHERE emailadress = '$emailadress'
");
}
}
//calling the function
forloop($howmanytimes);
?>
我遇到的问题是for循环内的查询不起作用(但也不会返回错误以指示出什么问题)。我只知道这里出了什么问题。请参阅代码注释以获取更多说明。
SET wishlist = wishlist + '$wishlisti'
这不是在SQL中将字符串添加在一起的方式。而是尝试使用concat。
SET wishlist = CONCAT(wishlist, '$wishlisti')