简体   繁体   English

用PHP将数据插入MySQL表时遇到问题

[英]Trouble inserting data into mysql table with php

I have a few string variables I am trying to insert them into my DB but I am having trouble because nothing is being inserted into the DB. 我有一些字符串变量,我试图将它们插入数据库中,但是由于没有任何内容插入数据库中,所以遇到了麻烦。 I know the variables are populated. 我知道变量已填充。 Since all variables are string I'm converting some of them to integers because those fields in the db table are type integer. 由于所有变量都是字符串,因此我将其中一些变量转换为整数,因为db表中的这些字段均为整数类型。 I tried assigning the mysql_query to a variable and then check to return an error but it didn't display anything. 我尝试将mysql_query分配给变量,然后检查返回错误,但未显示任何内容。 I'm a bit new at PHP so I'm not sure what's wrong with my code below. 我对PHP有点陌生,所以不确定下面的代码有什么问题。 I appreciate the help. 感谢您的帮助。

$connect = mysql_connect("localhost", "user", "pass");
if (!$connect) { die("Could not connect: ". mysql_error()); }

mysql_select_db("dbname");

mysql_query($connect,"INSERT INTO table1 (id, AU, TI, JO, VL, ISS, PB, SN, UR, DO, SP, EP, PY) VALUES ('NULL', '".$authors."', '".$title."', '".$journal."', '".(int)$volume."', '".(int)$issue."', '".$publisher."', '".$serial."', '".$url."', '".$doi."', '".(int)$startpage."', '".(int)$endpage."', '".(int)$year."')");

mysql_close($connect);

Try to debug your code, adding some more useful checks. 尝试调试代码,添加一些更有用的检查。

$link = mysql_connect("localhost", "user", "pass");
if (!$link) {
    die("Could not connect: ". mysql_error());
}

$dbSelected = mysql_select_db("dbname", $link);
if (!$dbSelected) {
    die ("Can't select db: " . mysql_error());
}

$result = mysql_query("YOUR_QUERY", $link);
if (!$result) {
    die("Invalid query: " . mysql_error());
}

ps: you may want to use mysqly::query , just because mysql_query is deprecated ps:您可能想使用mysqly :: query ,只是因为不建议使用mysql_query

ps2: you should google about SQL Injection, since your statement doesn't look secure (unless those values are escaped somewhere) PS2:您应该向Google询问有关SQL注入的问题,因为您的语句看起来并不安全(除非这些值在某处被转义了)

NOTE: I just noticed that you are using a wrong order for the parameters on mysql_query($query, $link). 注意:我只是注意到您对mysql_query($ query,$ link)上的参数使用了错误的顺序。 You have put $link as first parameter. 您已将$ link作为第一个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM