i already input about a thousand values to a table but my client want a "little" revision that ihave to insert a field (fulldetail) is there a way to get the values of the fields (acount, fname, mname, lname) and combind and put into the field (fulldetail).
After updating the table structure (adding the new column), you can execute a mysql query like this:
UPDATE `table_users`
SET `fulldetail` = CONCAT_WS( " ",`acount`,`fname`,`mname`,`lname` )
Related
Is it possible to INSERT a row, and simultaneously set one of the fields to contain the inserted row's ID? (the "self" id)
I'm trying to avoid using multiple queries if possible (it would be 3 queries in total otherwhise)
Something like this (but probably not):
INSERT INTO thetable (email, phone, activationkey) VALUES ($email, $phone, CONCAT(THIS_NEW_ID, md5($activation) )
Why would you want to store duplicated data?
You could change your table structure and just run:
INSERT INTO thetable (email, phone, activation_suffix)
VALUES ($email, $phone, md5($activation))
And then you have all the data you need.
You can always concat when you query the table:
SELECT CONCAT(id, activation_suffix) activationkey
FROM thetable
WHERE ...
UPDATE
On second thoughts, do you really need the id as part of the activationkey?
I wouldn't want to give any user the id for their record in my table unless it is hashed/encrypted.
I want to insert my table data from copying another table with some new given data.
I use this query
$sql = "INSERT INTO table2(name, city, email,money)
hasib,SELECT table1.city, table1.email,
newsletter_subscribers.email
FROM table1 WHERE name='jesy',100";
But its not Work
You must give all columns inside the select statement columns, even constant values, e.g.
insert into table2(name, city, email, money)
select 'hasib', city, email, 100
from table1
where name = 'jesy'
If you want to take values from multiple tables, as your select statement suggests, you must look into joins.
I want to insert data partially from select statement..
For example..
I have one table named movie..here's the fields :
-id_movie
-tittle
-studio
-time
And another table named reservation.. here's the fields :
-id_reservation
-tittle
-studio
-time
-seat_1
-seat_2
So, I want to insert value of tittle, studio, and time into reservation table that getting from select statement from movie table. But I'm a little bit confuse about another field in reservation table (id_reservation, seat_1, seat_2).. They're actually don't exist in the first table (movie table)
so how to write the query?
any simple example would be very helpful
thank you
$query = "INSERT INTO reservation(tittle,studio,time)
SELECT tittle,studio,time FROM movie";
INSERT INTO reservation (
tittle,
studio,
time
)
SELECT tittle,
studio,
time
FROM movie;
But you better not have NOT NULL on the columns you leaving without values.
If you want mysql query then:
"INSERT INTO reservation(tittle,studio,time)
SELECT tittle,studio,time from movie";
First do a SELECT from "movie" to obtain the fields you need from that table.
You will have the fields you need stored in 3 variables, say $title, $studio, $time.
As per the table reservation, the field id_reservation should be the PK so set it to autoincrement. You don't have to insert that value as it does it automatically in every insert.
The fields seat_1 and seat_2 will depend on other facts than the data you get from the first table. You should obtain the free seats (I guess there is another table with the total amount of seats), and select 2 using some kind of logic (for example, that they are next to each other)
So in the end, the INSERT query for the table reservation should look like
"INSERT INTO reservation (title, studio, time, seat_1, seat_2) VALUES('$title', '$studio', '$time', $seat_1, $seat_2)");
I have a MySQL table with an auto-incrementing primary key (UID). The user can bring up a record in this table via a PHP form, where the UID is a $_GET variable. I would like the database to update the chosen record if it exists, but every time the user hits submit, a new record is created. This is what I've tried so far:
//Present the form
$form = $con->prepare("SELECT Name, Date, Time FROM forms WHERE UID = :uid");
$data = array('uid'=>$_GET['uid']);
$form->execute($data);
$row = $form->fetch(PDO::FETCH_ASSOC);
//Write new record to database. If the UID exists, update the record.
INSERT INTO forms (Name, Date, Time) VALUES (:name, :date, :time)
ON DUPLICATE KEY UPDATE Name=VALUES(Name),Date=VALUES(Date),Time=VALUES(Time);
This doesn't work. Do I need another unique key in the table, or am I missing something obvious?
INSERT INTO forms (`Name`, `Date`, `Time`) VALUES (:name, :date, :time)
ON DUPLICATE KEY UPDATE `Name`=:name, `Date`=:date, `Time`=:time;
This should be the query; I removed VALUES() from your code and added backticks and corrected your parameters.
And I recommend you to use backticks around column names so it doesn't get confused with other similar SQL keywords as example.
I'm creating a registration and I'm using just straight PHP not JavaScript to send my form to the MySQL database, so everything is working fine, no syntax error or anything but I fill out all my information and click 'Register' and it returns a message saying 'Column count doesn't match value count at row 1'.
I'm only 14 so this is pretty confusing for me does anyone have a solution?
This is my INSERT INTO code:
$sql = mysql_query("INSERT INTO users(firstname, lastname, email, password, day, month, year, gender)
VALUES('$firstname','$lastname','$email','$db_password','$day','$month','$year')")
or die (mysql_error());
You are trying to insert 7 values into 8 columns - you are missing the insertion of gender.
The correct code would be:
$sql = mysql_query("INSERT INTO users(firstname, lastname, email, password, day, month, year, gender)
VALUES('$firstname','$lastname','$email','$db_password','$day','$month','$year', '$gender')")
or die (mysql_error());
By the way, if you are not already doing it, I highly recommend escaping the strings first, before passing them to the query like so:
$firstname=mysql_real_escape_string($firstname)
You should do this with all variables above. Here you can find more about the escape function.
With your code there, I see you forget to insert $gender.
When inserting data into a MySQL table, you will have to specify which data goes into which column. You do this by specifying the column names before the VALUES part:
INSERT INTO tblA (col1, col2) VALUES ('value1','value2');
If you omit that information, MySQL will expect all columns:
If your table is like this:
CREATE TABLE tblA (
col1 INT,
col2 INT
);
You can insert information like this:
INSERT INTO tblA VALUES ('value1', 'value2');
If you omit column names and do no specify values for all columns, MySQL will give the "Column count doesn't match value count at row" error will occur, as MySQL doesn't know what to put in the missing columns. With the table structure as above,
INSERT INTO tblA VALUES ('value1');
will result in that error.
In non-strict mode, MySQL will insert default values for omitted column names.
You have missed a value for gender column (the last one)
You are completely missing the gender value:
$sql = mysql_query("INSERT INTO users(firstname, lastname, email, password, day, month, year, gender)
VALUES('$firstname','$lastname','$email','$db_password','$day','$month','$year', 'gender goes here')")
or die (mysql_error());