is there a clean way of INSERT a lot of field entry values without them having to be in order? similar to how you can do with the UPDATE like below. can INSERT be done in that format?
$qstring="UPDATE test SET word = 'something' ,";
$qstring .= " word1 = 'something1',";
...
mysql_query($qstring);
Yupp,
insert into
your_table
set
field_1='Yay!',
field_2='Mmmbop!',
...
You can use the SET syntax:
insert into my_table set col1='value', col2='value'
Or, you can specify column names with a VALUES clause:
insert into my_table (col1, col2, col3) VALUES ('value1', 'value2', 'value3')
Using this later form, the values in the VALUES clause must match the order of the values in the column list that precedes the VALUES clause.
If you turn on "extended inserts" (it's usually on by default), you can use the latter form to insert multiple rows with a single statement:
insert into my_table (col1, col2, col3) VALUES
('value1', 'value2', 'value3'),
('row2value1', 'row2value2', 'row2value3'),
('row3value1', 'row3value2', 'row3value3')
INSERT INTO test SET word = 'something', word1 = 'something1'
yes we can do this also with INSERT
$qstring="Insert into test SET";
$qstring .= "word = 'something' ,";
$qstring .= " word1 = 'something1',";
...
mysql_query($qstring);
Related
I want to insert data into mysql table through user defined variables. 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
$val= aa, 11, 22, bb
Possible issues:
1- table is a reserved word. 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.
Try this before query:
$columns_name = "`$columns_name`";
$columns_name = str_replace(', ', '`, `',$columns_name);
$val = "'$val'";
$val = str_replace(', ', "', '",$val);
As title says, im trying to append a string to a VARCHAR column in my table.
The string is something like " //string ", forward slashes will be used later to explode the string to an array in PHP.
I was wondering if there's a way in MYSQL to perform a CONCAT(columnname, "//string") if the column is empty, otherwise perform a normal UPDATE ... SET ... WHERE . In this way, i will avoid the first value of my future exploded string to be a "//string" with forward slahes.
also, above I 've used bold characters for "in MYSQL" because I know i could first query the DB (to check if the column is empty) with something like:
$q = $conn->dbh->prepare('SELECT columnname FROM tablename WHERE username=:user');
$q->bindParam(':user', $username);
$q->execute();
$check = $q->fetchColumn();
and then leave PHP decide which operation perform:
if ($check != '') { // PERFORM A CONCAT }
else { // PERFORM AN UPDATE }
but this would mean a waste of time/resources due to 2x database calls and more PHP code.
thanks.
https://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
That means in your case:
INSERT INTO tablename (id,columnname) VALUES (1,'//string')
ON DUPLICATE KEY UPDATE columnname=CONCAT(columnname,'//string');
http://sqlfiddle.com/#!9/bd0f4/1
UPDATE Just to show you your options:
http://sqlfiddle.com/#!9/8e61c/1
INSERT INTO tablename (id, columnname) VALUES (1, '//string')
ON DUPLICATE KEY UPDATE columnname=CONCAT(columnname,'//string');
INSERT INTO tablename (id, columnname) VALUES (1, '//string')
ON DUPLICATE KEY UPDATE columnname=CONCAT(columnname,'//string');
INSERT INTO tablename (id, columnname) VALUES ((SELECT id FROM tablename t WHERE columnname='blahblah'), '//string')
ON DUPLICATE KEY UPDATE columnname=CONCAT(columnname,'//string');
INSERT INTO tablename (id, columnname) VALUES ((SELECT id FROM tablename t WHERE id=2), '//string')
ON DUPLICATE KEY UPDATE columnname=CONCAT(columnname,'//string');
INSERT INTO tablename (id, columnname) VALUES ((SELECT id FROM tablename t WHERE columnname='newone'), '//newone')
ON DUPLICATE KEY UPDATE columnname=CONCAT(columnname,'//newone');
If what you want is this:
first string: column will contain 'firststring'
second string: column will contain 'firststring//secondstring'
then do the update like this:
UPDATE tablename SET columnname = CONCAT( IF(IFNULL(columnname,'')='','',CONCAT(columnname,'//')), :string) WHERE username=:user
I have a table with quite a few columns. The total number of columns is not yet specified, and will change on a regular basis.
In my insert query, I only need to put two values into the table. All other values will be ' '. is there a way to only specify the first fields, without having to include '','','',''...? Please see below for example:
I would like to have this:
$query = mysql_query("INSERT INTO table VALUES('','$id')");
Rather than this:
$query = mysql_query("INSERT INTO table VALUES('','$id','','','','','',''......and on and on...)");
Is there a way to do this? Thanks!
Yes, specify the column names after the table name:
INSERT INTO table (column1, column2) VALUES ('','$id')
I'd prefer
INSERT INTO table SET columnA = 'valueA', columnB = 'valueB'
INSERT INTO table_name (column1, column2) VALUES (value1, value2)
http://www.w3schools.com/php/php_mysql_insert.asp
Just define the fields you will insert,
eg:
INSERT INTO table (fieldA, fieldB) VALUES('','$id')
the missing fields will have the default value for that field
Im doing a PHP script for insert in the table A values recovered from the table B (A and B are in different databases).
Table A Columns
[index(autoincrement),timestamp(currenttimestamp),col1,col2,.....col15]
and I have the query for retrieve the values from B:
$query= "select count(*) as col1, XXX as col2.....ZZZ as col15 from B";
so having the
$row=$mysql_fetch_array($result)
where
$result=mysql_query($query)
how can I make an
insert into A (col1,col2.....col15) values ($row['col1'],....$row['col15'];
easily without write all the code? Thanks
Insert into db1.A (col1, col2, col3) SELECT col1, col2, col3 FROM db2.B
If you have to transfer the data across databases on different servers, you can use sprintf and implode to generate your query.
$query = sprintf('INSERT INTO table_name (%s) VALUES ("%s")', implode(', ', array_map('mysql_escape_string', array_keys($row))), implode('", "',array_map('mysql_escape_string', $row)));
First of all thanks for your help, I'm asking how can I insert data into fields starting from the left? Depending 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')");