简体   繁体   English

通过mysql表中的变量插入

[英]insertion through variable in mysql table

I want to insert data into mysql table through user defined variables. 我想通过用户定义的变量将数据插入到mysql表中。 Here is what i am doing. 这是我在做什么。 Its gives no error but also don't upload any data into table. 它没有错误,但是也不会将任何数据上传到表中。

$sql="insert into table(".$columns_name.") values(".$val.")";

Here is how the the values of the variables are: 这是变量的值如何:
$columns_name= col1, col2, col3, col4 $ columns_name = col1,col2,col3,col4
$val= aa, 11, 22, bb $ val = aa,11,22,bb

Possible issues: 可能的问题:

1- table is a reserved word. 1-表是保留字。 your table name can't be 'table'. 您的表格名称不能为“表格”。 (I assume you wrote it as a mock name). (我假设您将其写为模拟名)。

2- the format of column names and values in INSERT query is wrong. 2-INSERT查询中的列名和值的格式错误。

Try this before query: 在查询之前尝试以下操作:

$columns_name = "`$columns_name`";
$columns_name = str_replace(', ', '`, `',$columns_name);

$val = "'$val'";
$val = str_replace(', ', "', '",$val);

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

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