I'm having some issues trying to insert a query into the database. I have this php form that should do an insert query but nothing happens, not even an error.
我在尝试将查询插入数据库时遇到了一些问题。我有这个php表单应该执行插入查询但没有任何反应,甚至没有错误。
This is an example query with the form:
这是一个带有以下形式的示例查询:
INSERT INTO FlashVideoList ('title', 'urltitle', 'description', 'tags', 'category', 'filename', 'filetype', 'size', 'uploadedbyuser', 'uploadtime') VALUES ('testtitle','testtitle','test','testtags','FlashVideo','SMILE!.swf','application/x-shockwave-flash','1007525','0','1339102426');
i echoed the contents of the query to get this
为了得到这个,我回应了查询的内容
This is the php code:
这是php代码:
function MySqlQuery($Query)
{
// Invokes global connection info
global $MySQL_Host, $MySQL_Username, $MySQL_Password, $MySQL_Database, $MySQL_Port;
global $mysqli;
// runs query
$result = $mysqli->query($Query);
return $result;
}
$mysqli = new mysqli($MySQL_Host, $MySQL_Username, $MySQL_Password, $MySQL_Database, $MySQL_Port);
MySqlQuery("INSERT INTO FlashVideoList ('title', 'urltitle', 'description', 'tags', 'category', 'filename', 'filetype', 'size', 'uploadedbyuser', 'uploadtime') " .
"VALUES ('" . $mysqli->real_escape_string($_SESSION['Title']) . "','" . $mysqli->real_escape_string(GetUrlTitle()) . "','" . $mysqli->real_escape_string($_SESSION['Description']) . "','" .
$mysqli->real_escape_string($_SESSION['Tags']) . "','" . $_SESSION['Category'] . "','" . $mysqli->real_escape_string($FlashFileName) . "','" . $mysqli->real_escape_string($_FILES["file"]["type"]) . "','" .
$mysqli->real_escape_string($_FILES["file"]["size"]) . "','" . $mysqli->real_escape_string($UploadUserID) . "','" . time() . "');");
mysqli_free_result($result);
mysqli_close($mysqli);
Any help is appereciated, thanks Select queries work fine with this same code
感谢任何帮助,感谢选择查询与此相同的代码正常工作
EDIT: Alright, i've made some progress, it seems that about everything possible is wrong with this code :P so yeah this is my query now:
编辑:好吧,我已经取得了一些进展,似乎所有可能的代码都错了这个代码:P所以是的,这是我现在的查询:
INSERT INTO FlashVideoList (`title`, `urltitle`, `description`, `tags`, `category`, `filename`, `filetype`, `size`, `uploadedbyuser`, `uploadtime`) VALUES (`letitle`,`letitle`,``,`tagzz`,`FlashVideo`,`585336_pokemonsnewgrounds.swf`,`application/x-shockwave-flash`,`5058231`,`0`,`1339103842`);
And if I run it directly through navicat i get the error:
如果我直接通过navicat运行它我得到错误:
[Err] 1054 - Unknown column 'letitle' in 'field list'
Anyone know what i'm doing wrong? :/
谁知道我做错了什么? :/
3 个解决方案
#1
1
Use msysqli_error()
to help find out the error
使用msysqli_error()来帮助找出错误
eg. mysqli_query($query) or die(mysqli_error());
例如。 mysqli_query($ query)或die(mysqli_error());
#2
1
Read the error message. What error message? Well, that's another issue, but it would have said why the insert failed1.
阅读错误消息。什么错误信息?嗯,这是另一个问题,但它会说插入失败的原因1。
One (but perhaps not the only) problem as mentioned in the comments is that '
is not a valid identifier quote and thus results in a parse error. In MySQL the default is `
, but it can be changed (to "
) if using ANSI quotes.
注释中提到的一个(但可能不是唯一的)问题是'不是有效的标识符引用,因此导致解析错误。在MySQL中,默认为`,但如果使用ANSI引号,则可以更改(为“)。
INSERT INTO FlashVideoList (`title`, ...)
1Here is an example of basic error handling for mysqli (scroll to the bottom). The basic idea is, if the query
returns FALSE then something failed and error
can/should be consulted.
1这是mysqli的基本错误处理示例(滚动到底部)。基本思路是,如果查询返回FALSE,则表示失败,可以/应该查询错误。
Also, I'd recommend cleaning up the code and getting rid of the "proxy" function call.
此外,我建议清理代码并摆脱“代理”函数调用。
#3
0
Why use backticks (or single quotes) in the first place? They let you use reserved words, but that's not something I'd propagate. Use sensible column names instead.
为什么首先使用反引号(或单引号)?他们让你使用保留字,但这不是我传播的东西。请改用合理的列名。
#1
1
Use msysqli_error()
to help find out the error
使用msysqli_error()来帮助找出错误
eg. mysqli_query($query) or die(mysqli_error());
例如。 mysqli_query($ query)或die(mysqli_error());
#2
1
Read the error message. What error message? Well, that's another issue, but it would have said why the insert failed1.
阅读错误消息。什么错误信息?嗯,这是另一个问题,但它会说插入失败的原因1。
One (but perhaps not the only) problem as mentioned in the comments is that '
is not a valid identifier quote and thus results in a parse error. In MySQL the default is `
, but it can be changed (to "
) if using ANSI quotes.
注释中提到的一个(但可能不是唯一的)问题是'不是有效的标识符引用,因此导致解析错误。在MySQL中,默认为`,但如果使用ANSI引号,则可以更改(为“)。
INSERT INTO FlashVideoList (`title`, ...)
1Here is an example of basic error handling for mysqli (scroll to the bottom). The basic idea is, if the query
returns FALSE then something failed and error
can/should be consulted.
1这是mysqli的基本错误处理示例(滚动到底部)。基本思路是,如果查询返回FALSE,则表示失败,可以/应该查询错误。
Also, I'd recommend cleaning up the code and getting rid of the "proxy" function call.
此外,我建议清理代码并摆脱“代理”函数调用。
#3
0
Why use backticks (or single quotes) in the first place? They let you use reserved words, but that's not something I'd propagate. Use sensible column names instead.
为什么首先使用反引号(或单引号)?他们让你使用保留字,但这不是我传播的东西。请改用合理的列名。