数据库总是接收相同的 ID
Database always receiving same IDs
我希望这不是一个糟糕的问题。我一直在努力理解我做错了什么,但我做不到。
我对 php 和 mysql 很陌生,所以我真的很困惑...
我有这个数据库(我会附上mysqlworkbench模型截图)
我正在尝试将销售插入到销售和 print_for_sale table 中。查询似乎有效,数据显示在 phpmyadmin 中。没有错误弹出。然而,销售 table 中的销售用户始终是相同的 ID。即使我使用不同的用户登录。在 print_for_sale table 中,fk_sale_id 始终是相同的 ID。 price_print_for_sale 也总是一样的。这是不应该发生的。我究竟做错了什么?
谁能帮我?
谢谢!
这是我的 php 代码:
<?php
session_start();
$user_id = $_SESSION['user_id'];
print_r($_POST);
$id_print= $_POST['print_id'];
include('database.php');
$bought_sizes=$_POST['sizes'];
$number_of_bought_sizes= count($bought_sizes);
//header('location:index.php?area=printstore');
$sqlinsertsale = "insert into sale
(fk_sale_user,
fk_payment_id)
values(".$user_id.", 1)";
//付款尚未定义,所以我使用 1 只是为了尝试。
mysql_query($sqlinsertsale);
for($i=0; $i< $number_of_bought_sizes; $i++){
$selectmultiple = "select *
from print_has_size
inner join size on fk_size_id = size_id
inner join print on fk_print_id = print_id
where print_id =".$id_print."";
$resultmultiple = mysql_query($selectmultiple);
$linemultiple = mysql_fetch_assoc($resultmultiple);
$size_price = $linemultiple["size_price"];
$selectsale = "select *
from sale";
$resultsale = mysql_query($selectsale);
$linesale = mysql_fetch_assoc($resultsale);
$sale_id = $linesale["sale_id"];
//$sale_id = mysql_insert_id();
/*PARA CADA 1 DOS TAMNHO*/
$sqlinsertprintforsale = "insert into print_for_sale
(fk_sale_id,
price_print_for_sale)
values(".$sale_id.", ".$size_price.")";
mysql_query($sqlinsertprintforsale);
}
?>
我还将附上选择页面的屏幕截图,以便您查看标记以备不时之需。
编辑:
(我从检查用户登录的地方添加 php 代码)
<?php
session_start();
include('database.php');
$user=mysql_real_escape_string($_POST['user_login']);
$pass=mysql_real_escape_string($_POST['user_pass']);
$sql="select user_id, user_name
from user
where
user_login='".$user."'
and user_pass = MD5('".$pass."')";
echo $sql;
$result = mysql_query($sql);
$num_of_regs = mysql_num_rows($result);
echo "<br>".$num_of_regs;
if($num_of_regs!=1) {
header('location:index.php?login=done');
}
else {
$line = mysql_fetch_assoc($result);
$user_name = $line['user_name'];
$user_id = $line['user_id'];
$_SESSION['user_name'] = $user_name;
$_SESSION['user_id'] = $user_id;
header('location:index.php');
}
?>
我也做了一个注销系统。
<?php
session_start();
session_destroy();
header('location:index.php');
?>
现在我注意到销售 table 没有收到数据..
只有 print_for 特卖会。错误的数据仍然存在。相同的 ID... 为什么? :(
这是我使用代码时收到的唯一错误消息
ini_set('display_errors', 真); error_reporting(E_ALL);
已弃用:mysql_connect():mysql 扩展已弃用,将来会被删除:在 /Applications/XAMPP/xamppfiles/htdocs/printstore/database 中使用 mysqli 或 PDO。php 第 7 行
已添加
echo $_SESSION['user_id'];
这是我对用户 1 的输出:
对于用户 2:
好像没问题,识别用户1和用户2。
当我制作 "new purchase" 时 Table 促销未刷新,而 table print_for_sale 使用相同的促销 ID 刷新。始终为 3(如截图所示)
- 我删除了 phpmyadmin 中销售的每一行,然后重试。它的工作。销售 table 似乎工作正常。现在唯一的问题是 table print_for_sale 即使我使用不同的用户(它在销售中显示正常 table),它仍然显示相同的 sale_id和始终为 25 的 price_print_price)。在这种情况下,我从未选择任何花费 25 的东西。-
当你注销时,你应该销毁你的会话
我假设您有一个类似 logout.php 的注销页面。在其中你应该设置下面的行。如果您不这样做,会话值将无法更改。
<?php
session_start();
session_destroy();
?>
此外,如果您将用户 ID 存储在会话变量中,则仅关闭所有浏览器 windows 而不是重新打开并登录会对您有所帮助。
关于您未来代码生活的额外信息:为什么选择 PDOhttp://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059
试一试。您似乎得到了相同的结果,因为您从不遍历结果。还要确保您的字段名称与数据库中的字段名称完全相同。
$selectsale = "select *
from sale";
$resultsale = mysql_query($selectsale);
while($linesale = mysql_fetch_assoc($resultsale))
{
$sale_id = $linesale["sale_id"];
}
我希望这不是一个糟糕的问题。我一直在努力理解我做错了什么,但我做不到。 我对 php 和 mysql 很陌生,所以我真的很困惑...
我有这个数据库(我会附上mysqlworkbench模型截图)
我正在尝试将销售插入到销售和 print_for_sale table 中。查询似乎有效,数据显示在 phpmyadmin 中。没有错误弹出。然而,销售 table 中的销售用户始终是相同的 ID。即使我使用不同的用户登录。在 print_for_sale table 中,fk_sale_id 始终是相同的 ID。 price_print_for_sale 也总是一样的。这是不应该发生的。我究竟做错了什么? 谁能帮我? 谢谢!
这是我的 php 代码:
<?php
session_start();
$user_id = $_SESSION['user_id'];
print_r($_POST);
$id_print= $_POST['print_id'];
include('database.php');
$bought_sizes=$_POST['sizes'];
$number_of_bought_sizes= count($bought_sizes);
//header('location:index.php?area=printstore');
$sqlinsertsale = "insert into sale
(fk_sale_user,
fk_payment_id)
values(".$user_id.", 1)";
//付款尚未定义,所以我使用 1 只是为了尝试。
mysql_query($sqlinsertsale);
for($i=0; $i< $number_of_bought_sizes; $i++){
$selectmultiple = "select *
from print_has_size
inner join size on fk_size_id = size_id
inner join print on fk_print_id = print_id
where print_id =".$id_print."";
$resultmultiple = mysql_query($selectmultiple);
$linemultiple = mysql_fetch_assoc($resultmultiple);
$size_price = $linemultiple["size_price"];
$selectsale = "select *
from sale";
$resultsale = mysql_query($selectsale);
$linesale = mysql_fetch_assoc($resultsale);
$sale_id = $linesale["sale_id"];
//$sale_id = mysql_insert_id();
/*PARA CADA 1 DOS TAMNHO*/
$sqlinsertprintforsale = "insert into print_for_sale
(fk_sale_id,
price_print_for_sale)
values(".$sale_id.", ".$size_price.")";
mysql_query($sqlinsertprintforsale);
}
?>
我还将附上选择页面的屏幕截图,以便您查看标记以备不时之需。
编辑: (我从检查用户登录的地方添加 php 代码)
<?php
session_start();
include('database.php');
$user=mysql_real_escape_string($_POST['user_login']);
$pass=mysql_real_escape_string($_POST['user_pass']);
$sql="select user_id, user_name
from user
where
user_login='".$user."'
and user_pass = MD5('".$pass."')";
echo $sql;
$result = mysql_query($sql);
$num_of_regs = mysql_num_rows($result);
echo "<br>".$num_of_regs;
if($num_of_regs!=1) {
header('location:index.php?login=done');
}
else {
$line = mysql_fetch_assoc($result);
$user_name = $line['user_name'];
$user_id = $line['user_id'];
$_SESSION['user_name'] = $user_name;
$_SESSION['user_id'] = $user_id;
header('location:index.php');
}
?>
我也做了一个注销系统。
<?php
session_start();
session_destroy();
header('location:index.php');
?>
现在我注意到销售 table 没有收到数据.. 只有 print_for 特卖会。错误的数据仍然存在。相同的 ID... 为什么? :(
这是我使用代码时收到的唯一错误消息 ini_set('display_errors', 真); error_reporting(E_ALL);
已弃用:mysql_connect():mysql 扩展已弃用,将来会被删除:在 /Applications/XAMPP/xamppfiles/htdocs/printstore/database 中使用 mysqli 或 PDO。php 第 7 行
已添加
echo $_SESSION['user_id'];
这是我对用户 1 的输出:
对于用户 2:
好像没问题,识别用户1和用户2。 当我制作 "new purchase" 时 Table 促销未刷新,而 table print_for_sale 使用相同的促销 ID 刷新。始终为 3(如截图所示)
- 我删除了 phpmyadmin 中销售的每一行,然后重试。它的工作。销售 table 似乎工作正常。现在唯一的问题是 table print_for_sale 即使我使用不同的用户(它在销售中显示正常 table),它仍然显示相同的 sale_id和始终为 25 的 price_print_price)。在这种情况下,我从未选择任何花费 25 的东西。-
当你注销时,你应该销毁你的会话
我假设您有一个类似 logout.php 的注销页面。在其中你应该设置下面的行。如果您不这样做,会话值将无法更改。
<?php
session_start();
session_destroy();
?>
此外,如果您将用户 ID 存储在会话变量中,则仅关闭所有浏览器 windows 而不是重新打开并登录会对您有所帮助。
关于您未来代码生活的额外信息:为什么选择 PDOhttp://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059
试一试。您似乎得到了相同的结果,因为您从不遍历结果。还要确保您的字段名称与数据库中的字段名称完全相同。
$selectsale = "select *
from sale";
$resultsale = mysql_query($selectsale);
while($linesale = mysql_fetch_assoc($resultsale))
{
$sale_id = $linesale["sale_id"];
}