0

I want to add the username that select an item to my db . for example if three users selected that item i want it to be like user1,user2,user3 .. im getting the different users to go into db but i cant get the comma to separate .. im using explode but still not working properly

<?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>";

    }

 ?>
4

2 回答 2

2

EDIT

Ok, this should work for you....Iv also cleaned up some of your multiple executing queries...

This retrieves the test row, gets the usernames, attaches the current username to the list, and re-inserts....

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:

Just insert this into your switch statement

Also...for security purposes you will wanna escape the user entered data...

$submit = mysql_real_escape_string($_POST["submit"]);
$tests = mysql_real_escape_string($_POST["test"]);

Or better yet, switch to using mysqli_* or PDO, as mysql is deprecated and will soon be removed from PHP altogether.

于 2013-06-17T04:31:11.583 回答
0

当你得到它时,我不确定用户名是什么。

If $usernameis an ARRAY before you do this line:$username = explode(",", $username);那么你应该做$username = implode(",",$username);

所以如果它是这样的:

INPUT: $username = array('fred','frank','jill');

$username = implode(",",$username);

OUTPUT: fred,frank,jill
于 2013-06-17T04:28:22.963 回答