开发者

MYSQL - INSERT INTO from left

First of all thanks for your help, I'm asking how can I insert data into fields starting from the left? De开发者_运维知识库pending on the entity sometimes several fields at the end are left blank but I need to insert the filled fields into the table. If I try to do this, I obviously get a column-values mismatch error.

Thanks!


The column count you use and the number of values you try to insert have to match. You can specify what you want to insert, so you don't have to pass the columns which aren't needed (and so you don't need to pass "blanks").

INSERT INTO Store_Information (store_name, Sales, Date)
VALUES ('Los Angeles', 900, 'Jan-10-1999')

More information: http://www.1keydata.com/sql/sqlinsert.html


Just specify only fields names you gonna insert at the moment:

INSERT INTO table (field1, field2, field3) VALUES (value1, value2, value3)

or this way

INSERT INTO table SET field1=value1, field2=value2, field3=value3

no matter is it from left, right or checkered

as SQL query is just a string, you could use some PHP code to build this string in the way you want.

hereis an example code in the form of very simple helper function to produce SET statement dynamically:

function dbSet($fields) {
  $set='';
  foreach ($fields as $field) {
    if (isset($_POST[$field])) {
      $set.="`$field`='".mysql_real_escape_string($_POST[$field])."', ";
    }
  }
  return substr($set, 0, -2); 
}

it can be controlled by $fields array. used like this

//if we have full set of data
$fields = explode(" ","name surname lastname address zip fax phone");
$query  = "INSERT INTO $table SET ".dbSet($fields);

//if we have only three fields ready
$fields = explode(" ","name surname lastname");
$query  = "INSERT INTO $table SET ".dbSet($fields);

but you desperately need to learn basic PHP string operations to be able to do such things yourself.
string operations are most important in PHP, as almost every task for PHP is just string manipulation like in your case.


Use querys like

 insert into table set col1=val1, col2=val2;

or

 insert into table(col1, col2) values(val1, val2);

That is the only way around it. I think method 2 is the most commonly used.

If there is any way you can supply default values for each column use them for the columns you don't have values for.


if all the data can be "NULL", you can setup your query with variables that, if not set, are NULL, example:

$col1 = null; $col2 = null; $col3 = null; $col4 = null; $col5 = null;

then populate the columns you need $col1 = 'val1'; $col2 = 'val2';

$query = "INSERT INTO (col1, col2, col3, col4, col5) VALUES ($col1, $col2, $col3, $col4)";

Sincerely I'll never use something like this... but you asked for it...


mysql_query("INSERT INTO People (First_Name, Last_Name, Age)
VALUES ('Marcus', 'Porter', '28')");

mysql_query("INSERT INTO People (First_Name, Last_Name, Email)
VALUES ('Marcin', 'Kruk', 'marcin.kruk@gmail.com')");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜