PHP,mysql_query 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9092159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP, mysql_query just NOT working
提问by dcolumbus
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 NOTwork:
但这不工作:
$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.
这两段代码在一个表单中……如果表单中有错误,则运行 FIRST sql 查询以保存当时会话中的任何内容。如果没有错误,并且在执行其他一些操作之后,将运行 SECOND sql 查询以更新那些先前保存的条目中的“已删除”状态。
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.
我一直在修补一百万种替代语法,但我无法将这些行更新到 UPDATE。当我在 phpMyAdmin 中执行原始 SQL 时,它工作正常。这没什么合乎逻辑的……或者也许我只是太累了。
FYI: $sessionid = session_id();
仅供参考:$sessionid = session_id();
-- UPDATE--
--更新--
I really do appreciate everyone's concern, but let me address some things:
我真的很感谢大家的关心,但让我解决一些问题:
- Yes, there is a column in my table named 'removed' ... are you serious? :P
- I am of course using session_start();
- 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. - The FIRST query is responsible for creating the rows that the SECOND query is supposed to be updating... so the data is there.
mysql_error()
does not produce anything ... there's simply no error.
- 是的,我的表中有一个名为“removed”的列……你是认真的吗?:P
- 我当然使用 session_start();
- 是的,我正在检查 $sessionid 以确保它被调用。它始终在页面上回显,也在
die
我执行的任何语句中。 - 第一个查询负责创建第二个查询应该更新的行......所以数据就在那里。
mysql_error()
不会产生任何东西......根本没有错误。
Again, I appreciate your concern, but so far all suggestions have been tried and tried again.
再次感谢您的关注,但到目前为止,所有建议都已反复尝试。
回答by Joni
Use mysql_error()
to obtain the error message from the database, like this:
用于mysql_error()
从数据库中获取错误信息,如下所示:
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.
错误可能是许多事情之一,例如可能用户没有 DELETE 权限。如果您不使用,mysql_error()
则无法知道发生了什么。
EditThe 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.
编辑session_id 列中的数据看起来很短。检查返回的字符串有多长,session_id
并检查数据库中的列长度。插入对于列来说太长的数据不是错误,但会裁剪数据。
回答by Amber
Have you tried checking...
你有没有试过检查...
- What the value of
$sessionid
is when the script is invoked - What error, if any, mysql returns?
$sessionid
调用脚本时的值是什么- mysql 返回什么错误(如果有)?
You can do the latter by changing your die()
to be...
你可以通过改变你的方式来做后者die()
......
mysql_query($query) or die (mysql_error());
回答by Authman Apatira
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 highlyrecommend you take a moment to do some research on the dangers of SQL Injection.
不知道为什么第二个查询不执行。没有看到您的数据库表的架构,也没有发布服务器生成的错误消息的内容,真的很难为您提供帮助。同时,我强烈建议您花点时间研究SQL 注入的危险性。
Might also want to ensure you call session_start()
before messing with session_id();
可能还想确保你session_start()
在搞乱之前先打电话session_id();
回答by Steve
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 )
mysql_error() 返回空白的一种可能原因是同时打开了多个连接。我最近在将脚本传输到更新的服务器/PHP 版本时遇到了这个问题。尝试在您的调用中提供连接句柄,如下所示: 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...
而且,尽管您表示您不担心注入攻击 - 您应该始终转义来自用户/可疑来源的所有数据。至少在您放入数据库的每个 $_GET、$_POST、$_COOKIE、$_REQUEST 和 $_SERVER 变量周围使用 mysql_real_escape_string()。我向你推荐小鲍比桌的故事......
回答by Mehran
Uncheck your Auto increament box into the database structure then check it again to save auto increament structure, your problem will be solved. I got this kind of error while doing
在数据库结构中取消选中您的自动增量框,然后再次选中它以保存自动增量结构,您的问题将得到解决。我在做的时候遇到了这种错误
mysql_query($query) or die (mysql_error());
mysql_query($query) 或死 (mysql_error());
Error Was
错误是
database update failed: auto increatment failed to updated by Engine.
数据库更新失败:引擎更新自动创建失败。
Seems like this error was occur i omitted to copy
似乎发生了这个错误,我省略了复制