0

I'm kind of newbie in php and encountered a problem which I have no clue how to fix it. I am trying to make a simple system, in which after pressing button (yes/no) some values in database are changing. Unfortunately my $_SESSION and $_POST seems to don't cooperate with me and returns null values. Here's the code(I've deleted lines including passwords and so on):

<?php
session_start();
echo get_defined_vars();
echo var_dump($_session);
echo var_dump($_POST);
if(!isset($_SESSION['zalogowany'])) {
    $_SESSION['text'] = 'Nie jesteś zalogowany!';
    header('Location: login.php');
    exit();
}
if(isset($_POST['Submit_Yes']))
{
    $id = $_SESSION['id'];
    $link = mysql_connect($host, $database_username, $database_password);
    mysql_select_db($database, $link);
    $sql = 'UPDATE Tickets SET Głosy=Głosy+1 WHERE ID_Ticketu=$id';
    $result = mysql_query($sql, $link);
}
if(isset($_POST['Submit_No'])){
    $id = $_SESSION['id'];
    $link = mysql_connect($host, $database_username, $database_password);
    mysql_select_db($database, $link);
    $sql = 'UPDATE Tickets SET Głosy=Głosy-1 WHERE ID_Ticketu=$id';
    $result = mysql_query($sql, $link);
}
?>

EDITED: FORM code:

  <Form Name=single_ticket_form Method="POST" ACTION="ticket_vote.php">
  <?php         
  mysql_select_db($database, $link);
  $sql = "Select * from Tickets WHERE ID_Ticketu=$id";
  $result = mysql_query($sql, $link);
  $row = mysql_fetch_array($result);
  echo '<span>Nick:<p class=ticket_result>' . $row['Nazwa'] . '</p></span>';
  echo '<span>Imię i Nazwisko postaci:<p class=ticket_result>' . $row['Imię_Nazwisko'] . '</p></span>';
  echo '<span>Dział:<p class=ticket_result>' . $row['Dział'] . '</p></span>';
  echo '<span>Treść:<p class=ticket_result_msg>' . $row['Treść'] . '</p></span>';
  echo '<INPUT class=button_yes TYPE = "Submit" Name = "Submit_yes" value="Tak">';
  echo '<INPUT class=button_no TYPE = "Submit" Name = "Submit_no" value="Nie">';
  $_SESSION['id'] = $id;
  }
  ?>
  </Form>
4
  • 2
    You don't need an echo in front of var_dump(). Commented Mar 13, 2015 at 11:57
  • Also, we need to see the form Commented Mar 13, 2015 at 11:58
  • Where do you set these session and post values? Commented Mar 13, 2015 at 11:58
  • you need to write $_session in uppercase on line 4 Commented Mar 13, 2015 at 12:01

1 Answer 1

1

INFOS

  • get_defined_vars return @array => 'echo' => 'var_dump'
  • $_SESSION return @array => no need extra 'echo' before 'var_dump'
  • $_POST return @array => no need extra 'echo' before 'var_dump'
  • $_session => $_SESSION
  • In html form, name of your submit is 'Submit_yes' and in your condition 'isset' you wrote 'Submit_Yes'
  • in your line $sql, you put simple quote instead of double quote, so the variable inside '$id' wasn't interpreted.

FORM:

<form name="single_ticket_form" method="POST" action="ticket_vote.php">
<?php
    $_SESSION['id'] = $id;
    mysql_select_db($database, $link) or die(mysql_error());
    $sql = "SELECT * FROM Tickets WHERE ID_Ticketu = $_SESSION['id']";
    $result = mysql_query($sql, $link) or die(mysql_error());
    $row = mysql_fetch_array($result);
    if ($row)
    {
        var_dump($row);
?>
        <span>Nick:<p class="ticket_result"><?php echo $row['Nazwa']; ?></p></span>';
        <span>Imię i Nazwisko postaci:<p class="ticket_result"><?php echo $row['Imię_Nazwisko']; ?></p></span>
        <span>Dział:<p class=ticket_result><?php echo $row['Dział'] ?></p></span>
        <span>Treść:<p class=ticket_result_msg><?php echo $row['Treść'] ?></p></span>
        <input type="submit" class="button_yes" name="Submit_yes" value="Tak" />
        <input type="submit" class="button_no" name="Submit_no" value="Nie" />
<?php
    }
    else
    {
        echo "Wrong id";
    }
?>
</form>

FORM ACTION:

<?php
    session_start();

    var_dump(get_defined_vars());
    var_dump($_SESSION);
    var_dump($_POST);

    if(!isset($_SESSION['zalogowany']))
    {
        $_SESSION['text'] = 'Nie jesteś zalogowany!';
        header('Location: login.php');
        exit();
    }

    if(isset($_POST['Submit_yes']) || isset($_POST['Submit_no']))
    {
        $id = $_SESSION['id'];
        $link = mysql_connect($host, $database_username, $database_password) or die(mysql_error());
        mysql_select_db($database, $link) or die(mysql_error());
        if (isset($_POST['Submit_yes']))
            $sql = "UPDATE Tickets SET Głosy=Głosy+1 WHERE ID_Ticketu=$id";
        else
            $sql = "UPDATE Tickets SET Głosy=Głosy-1 WHERE ID_Ticketu=$id";
        $result = mysql_query($sql, $link) or die(mysql_error());
    }
?>
Sign up to request clarification or add additional context in comments.

5 Comments

After changing lines which you have posted, var_dump($_SESSION) gives me proper values, but still value in database is not changing. Any idea about this?
Check my edit, in your line $sql ('UPDATE Tickets SET Głosy=Głosy+1 WHERE ID_Ticketu=$id'), you put simple quote instead of double quote, so the variable inside '$id' wasn't interpreted.
And so I have changed it earlier. I replaced my code with the one you've posted and still column "Głosy" is not changing.
It can be your charset encoding database (Głosy have a special character >> ł <<)
Thanks anyway for the help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.