My SQL 在没有 mysql_pconnect 的情况下通过 PHP 使用临时表

发布于 2024-08-06 22:12:01 字数 309 浏览 5 评论 0原文

我想在我的 PHP 代码中使用临时表。这是一份将邮寄的表格。我确实使用会话变量和数组,但填写的一些数据必须以表格格式存储,并且用户必须能够在出现拼写错误等情况下删除条目。使用数组执行此操作可以工作(不确定),但我有点新在 php 和使用表似乎要简单得多。我的问题是,使用 mysql_connect 创建表并添加数据行,但是当我添加第二行时,它会删除表并再次创建它...使用 mysql_pconnect 的工作原理是不删除表,但会创建比表实例更多的内容次并删除条目?真是一团糟!如何最好地使用临时表并且在页面刷新时不删除​​它们?如果用户关闭页面并将表保留在数据库中,则不使用临时表可能会导致其他问题。

I want to use temporary tables in my PHP code. It is a form that will be mailed. I do use session variables and arrays but some data filled in must be stored in a table format and the user must be able to delete entries in case of typos etc. doing this with arrays could work (not sure) but I'm kinda new at the php and using tables seems so much simpler. My problem is that using mysql_connect creates the table and adds the line of data but when i add my 2nd line it drops table and create it again... Using mysql_pconnect works by not dropping the table but creates more than on instance of the table at times and deleting entry's? what a mess! How can I best use temporary tables and not have them droped when my page refreshes? not using temporary tables may cause other issues if the user closes the page and leaving the table in the database.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲娇萝莉攻 2024-08-13 22:12:01

听起来很乱!我根本不知道为什么您要使用临时表,但您可以创建一个随机表名称并将其分配给会话变量。但这是非常错误的,因为您将为每个用户都有一个表!

如果必须使用数据库,请向名为 sessionID 的表中添加字段。当您插入/删除引用 php sessionid 时。

不过,仅将数据存储在会话中可能会容易得多......

Sounds like a mess! I am not sure why you are using a temp table at all, but you could create a random table name and assign it to a session variable. But this is hugely wrong as you would have a table for each user!

If you must use a database, add field to the table called sessionID. When you do your inserting/deleting reference the php sessionid.

Just storing the data in the session would probably be much easier though...

半葬歌 2024-08-13 22:12:01

最好创建一个永久表和临时行。因此,假设您已将保存所有半完整表单数据的对象序列化为 $form_data。在脚本结束时,最后应该发生的事情是将 $form_date 存储到数据库中,并将结果行 id 存储在 $_SESSION 中。类似于:

$form_data=serialize($form_object); //you can also serialize arrays, etc.
//$form_data may also need to be base64_encoded

$q="INSERT INTO incomplete_form_table(thedata) '$form_data'";    
$r=mysqli->query($q);
$id=$mysql->last_insert_id;
$_SESSION['currentform']=$id;

然后,当您到达下一页时,您可以像这样重新构建表单:

$q="SELECT thedata FROM incomplete_form_table WHERE id={$_SESSION['currentform']}";
$r=$mysql->query($q);
$form_data=$r->fetch_assoc();
$form=$form_data['thedata'];//if it was base64_encoded, don't forget to unencode it first

如果表具有时间戳字段,您可以定期(自动)清理 incomplete_form_data 表。只需删除您认为过期的所有内容即可。

上述代码尚未彻底检查语法错误。

Better to create a permanent table and temporary rows. So, say you've serialized the object holding all your semi-complete form data as $form_data. At the end of the script, the last thing that should happen is that $form_date should be stored to the database and the resulting row id be stored in your $_SESSION. Something like:

$form_data=serialize($form_object); //you can also serialize arrays, etc.
//$form_data may also need to be base64_encoded

$q="INSERT INTO incomplete_form_table(thedata) '$form_data'";    
$r=mysqli->query($q);
$id=$mysql->last_insert_id;
$_SESSION['currentform']=$id;

Then, when you get to the next page, you reconstitute your form like this:

$q="SELECT thedata FROM incomplete_form_table WHERE id={$_SESSION['currentform']}";
$r=$mysql->query($q);
$form_data=$r->fetch_assoc();
$form=$form_data['thedata'];//if it was base64_encoded, don't forget to unencode it first

You can (auto-) clean up the incomplete_form_data table periodically if the table has a timestamp field. Just delete everything that you consider expired.

The above code has not been exhaustively checked for syntax errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文