PHP,mysql_query 不起作用

发布于 2025-01-01 21:57:51 字数 1839 浏览 0 评论 0 原文

我准备好拔掉我的头发了...

这工作得很好:

$link = mysql_connect('localhost', 'prototype_wp_usr', 'password') or die("Error: " . mysql_error());
mysql_select_db('prototype_wp');
$query = "INSERT INTO wp_zsession_capture
        (id, session_id, user_login, first_name, last_name, user_email, phone_number, last_conviction)
        VALUES (
            'NULL', 
            '". session_id() ."',
            '". $_POST['user_login'] ."',
            '". $_POST['first_name'] ."',
            '". $_POST['last_name'] ."',
            '". $_POST['user_email'] ."',
            '". $_POST['phone_number'] ."',
            '". $_POST['last_conviction'] ."'
        )";

mysql_query($query) or die ('Error updating database: ' . mysql_error());
mysql_close($link);

这确实工作:

$link = mysql_connect('localhost', 'prototype_wp_usr', 'password') or die("Error: " . mysql_error());
mysql_select_db('prototype_wp');
$query = "UPDATE wp_zsession_capture SET removed = 1 WHERE session_id = '$sessionid'";

mysql_query($query) or die ('Error updating database: ' . mysql_error());
mysql_close($link);

这两段代码在一个表单中...如果表单中有错误,则第一个运行 sql 查询来保存当时会话中的所有内容。如果没有错误,并且在发生其他一些事情之后,将运行第二个 sql 查询来更新那些先前保存的条目中的“已删除”状态。

我一直在修改一百万种替代语法,但无法更新这些行。当我在 phpMyAdmin 中执行原始 SQL 时,它工作正常。这毫无逻辑可言……或者也许我只是太累了。

仅供参考:$sessionid = session_id();

-- 更新 --

我真的很感谢大家的关心,但让我解决一些问题:

  1. 是的,我的表中有一个名为“已删除”的列...你是认真的吗? :P
  2. 我当然使用 session_start();
  3. 是的,我正在检查 $sessionid 以确保它被调用。它始终在页面上回显,并且也在我执行的任何 die 语句中。
  4. 第一个查询负责创建第二个查询应该更新的行......所以数据就在那里。
  5. mysql_error() 不会产生任何结果...根本就没有错误。

再次感谢您的关心,但到目前为止,所有建议都已尝试过并再次尝试。

在此处输入图像描述

I'm ready to pull my hair out...

This works perfectly:

$link = mysql_connect('localhost', 'prototype_wp_usr', 'password') or die("Error: " . mysql_error());
mysql_select_db('prototype_wp');
$query = "INSERT INTO wp_zsession_capture
        (id, session_id, user_login, first_name, last_name, user_email, phone_number, last_conviction)
        VALUES (
            'NULL', 
            '". session_id() ."',
            '". $_POST['user_login'] ."',
            '". $_POST['first_name'] ."',
            '". $_POST['last_name'] ."',
            '". $_POST['user_email'] ."',
            '". $_POST['phone_number'] ."',
            '". $_POST['last_conviction'] ."'
        )";

mysql_query($query) or die ('Error updating database: ' . mysql_error());
mysql_close($link);

This does NOT work:

$link = mysql_connect('localhost', 'prototype_wp_usr', 'password') or die("Error: " . mysql_error());
mysql_select_db('prototype_wp');
$query = "UPDATE wp_zsession_capture SET removed = 1 WHERE session_id = '$sessionid'";

mysql_query($query) or die ('Error updating database: ' . mysql_error());
mysql_close($link);

These two pieces of code are within a form... if there are errors in the form, the FIRST sql query is run to save whatever is in the session at that point. If there are no errors, and after some other stuff goes on, the SECOND sql query is run to UPDATE the 'removed' status within those previously saved entries.

I have been tinkering with a million alternative syntaxes and I cannot get those rows to UPDATE. When I execute raw SQL within phpMyAdmin, it works fine. There is nothing logical about this... or maybe I'm just so fantastically tired.

FYI: $sessionid = session_id();

-- UPDATE --

I really do appreciate everyone's concern, but let me address some things:

  1. Yes, there is a column in my table named 'removed' ... are you serious? :P
  2. I am of course using session_start();
  3. Yes, I am checking the $sessionid to make sure it is invoked. It's echoed on the page at all times and is also within any die statements I execute.
  4. The FIRST query is responsible for creating the rows that the SECOND query is supposed to be updating... so the data is there.
  5. mysql_error() does not produce anything ... there's simply no error.

Again, I appreciate your concern, but so far all suggestions have been tried and tried again.

enter image description here

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

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

发布评论

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

评论(5

心碎的声音 2025-01-08 21:57:51

使用 mysql_error() 来从数据库获取错误消息,如下所示:

mysql_query($query) or die ('Error updating database: '.mysql_error());

错误可能是多种原因之一,例如用户可能没有 DELETE 权限。如果您不使用mysql_error(),则无法知道发生了什么。

编辑 session_id 列中的数据看起来很短。检查session_id返回的字符串有多长,并检查数据库中的列长度。插入对于列来说太长的数据并不是错误,但它会裁剪数据。

Use mysql_error() to obtain the error message from the database, like this:

mysql_query($query) or die ('Error updating database: '.mysql_error());

The error could be one of many things, like for example maybe the user does not have the DELETE permission. If you don't use mysql_error() there's no way to know what happened.

Edit The data in your session_id column looks suspiciously short. Check how long the strings returned by session_id are and check the column length in the database. Inserting data that is too long for a column is not an error but it does crop the data.

☆獨立☆ 2025-01-08 21:57:51

您是否尝试过检查...

  1. 调用脚本时$sessionid的值是什么?
  2. mysql返回什么错误(如果有)?

您可以通过将 die() 更改为...来实现后者。

mysql_query($query) or die (mysql_error());

Have you tried checking...

  1. What the value of $sessionid is when the script is invoked
  2. What error, if any, mysql returns?

You can do the latter by changing your die() to be...

mysql_query($query) or die (mysql_error());
浅黛梨妆こ 2025-01-08 21:57:51

不知道为什么第二个查询不执行。如果没有看到数据库表的架构,也没有发布服务器生成的错误消息的内容,那么真的很难为您提供帮助。但与此同时,我强烈建议您花点时间研究一下 SQL 注入

可能还想确保在使用 session_id(); 之前先调用 session_start()

No idea why the second query does not execute. Without seeing your database table's schema and without you posting the contents of the server's generated error message, it really is going to be difficult to assist you. In the mean time though, I highly recommend you take a moment to do some research on the dangers of SQL Injection.

Might also want to ensure you call session_start() before messing with session_id();

情话已封尘 2025-01-08 21:57:51

mysql_error() 返回空白的一个可能原因是同时打开了多个连接。我最近在将脚本传输到较新的服务器/版本的 PHP 时遇到了这个问题。尝试在您的调用中提供连接句柄,如下所示: mysql_error( $link )

这应该可以帮助您找到错误是什么,并且了解这一点将有助于我们回答您的问题。

而且,尽管您表示您并不担心注入攻击 - 您应该始终转义来自用户/可疑来源的所有数据。至少在放入数据库的每个 $_GET、$_POST、$_COOKIE、$_REQUEST 和 $_SERVER 变量周围使用 mysql_real_escape_string() 。我建议您参考小 Bobby Tables 的故事...

如果您不转义用户输入,就会发生这种情况。

( http://xkcd.com/327/ )

One possible reason mysql_error() is returning blank is that more than one connection is open at the same time. I have had this issue recently while transferring scripts onto a newer server/version of PHP. Try providing the connection handle in your call like so: mysql_error( $link )

That should help you find what the error is, and knowing that will help us answer your question.

And, although you stated you weren't concerned about injection attacks - you should ALWAYS ESCAPE ALL DATA that comes from a user/questionable source. At least use mysql_real_escape_string() around EVERY $_GET, $_POST, $_COOKIE, $_REQUEST, and $_SERVER variable you put into the database. I refer you to the story of little Bobby Tables...

This is what happens if you don't escape user input.

( http://xkcd.com/327/ )

等风来 2025-01-08 21:57:51

取消选中数据库结构中的自动增量框,然后再次选中它以保存自动增量结构,您的问题将得到解决。

时遇到这种错误

mysql_query($query) or die (mysql_error());

我在执行错误

数据库更新失败:引擎更新自动增量失败。

好像发生了这个错误,我忽略了复制

Uncheck your auto increment box into the database structure then check it again to save auto increment structure, your problem will be solved.

I got this kind of error while doing

mysql_query($query) or die (mysql_error());

Error is:

Database update failed: auto increment failed to updated by engine.

Seems like this error was occur I omitted to copy

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