我想将选择项目的用户名添加到我的数据库中。例如,如果三个用户选择该项目,我希望它像user1,user2,user3 ..我得到不同的用户进入数据库,但我不能让逗号分开..我使用爆炸但仍然无法正常工作
<?php
session_start();
$date = date("Y-m-d H:i:s");
include("php/connect.php");
if (isset($_SESSION['username'])){
$query1 = mysql_query("SELECT * FROM user WHERE username='$username'");
$username = $_SESSION['username'];
$submit = $_POST["submit"];
$tests = $_POST["test"];
// If the user submitted the form.
// Do the updating on the database.
if (!empty($submit))
{
if (count($tests) > 0)
{
foreach ($tests as $test_id => $test_value)
{
switch ($test_value)
{
case 1:
mysql_query("UPDATE test SET win = win + 1 WHERE id = '$test_id'");
mysql_query("UPDATE test SET date = '$date' WHERE id = '$test_id'");
$username = explode(",", $username);
$cnt = count($username);
$slice = array_slice($username, 0,10);
mysql_query("UPDATE test SET users = CONCAT(users, '$username') WHERE id = '$test_id'");
break;
case 'X':
mysql_query("UPDATE test SET draw = draw + 1 WHERE id = '$test_id'");
mysql_query("UPDATE test SET date = '$date' WHERE id = '$test_id'");
break;
case 2:
mysql_query("UPDATE test SET lose = lose + 1 WHERE id = '$test_id'");
mysql_query("UPDATE test SET date = '$date' WHERE id = '$test_id'");
break;
default:
// DO NO THING.
}
}
}
}
// Whenever this wiil be fetched it will be updated.
$query = "SELECT * FROM test ORDER BY `id` ASC LIMIT 3";
$result = mysql_query($query);
echo "<h2>Seria A</h2><hr/>
<br/>Welcome, ".$username."! <a href='php/logout.php'><b>LogOut</b></a><br/>";
while($row = mysql_fetch_array($result)){
$id = $row['id'];
$home = $row['home'];
$away = $row['away'];
$win = $row['win'];
$draw = $row['draw'];
$lose = $row['lose'];
echo "<br/>",$id,") " ,$home, " - ", $away;
echo "
<form action='seria.php' method='post'>
<select name='test[$id]'>
<option value=\"\">Parashiko</option>
<option value='1'>1</option>
<option value='X'>X</option>
<option value='2'>2</option>
</select>
<input type='submit' name='submit' value='Submit'/>
<br/>
</form><br/>";
echo "Totali ", $sum = $win+$lose+$draw, "<br/><hr/>";
}
}else{
$error = "<div id='hello'>Duhet te besh Log In qe te vendosesh parashikime ndeshjesh<br/><a href='php/login.php'>Kycu Ketu</a></div>";
}
?>
答案 0 :(得分:2)
修改强>
好的,这应该对你有用....我还清理了你的多个执行查询......
这将检索测试行,获取用户名,将当前用户名附加到列表中,然后重新插入....
case 1:
$sql = mysql_query("SELECT * FROM test WHERE id = '$test_id'");
$users = mysql_fetch_row($sql);
$usernames = $users['users'].",".$username;
mysql_query("UPDATE test SET users = '$usernames', win = win + 1, date = '$date' WHERE id = '$test_id'");
break;
case 'X':
mysql_query("UPDATE test SET date = '$date', draw = draw + 1 WHERE id = '$test_id'");
break;
case 2:
mysql_query("UPDATE test SET date = '$date', lose = lose + 1 WHERE id = '$test_id'");
break;
default:
只需将其插入您的switch语句
即可另外......出于安全考虑,你想要逃避用户输入的数据......
$submit = mysql_real_escape_string($_POST["submit"]);
$tests = mysql_real_escape_string($_POST["test"]);
或者更好的是,切换到使用mysqli_ *或PDO,mysql is deprecated,很快就会从PHP中删除。
答案 1 :(得分:0)
我不知道当你拿到它时用户名是什么。
如果$username
是 ARRAY ,请在执行此行之前:$username = explode(",", $username);
然后您应该执行$username = implode(",",$username);
所以如果是这样的话:
INPUT: $username = array('fred','frank','jill');
$username = implode(",",$username);
OUTPUT: fred,frank,jill