admin管理员组文章数量:1302407
I am trying to insert data into a table, getting errors, I have tried messaging the data, but cannot seem to get it to work, can you please help me figure out what I did wrong, please...
Here is my code I execute after validation is done:
$sql = 'INSERT INTO `tablename` (`file`,`afile`,`type`,`keyword`,`location`,`numbl`,`userul`,`uldate`) values (%s,%s,%s,%s,%s,%s,%s,%d) "'. $_REQUEST['filename'] . '", "' . $url . '", "' . $_REQUEST['filetype'] . '", "' . $_REQUEST['filekeyword'] . '", "' . $_REQUEST['filelocation'] . '", "' . $_REQUEST['filenumbl'] . '", "' . $user_login . '", "' . time() . '"';
$wpdb->query($sql);
$_tbInsertId = $wpdb->insert_id;
It is not working. I added the show errors code and it says this:
WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s,%s,%s,%s,%s,%s,%s,%d) "LK-Contractor-NOGglRev-16Ld-082214.csv"' at line 1]
I even tried putting a comma AFTER the closing ) in values... but same error. I added "quote" marks around all of the fields, that did not fix it either.
Do you see what is causing it? Thanks, Richard
I am trying to insert data into a table, getting errors, I have tried messaging the data, but cannot seem to get it to work, can you please help me figure out what I did wrong, please...
Here is my code I execute after validation is done:
$sql = 'INSERT INTO `tablename` (`file`,`afile`,`type`,`keyword`,`location`,`numbl`,`userul`,`uldate`) values (%s,%s,%s,%s,%s,%s,%s,%d) "'. $_REQUEST['filename'] . '", "' . $url . '", "' . $_REQUEST['filetype'] . '", "' . $_REQUEST['filekeyword'] . '", "' . $_REQUEST['filelocation'] . '", "' . $_REQUEST['filenumbl'] . '", "' . $user_login . '", "' . time() . '"';
$wpdb->query($sql);
$_tbInsertId = $wpdb->insert_id;
It is not working. I added the show errors code and it says this:
WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s,%s,%s,%s,%s,%s,%s,%d) "LK-Contractor-NOGglRev-16Ld-082214.csv"' at line 1]
I even tried putting a comma AFTER the closing ) in values... but same error. I added "quote" marks around all of the fields, that did not fix it either.
Do you see what is causing it? Thanks, Richard
Share Improve this question asked Sep 13, 2014 at 17:44 Richard JonesRichard Jones 112 bronze badges 02 Answers
Reset to default 1I found the answer, in the codex. I just used it's code and adapted it, this code:
$wpdb->query( $wpdb->prepare(
"
INSERT INTO $wpdb->postmeta
( post_id, meta_key, meta_value )
VALUES ( %d, %s, %s )
",
10,
$metakey,
$metavalue
) );
So it works now.
If you are still facing the issue try printing the query
echo $sql = 'INSERT INTO `tablename` (`file`,`afile`,`type`,`keyword`,`location`,`numbl`,`userul`,`uldate`) values ("'. $_REQUEST['filename'] . '", "' . $url . '", "' . $_REQUEST['filetype'] . '", "' . $_REQUEST['filekeyword'] . '", "' . $_REQUEST['filelocation'] . '", "' . $_REQUEST['filenumbl'] . '", "' . $user_login . '", "' . time() . '"';
// $wpdb->query($sql);
// $_tbInsertId = $wpdb->insert_id;
Now run the printed query direct in phpmyadmin to check the issue if any of value is missing
Hope it will help
本文标签: termsINSERTING Data into table with placeholders
版权声明:本文标题:terms - INSERTING Data into table with placeholders 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741675666a2391867.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论