我是php的新手,遇到了一个问题,我不知道如何修复它。 我正在尝试创建一个简单的系统,在按下按钮(是/否)后,数据库中的某些值正在发生变化。不幸的是我的$ _SESSION和$ _POST似乎不合作我并返回空值。 这是代码(我删除了包括密码在内的行等):
<?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 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>
答案 0 :(得分:1)
<强> INFOS 强>
<强> 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>
表格操作:
<?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());
}
?>