I have a database that stores user details, I want users to be able to update their details if their name matches.
Currently I submit details to the database like this:
$sql="INSERT INTO gdpr_info (name, email, phone, comments, phoneout, emailout, postout, phonein, emailin, postin) VALUES ('".$yourName."','".$yourEmail."', '".$yourPhone."', '".$comments."','".$phoneout."','".$emailout."','".$postout."','".$phonein."','".$emailin."','".$postin."')";
How would I go about updating the user row ONLY if the name matches for example if a user called 'Robbie Fowler' wanted to update his email he would go to the form, type his name and anything else he puts in after would update his row instead of creating a new row.
I've seen the duplicate key option, but on the form there are checkboxes so I'm worried that if I use that most of the forms will have at least one duplicate key due to the checkbox and it will update the wrong row.
Can you specific which column must be duplicated to update the row?
If he is the user want to update his details I guess you the use the mysql update query to update how this will done is
First fetch the user from the db . The user can be the logged in user who wants to update
Then update the email column like this
UPDATE table-name SET column ='.$_POST['newvalue'].' WHERE namecolumn ='.$loggedINuser.';
Or if you want to change the bame as you said in the question with the name input and the new email then
UPDATE table-name SET column ='..$_POST['newvalue'].' WHERE namecolumn ='.$_POST['name'].';
And also I guess there can be only one primary fields in a table so just look for that the You can have multiple unique columns but one primary column (id column is recommended for that )
Hope this will work
Related
How can I insert more than one row for the same value
for example, each user has to submit 2 forms so the username is the same in each form but the information is different
I tried to use UPDATE but it removes the ole information and replaces it with the new one while I want to keep both
is there a way to do that?
insert into your_table (username, col2)
values ('user1', 1),
('user1', 2)
Have two tables, 'USERS' and 'FORMSUBMISSIONS'
When a user submits a form for the first time, a new entry is created in the USERS table, which is unique for each user, and would contain information connected to the user.
And whenever a form is submitted (including the first time), an entry is written to the FORMSUBMISSIONS table with the details of that submission, and a foreign key back to USERS.
That's a cleaner data model for this situation. It will also help future queries on the data. If you are limited to a single table for some reason, then successive inserts will work as above, as long as there is no unique key on the USER field.
you can add duplicate data just your primary key can't be duplicated because it causes primary key constraint. so what you can do is have an extra column let's say "ID" make it your primary key. While submitting the row keep on adding ID column's value by one, rest of the data could be same.
It depends on whether your USERNAME column allows duplicates.
If it's the primary key of the table, your table schema doesn't support what you want to do, because PK should be UNIQUE.
If your USERNAME column allows duplicates, you can use INSERT:
declare #username varchar(max) = 'your_username' --declare a variable to use the same username
insert into table_name (username, form_data)
values(#username, 'form_data_1')
,(#username, 'form_data_2')
It also depends on how you're executing the SQL statement. I would definately go and create stored procedure to do this insert.
you can use bulk insert query for that. as suggested by #huergen but make sure that your username or any field that might be in form data does not have UNIQUE key index. you can also add another field that works like PRIMARY key in that table.so many ways to do but it depends upon your requirement.
Use below insert format to get your desired result:
insert into Table_name(Field1, Field2)
SELECT 'user_1', 1 UNION ALL
SELECT 'user_1', 2
I am trying to update an emails field in my database... when one of our teachers sends an invitation through our system the invited email is recorded in our database.
I want the teacher to be able to send the email, and then if they forgot someone they can send another invite and the database field will then hold for example two emails (the original and then the added one).
Here is the code that I have to store the emails in the DB...
$recipientemail = $_POST['recipientemail'];
// Stores the (instance) in the instance database
include_once("$_SERVER[DOCUMENT_ROOT]/classes/includes/dbconnect.php");
$sql = ("UPDATE `database1`.`instances` SET `invitemail` = '{$recipientemail}' WHERE `instances`.`instance` = '{$instance}';");
$query = mysqli_query($dbConnect, $sql)or die(mysql_error());
This code overwrites the originally invited email whenever I invite a new person... many thanks for your consideration!
Update
The solution was in the form of the MySQL "concat()" function. I should have probably been clearer that I am not working with numerical values but rather strings (email addresses). So if we look at the example in the answer below:
UPDATE table SET c=c+1 WHERE a=1;
Here it's adding c and one mathematically, I wanted to add the emails to my database even separated by a comma so I simply did this...
UPDATE table SET c = concat(c, ',', 'new#email.com') WHERE a=1;
Works like a CHARM! ;-) And thanks for all the answers!
Try to use INSERT ... ON DUPLICATE KEY UPDATE
If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, MySQL performs an UPDATE of the old row.
For example, if column a is declared as UNIQUE and contains the value 1, the following two statements have similar effect:
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
UPDATE table SET c=c+1 WHERE a=1;
(The effects are not identical for an table where a is an auto-increment column. With an auto-increment column, an INSERT statement increases the auto-increment value but UPDATE does not.)
The ON DUPLICATE KEY UPDATE clause can contain multiple column assignments, separated by commas.
With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row, and 2 if an existing row is updated.
Hope this will help.
Hi everybody I would to know how is the best method for add if not exist a record or update one field in MySQL database from PHP. For example:
At the begining I have a blank table:
Table (id, date, num)
When a certain process is verified system must:
(if not exist record from that id) add a record in that table.
(if exist record from that id) auto-increment num value and update date value.
The question is whether it is possible to do all this in a single query?
Or should I make a query to check for the id? And depending on that add value or autoincrement the num value, and autoincrement should consult the previous value by adding one (1).
$sql="insert into
table (id,date,num)
values ({$id},CURDATE(),1)
on duplicate key update date=CURDATE(), num=num+1;"
The field id MUST be unique
I am creating user detail table in MySQL where user details are stored when user submits registration form.
In my user table user id field is primary key and auto increment.
My application should be used by many users from many locations. Now there is one scenario when it is possible that two user clicks on submit button at same time from anywhere. My primary key is of integer type and has length of 15, but when two user clicks at same time then which user should get first next id. Or it is possible that none of that get registered and get error.
so what can i do in this case
Your mySQL id field is good, so you should have no problem; however, when inserting a new record, then depending on what your insert SQL query looks like, make sure you leave the id undefined, or use something like:
insert into users values(NULL, "blah", "blah")
-where "NULL" is your first column / field.
This will only work the way you expect if the field (column) in place "NULL" is set (when created, or altered) to AUTO INCREMENT.
If these are set, the record id is incremented on each data entry, no matter if it happens at the same time; new records are always queued to be inserted one after the other.
I have a form, which contains an employees personal information. I update the form, and it inserts into a table, called employee_info. It updates the table with the details and inserts a NEW ID(employee_id). In my database, I have another table called department_info, and the field which is relevant is department_id. This is the php markup, for inserting data into the database:
$sql_data_array = array('employee_firstname' => $employee_firstname); //other variables go here
if (ACCOUNT_DOB == 'true') $sql_data_array['driver_dob'] = tep_date_raw($driver_dob);
tep_db_perform(TABLE_EMPLOYEES, $sql_data_array);
$employee_id = tep_db_insert_id();
What I need to do is, when the form is updated, and data is inserted into the employee_info table, I need it to insert a new id for employee_id(which is already happening), and also to insert the department_id into the employee_info table.
The department_id is used to login, and I want to show a list of the employees, which belong to the department. Can anyone tell me how I would do this?
When you insert with PHP, most of the implementations return so called last insert id. This is a value of AUTO_INCREMENT column.
Even if your implementation does not support it, there should be a function that returns it.
EDIT:
From your update, this seems to be osCommerce. The problem with your code is that method tep_db_perform is used to insert one or MORE elements. What will happen, if you insert 2 rows? But in simple case, use tep_db_insert_id(), it should return last id.
php function mysqli_insert_id retrieves the ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT). Get this id and use it in other tables.