Adding FIELDS on mySQL database without visiting phpMyadmin - php

i want to create database with a intity name "insert" but have a only one FIELD name--> "ID_no" <-- (this is auto increment field)
then .. i want to use PHP codes to add a FIELD without visiting the phpMyadmin
example:
i create my database name "data" and the intity name is "insert"
FIELD
ID_no |
then i will the use the php output code which functions of ADDING FIELDS in mySQL database to add a fields On my database..
PLEASE SHARE YOUR SOURCE CODE.....

You need to write an ALTER TABLE.. query, and then execute this in the ususal way. eg:
ALTER TABLE <tablename> ADD COLUMN <columnname> <columntype>;
replacing the values in < >

Related

PHP Insert / Update a mysql table from HTML form

This is my first mysql table.
I use HTML form in order to insert datas into a mysql table.
When submited, the form calls a php script in order to add datas into the form.
Then, mySQL table could look as follow :
From this mySQL table how could i generate and display a HTML table sorted by "NEXT_EVENT" column ?
If i want go on step ahead, is there a way a let user click on an upward / downward arrow placed next to each column header (links) in order to get the HTML table sorted as the desired column ?
--
Once the table sorted, i need to generate a raw simple TXT file where the ID on very FIRST row and NEXT_EVENT values will be copied to.
ie, sort.txt :
the 2 fields are comma separed.
2, 1499005140
Many thanks for your help,
To have such a link next to the heading you can use jQuery plugin tablesorter. You can refer at
http://tablesorter.com/docs/
And for just executing a query for one column, you can use ORDER BY clause
Hope it helps

Autoincrement Value in PHP

What's the best way, or, how would you do to have a field in PHP+SQL formatted as AB00000 where the first inserted should be AB00001 and so on. I have a web page in PHP+SQL to create a form and insert it into a table wich one of the columns is the "reference_nr" and my whole code is already made using the ID (AutoIncrement) so I can't use that for that table.
What I would need is something that would always write the last used maybe to another different table and before INSERT into form's table I would SELECT the last value and increment + 1 and INSERT with the result of that math operation.
Do I need another table or can I select directly from the form's table?
Do you think it give me problems if two persons try to INSERT the
form at the same time?
SQL brings that already with it:
Use the SQL-Function LAST_INSERT_ID() similar to the example in the source page I linked you below. Let's assume table foo has an auto-increment field "id":
INSERT INTO
foo (auto, text, somevalue)
VALUES
(NULL,'text', 2); # id was generated hoer
Then you do:
UPDATE
foo
SET
somevalue = somevalue + 1
WHERE
id = LAST_INSERT_ID()
Source: http://dev.mysql.com/doc/refman/5.7/en/getting-unique-id.html
If your problem is how to get an autoincrement value for a string column with prefix like AB00000, autoincrement fields are only numeric.
If the field have a fixed format you can create a plain insert using the id value from:
SELECT CONCAT('AB', LPAD( MAX(SUBSTRING(id,3)+1 ,5,'0') from table
And use this value directly in your insert to avoid transaction problems

Is there a way to copy a table structure to all tables in MySQL database at once?

phpmyadmin allows you to create a new table from a current table by copying its strucutre/strucute+data/data only...etc through a window similar to this...
However is it possible to copy the structure of a table to multiple tables in your database at once ?
one option would be to export the structure of the table. Then copy the sql generated by phpadmin. You could create a php script that iterates over the table names and runs the same alter/create query but replaces the table name.
You can do this using the show TABLES; query in mysql to get the tables then run alter $row[0] ... or create $row[0] ... queries from the export to do the work for you. just replace the table name with the varable name you have set in your loop.

How to input values into database and collect the new id to store in php/html?

I just can't figure out how to solve this problem using php to show on a web page.
When a user inputs values into "navn" and "kommentar" on my webpage. I will save these into two values. The "navn" value shall be saved into the database table "bruker" where "bruker_id" is auto increment. Then I want to get back which bruker_id the "navn" was saved under.
--> INSERT INTO bruker (navn) VALUES (inserted name from user);
Using the "bruker_id" and the "kommentar" I want to save into the table "kommentar" all the relevant information. Timestamp shall be current time and attraksjons_id is saved in a URL parameter called attraksjoner in Dreamweaver.
--> SELECT bruker.bruker_id FROM bruker WHERE bruker.navn = inserted name from user;
--> INSERT INTO attraksjon (bruker_id, attraksjons_id, kommentar, tidsstempel) VALUES (collected bruker_id from user, URL parameter attraksjoner from dreamweaver, comment collected from form, automated timestam);
Thank you in advance for your answer.
My database is as following:
Tablenavn: attraksjon
Primary key: attraksjons_id
attributes: attraksjons_navn, generell_info
---- Relation many-to-many table ----
Created many-to-many
table reisetips,
primary keys: bruker_id, attraksjons:id
attributes: kommentar, tidsstempel
--- Next tabel which is connected to attraksjon with many-to-many relation ----
Table name: kommentar
Primary keys: bruker_id
attributes: navn
My form consists of a text field for "navn" and one text area for "kommentar".
The Mysqli function "insert_id" can be helpfull in your case.
As described by the documentation : PHP Documentation
Another way to achieve this is by not using auto_increment and manage the ID yourself:
Before Inserting into the database your row, grab the last ID in the table and add one (For Exemple).
Pay attention that to avoid lock and other concurent write, you'll have to do this using transaction and prepared statement.
if you are using PDO then PDO::lastInsertId will do the work

can i get primary key value and extension after inserting in database using triggers in MySQL

Well, let me explain this as simple as possible, basically i have a table doc_info, which stores information regarding uploaded files; like file name, date created, uploaded by etc;
what i want is to create an INSERT trigger on this table, which will get two things, the primary key ID of this newly inserted row and the extension of the uploaded filename which will be in the document name; and concatenate them and update that same row, with this concatenated value
Example,
If someone uploads "document.docx", then ID will be auto generated as x and document name will be document.docx, thus the value to store will be "x.docx" using update on that same row.
I am new to this MySQL, and have little knowledge if operations like this can be performed with MySQL.
To implement such action within db you should create two triggers: after insert and on update. They should be like this one
delimiter |
CREATE TRIGGER changeProperty AFTER INSERT ON doc_info
FOR EACH ROW
BEGIN
UPDATE doc_info SET doc_info.someProperty = CONCAT(doc_info.id, doc_info.extension) WHERE doc_info.id = NEW.id;
END;
|
You can calculate extension on you file name by following expression: SUBSTRING_INDEX(doc_id.fileName, '.', -1);

Categories