Simplify Insert-update-delete From Database
I'm looking for some functions or objects allowing to
select-insert-update-delete from any table in a mysql database without the
need to create a new query every time. Example:
selectdatas(array('field1','field2','fieldn'),arra y('table1','tablen'),array('left
join,idy','inner join, idx'))
then the function build the query, execute it and then return an object with
the query result.
I must build a database driven site that is 90% of the time spent on
building queries. If I may avoid build them manually it will help me a lot
and let me gain some days of programming.
View Complete Forum Thread with Replies
Related Forum Messages:
- OOP Page: Where To Put Update, Insert, Delete
- Php/mysql Insert, Update, Delete All-in-one
- Update, Delete And Insert Values
- Insert, Delete, Update And Query Data Via DBX
- Simplify Update Query
- Simplify The Syntax For The INSERT
- Database Update/edit And Delete
- Update Without Delete
- SQL Insert And Delete?
- INSERT And DELETE
- Insert Into New Table And Delete From Old
- Basic Delete/update Script
- How To Do Insert,delete,edit Using Ajax
- Insert/update
- INSERT OR UPDATE?
- Can Insert But Not Update?
- INSERT Vs UPDATE
- Insert And Then Update
- Insert Into / Update
- Insert And Update
- Insert Or Update
- Insert/update
- Multi Table Insert Delete Select Function Needed.
- Insert On Duplicate Key Update
- Should I Use INSERT? UPDATE? REPLACE?
- Multiple Insert Or Update
- SELECT, INSERT, UPDATE
- Insert To One Table, Update To Another
- Synchronizing Via INSERT/UPDATE
- Update Based On Last Insert ID
- UPDATE And INSERT In Mysql
- Update If An Insert Has Been Made
- Insert Or Update Dilemna
- Records For Update Or Insert
- Update And Insert Data Into The Db.
- Modifying Insert To Update
- MySQL: Insert...select - Update?
- Dynamic Update/insert Statement?
- When To Update Or Insert Based On Checkboxes
- Update Query - Retireving The Last Insert Id
- Combine INSERT And UPDATE MySQL?
- INSERT, UPDATE Into A Memory Table?
- Update Query - How Do You Update A Value In The Database In A Certain Row?
- Can Not Update A Field Where The It's Id Equals An Id Recieved From The Last Insert.
- Automatically Update Database On Server Usign Php.e.g Mysql Database
- Present An Array Of Data, And The Corresponding Primary Key And Let The Code Work Out Whether To INSERT Or UPDATE It.
- Simplify An Elseif
- Simplify Switch
- Simplify PHP/SQL Statement?
- Simplify Fputs()
- Delete From Database
- Delete Database
OOP Page: Where To Put Update, Insert, Delete
I have a page that shows info from MySQL. It is for an online education site. It uses different classes arranged in a hierarchy. Each lesson is roughly laid out like this: Lesson |-Intro | |-Section | |-Procedure | |-text, video, images | |-Section | |-Procedure | |-text, video, images | |-Summary I have all the classes working properly for displaying info from the database. Now I am building the admin site. Where should I put the update/insert/delete code? Would those go inside the classes? Should I use the lesson object when doing the maintenance? Or, should I just have a separate page that handles the database stuff and only use the objects to display the data from the database?
View Replies !
Php/mysql Insert, Update, Delete All-in-one
I have a check box form in an admin area, that links various related tables into one. These check box results are stored in an array, and then looped, there are in fact four groups of arrays, but the array length could become large (i.e. dozens - not hundreds) The issue I have is that from the one form multiple things need to occur. - If a box is checked and the record doesn't exist a new record needs to be inserted. - If a box is checked and the record does exist, it should be ignored (as it already exists). - If the box is unchecked and the record exists, it should be deleted. - If the box is unchecked and the record doesn't exist it should be ignored (the record doesn't exists). I'm looking for the logic and process (rather than being hand fed code ) Can this be done with 1 (or more likely 2) sql statements. Or do I have to loop thru the arrays, checking the existance and then applying a specific sql statement depending upon checked status and the records existance. Someone mentioned using IF() in the sql statement, but I am not familiar with that one.
View Replies !
Update, Delete And Insert Values
i am looking for a code example for how to update and insert values into an existing xml doc. i know how to read from it but i couldn't find any materials about how update, delete and insert values to xml. i want to use simplexml.
View Replies !
Simplify Update Query
I want to search for the word NULL in any of my fields and replace them with ''(nothing). I'm using the query below to do the job. Is there any way to summarize all these statements into one query so that I don't have to run them individually? PHP Code: $query1 = "UPDATE table SET field1 = '' WHERE field1 IS NULL"; $query2 = "UPDATE table SET field2 = '' WHERE field2 IS NULL"; $query3 = "UPDATE table SET field3 = '' WHERE field3 IS NULL"; $query4 = "UPDATE table SET field4 = '' WHERE field4 IS NULL";
View Replies !
Simplify The Syntax For The INSERT
I have a form being submitted with values such as: Rate[1] Rate[2] etc etc. I'm working on the submission page. And I want to simplify the syntax for the INSERT so I'm defining $Rate and then just going to put that on the query. So if I do a test, such as: $x = 5; $Rate = $_POST['Rate[$x]']; I'm not getting a value. I know it's a problem with the brackets/syntax but I'm not good with proper syntax. Could anyone guide me here? I know it just needs some quotations and " ' and such in the right spots.
View Replies !
Database Update/edit And Delete
i have spent the last few days trying to create a simple script to add/edit and delete entries in a mysql database with php. i have 1 database with 40 tables, each table has 3 fields (id, header and body) i created an input page with input fields for header and body. the table to store the information in is selected from a drop down menu. all this works fine. for some reason i cannot get the edit and delete page top work at all. would someone mind taking a look at the code and let me know what i am doing wrong? Code:
View Replies !
Update Without Delete
If i get a user to fill in a form that will update acurrent record, the user may leave a field blank, if field is not manditory, when you do an update, the blank field will over write the current data if there was any, is there a way round this?
View Replies !
SQL Insert And Delete?
Anyone know if there's a way to call a query but remove whatever you grabbed from the database, or would I have to do a query to retrieve what i needed, then follow it up with a delete on the same query results?
View Replies !
INSERT And DELETE
I have a dynamic website that prints the info in the database out with checkboxes next to it. When a user checks the boxes and press the submit button, the record should be deleted from the database and then they should be stored in another database that assigns a different primary key ( not in that order). Also I would like for the user to be redirected back to the same page and for the records that were deleted, not to show up.
View Replies !
Insert Into New Table And Delete From Old
I've got 2 tables of users: 'pending' and 'approved'. The first table (pending) is a 'holding' table for unapproved users and when the user is approved I can click on a checkbox by their name and copy them over to the approved users table (INSERT INTO approved...) This all works fine but I'd also like to delete them from the 'pending' table at the same time. How would I do this? Something like (INSERT INTO approved, DELETE FROM pending)? I'd do a bit of trial and error but since it's on a live site, I don't want to muck it up and not be able to fix it! Code:
View Replies !
Basic Delete/update Script
i am trying to do is delete a user off the database, via the admin menu. when the user is deleted the database is updated: this also be done using a HTML form, where when the admin type in a name and the user is located, the form then ask's if the admin would like to delete the user.
View Replies !
Insert/update
I came accross a little piece of code today that I thought was quite cute, but I'm not sure if it's any better than my usual method. The code ultimately needs to see if a unique identifier exists in a database, then either insert or update accordingly. The way I've always done it is: SELECT * FROM blah where..... if (there's a row) then UPDATE..... else INSERT but the nifty bit of code did it like this. UPDATE.... if (mysql_affected_rows == 0) then INSERT
View Replies !
INSERT OR UPDATE?
I am writing an app in PHP that uses a PostGres database. One thing i have noticed is that what should/could be a single line of SQL code takes about 6 lines of PHP. This seem wasteful and redundant to me. Here is a sample of what I'm talking about ($db is a PDO already defined and created). $query[1] = "UPDATE my.table, SET somefield = '$someval' WHERE somecondition"; $query[2] = "INSERT INTO my.table (somefield) VALUES ('$someval')"; if(!$db->query($query[1])){ $db->query($query[2]); } What I'm curious to know is if there is some way to simplify this, either buy some PHP builtin or extension, or possibly something in SQL I am missing. It seems to me that "UPDATE OR INSERT", should be valid, but I can't seem to find anything relevant at all about it. Anyways I hope you don't mind, but I'm crossposting this to pgsql.general and comp.lang.php to see if I can get some information on the subject.
View Replies !
Can Insert But Not Update?
I'm having a problem getting the value to update for the total amount of avatars a person has when they unlock a new one. I've been sitting here looking at the code for the last hour and for the life of me I can't figure out what I'm doing wrong. Code:
View Replies !
INSERT Vs UPDATE
There: -On page 1 of a registration form I have an INSERT quiery to input the data in the DB. -On page 2 I have an UPDATE for additional fields -On page 3 I also have an UPDATE statement for remaining fields On page 4 it's the confirm page to make sure the data is right for the user. It's basically a SELECT quiery if the user were to go back to page 1 and make some changes, would that basically create an issue since there is an "INSERT" quiery and not an "UPDATE" quiery? Because basically, once the user clicks on "next" to go to page 2, the user ID is already created and for those fields, the INSERT quiery inputs the data.
View Replies !
Insert And Then Update
I have the following code but it doesn't work. <?php $query = "INSERT INTO transportationcharges (TransportationUnit) VALUES ('$TransportationUnit')"; $updateSQL = $conn->GetUpdateSQL($query, $TransportationUnit); $conn->Execute($updateSQL); ?>
View Replies !
Insert Into / Update
I have a page with several text boxes and one text area. For some reason when I submit the information the first time using an insert into statement everything works fine. If I try to edit the info and submit it again with an update statement I get a syntax error. Not really sure what the problem is. Code:
View Replies !
Insert And Update
I have to pass info from table A to table B, I need for each record I passed (insert) into table b, to update the relevant record in table A. Is there any way to do this only with sql i.e. mybee insert and update in the same sentence. imaginary example: insert into table_b (field_a,field_b) select field_a, (update table_a set field_b="processed" where field_a=out.field_a) from table_a out,
View Replies !
Insert Or Update
When inserting or updating, is there a join for these functions or are joins just used for extracting data, and multiple insert or update code used for putting selected data into their respective tables.
View Replies !
Insert/update
i'm trying to do a many to many insert/update and cannot find any information on how this is done in php? I have done this in asp before but am not familiar enough with php to get a working version. here is the asp code that i have used in the past for an insert. Code: <% set customersRS = Server.CreateObject("ADODB.Recordset") customersRS.ActiveConnection = MM_connLMYC_STRING customersRS.Source = "SELECT top 1 * FROM customerRegister order by customerID desc" customersRS.CursorType = 0 customersRS.CursorLocation = 2 customersRS.LockType = 3 customersRS.Open() customersRS_numRows = 0 %> <% If Request("selected") Then customersRS.addnew customersRS("Name")=Request("customerName")....
View Replies !
Multi Table Insert Delete Select Function Needed.
I am going nuts here and I know with all the functions out there, there must be an easier way to do this. Maybe someone can point me to the right tutorial or even show me how to do this. I have a $main_id and many $sub_id tables and like to “SELECT to view” INSERT and DELETE those. UPDATE must not be but would be nice. Here is how it looks:
View Replies !
Should I Use INSERT? UPDATE? REPLACE?
I have a table that looks like this: MyTable (id,date,field1,field2) At the top of my script I insert a date into my table and initialize $top_id with my_insert_id. Then down towards the end I want to write to field1 and field2 their values, and I am using this: PHP Code:
View Replies !
Multiple Insert Or Update
I have this form and i want to do a multiple insert using checkboxes. The values are fetched from a tbl1 and will be inserted into another table tbl2. I want to get only those fields where checkbox next to it is selected Code:
View Replies !
SELECT, INSERT, UPDATE
On this project I have got 2 items a mobile and simcard, these are in one table as two records and have a unique equid each. the simcard is also in another table called siminfo and has a parentid (which is the same equid as the mobile) to create the link. In the equipment table there is also userid I already have code that transfers these items individually, but i want to be able to transfer them both when i transfer the sim card. I currently have the followin but its not working for some reason. the variable $equid does work i have tested it. This code is not picking up the simparentid though Code:
View Replies !
Insert To One Table, Update To Another
I'm trying to do two queries at once utilizing data from a form. I'm creating a new row in one table, and I need to reflect the ID number and other supplementary data to an existing record in another table. Is there a way to string two queries together so they execute at the same time? Here's what I've been trying so far: Code:
View Replies !
Synchronizing Via INSERT/UPDATE
I've been googling for an answer but am not even sure how to phrase my search on this question. Here's the situation: I have a real estate database with 2 tables. The first is a listing of all houses for sale in the market, updated daily on an automatic basis (via cron.) . The other is a listing of just the homes available through a certain agency. I am trying to do an update of the second database that does 2 things, A. Inserts new homes from Table1 into Table2 and B. Updates the existing information from Table2 with current information from Table1 (price changes, etc.) . I have accomplished part A using the following code: PHP Code: $query = "INSERT IGNORE INTO table2 SELECT * FROM table1 WHERE table1.category='XXXXX'"; I'm having trouble coming up with proper code for the second part. There is additional information added to the rows in Table2 so I know I need to use update to change just the new information but I'm not certain how to use the right variables. Ok, for my actual question without the background, here it is: How do I perform an UPDATE of multiple records, matching a certain criteria, but not all records?
View Replies !
UPDATE And INSERT In Mysql
Im trying to do this: if a column exists then update it, if it does not exist insert it. This is my code (and its not working): if(!($q = mysql_query($sql_update))){ Â Â Â mysql_query($sql_insert); not working... Noticed that mysql_query($sql_update) returns 1 even if the column does not exist... should it not return false in that case?
View Replies !
Insert Or Update Dilemna
I have a large database that is updated daily. I am trying to pass a subset of data from one table to another within the database. It's a real estate site and I am trying to create a smaller table to work with houses from a certain office from within all the data. PHP Code: $query = "INSERT INTO table2 SELECT * FROM table1 WHERE officeid = 'xxx'"; But what I want to do is set it so that my script evaluates each record, checks whether it exists in table2, if it does, it updates it, if it doesn't, it inserts it. Is there a way to do this with if ... else or am I just thinking wishfully. I would be delighted to read up on this if someone could help me figure out the right Google query, I've tried but to no avail.
View Replies !
Records For Update Or Insert
I am trying to add a signup for an e-Newsletter and I want to check to see if they already signed up to prevent duplicates. I found this link talking about it and I am using bpat1434's technique and it tells me the record was updated, when it wasn't even inserted into the database yet? I used a unique email address to test the insert and it passes PHP Code: if(!$result) and continues to the update.
View Replies !
Update And Insert Data Into The Db.
I need to figure out the best way to update and insert data into the db. I have 3 tables room[id, room_num, building, numcomps] software[id, title, version] softroom[rid, sid, date] The site is meant to print out software for a room, example: Code:
View Replies !
Modifying Insert To Update
Here is my code for the insert at the moment: PHP Code: <?php include("connect.php"); session_start(); require_once('common_functions.php'); $_SESSION['form1_data'] = $_POST; // build main query from form 1 Â Â Â Â $sql = "INSERT INTO quality_info SET quality_id =Â Â NULL "; Â Â Â Â foreach ($_SESSION['form1_data'] as $col =>$val ){ Â Â Â Â Â Â Â Â if($col <> 'submit'){ Â Â Â Â Â Â Â Â $sql .=", $col = '$val' "; Â Â Â Â Â Â Â Â } Â Â Â Â } ..........
View Replies !
MySQL: Insert...select - Update?
I was reading about insert select http://dev.mysql.com/doc/mysql/en/insert-select.html Thought this may be a better way to perform a query I'm doing. The differenc is that I need td select from one field, and then update another field in the same record (it is a backup/undo field). Is there a way to do this in one query? It's a large chunk of data (longtext) so I thought this would be faster than the alternative...
View Replies !
Dynamic Update/insert Statement?
I'm hoping to build a dynamic sql statement for inserts and updates based on FORM values. I've done this in ASP but not sure about PHP syntax. Right now I just output the form entry results, but here's what I'm thinking for an update: PHP Code:
View Replies !
When To Update Or Insert Based On Checkboxes
I'm building a script that involves a step-by-step registration process. It is step-by-step because a user can go back and forth and edit everything he/she has entered. One of these steps (Step 2) contains some checkboxes that pertain to sections (called extras) that users may want to add to their profiles (such as Hobbies, Awards, Favorite Movies etc.). After several steps the user is then taken to pages for them to fill up according to the extras he/she had checked earlier. My main concern lies wherein the user chooses to go back to that page and checks/unchecks some of the checkboxes (all of which are called 'extras'). There are two buttons on that page, 'Back' and 'Continue'. If Back is pressed, we are taken to Step 1, and if Continue is pressed, we go to Step 3. Currently, when the Continue button is pressed, the script deletes everything based on some conditions and inserts whatever is checked into a Mysql table, which I think is not a very good way to do it. I'm trying to figure out a way so that the script recognizes 1. what was checked just recently, 2. what was checked and is still checked and 3. what was checked and now unchecked. This way, I can instruct the script to INSERT only the newly checked ones, UPDATE the still checked ones and DELETE the unchecked ones.
View Replies !
Update Query - Retireving The Last Insert Id
I have to update my the filename field after I insert info into it for purpose of retireving the last insert id. I am using pdo as my db connection and everything else. The questionis that why will the code below not even execute. do i have to use bindParam or use exec or something along those lines PHP Code: // Retrieve the ID from the last insert             $new_id = $db->lastInsertId();             $filetype = $photos_uploaded['type'][$counter];             $extention = $known_photo_types[$filetype];             $filename = $new_id.".".$extention;             //mysql_query( "UPDATE gallery_photos SET photo_filename='".addslashes($filename)."' WHERE photo_id='".addslashes($new_id)."'" );             $select = $db->prepare("UPDATE gallery_photos SET photo_filename='".$filename."' WHERE photo_id='".$new_id."'");             $select->execute();
View Replies !
Combine INSERT And UPDATE MySQL?
Is there a way to combine INSERT and UPDATE MySQL? I want to say "INSERT the data if the username doens't exist, or update the existing data if the username already exists" INSERT INTO scores VALUES ('".$uname."', '".$score."') [if username doens't exist already] combined with UPDATE scores SET score='".$score."' WHERE uname='".$uname."'
View Replies !
INSERT, UPDATE Into A Memory Table?
Just created a MySQL table in the memory (memory storage engine) but im having problems adding information to it... First of all it is complaining every time i try to auto increment the primery key.... then it is not adding anything when I use a standard insert into the table (it has no auto inc. now....).
View Replies !
Update Query - How Do You Update A Value In The Database In A Certain Row?
I am trying to make a little thing that allows you to edit something that you have previously submitted to be in the database. How do you update a value in the database in a certain row? Right now when I run my script, it submits the updated info into into every row in the database. How do I just make it so it inserts it into row x? (x being some random number that I will have passed to it).
View Replies !
Present An Array Of Data, And The Corresponding Primary Key And Let The Code Work Out Whether To INSERT Or UPDATE It.
There's lots of DB abstraction layers out there, but a quick look around them hasn't turned up anything which seems to met my requirements. Before I go off and write one I thought I'd ask here if anyone knows of such a beast... I want some code where I present an array of data, and the corresponding primary key and let the code work out whether to INSERT or UPDATE it, I also want to be able to present the data from a QBF or QBE then be able to step through the result set. However I don't want to have to configure the DBMS structure - after all most of it is already in the DBMS (OK so not the relationships in a MySQL db). It'd be really cool if I could throw SQL directly at it *too*.
View Replies !
Simplify An Elseif
I know there will be a simple way to do this but I can't figure it out without an infinite elseif structure. Basically for every four $totalamount purchased a $freebonus is to be added. if ($totalamount >= 4 and $totalamount < 8) { $freebonus = 1; } elseif ($totalamount >= 8 and $totalamount < 12) { $freebonus = 2; } elseif ($totalamount >= 12 and $totalamount < 16) { $freebonus = 3; }
View Replies !
Simplify PHP/SQL Statement?
I need to concatenate a string based on values from two different database, and I think the way I'm going about it is the long way, and I'm hoping someone can simplify this for me (if it can be, I don't know). It's more to do with the SQL query than anything. Code:
View Replies !
Simplify Fputs()
i am currently using code that resembles this: fputs($cmke,"<? $first=""); fputs($cmke,$first); fputs($cmke,""; $last=""); fputs($cmke,$last); fputs($cmke,""; $username=""); fputs($cmke,$username); fputs($cmke,""; $password=""); fputs($cmke,$mypass); fputs($cmke,"";?>"); this isn't too bad, but in reality i have many more variables, i'd like to turn this into just one fputs() command is that possible? My script isn't done yet so i can't test, but i would assume that it could be written like this: fputs($cmke,"<? $first="" . $first . ""; $last="" . $last . ""; $username="" . $username . ""; $password="" . $mypass . "";?>"); is this right?
View Replies !
Delete From Database
I have code that does not delete from a database. The same code (cut an paste in the same file, but different function and having a different query) works. So, of course, I tested the query interactively. I echoed the query and did a cut and paste when interactively connect to the database. That one worked so it isn't the query since interactively it is seeing exactly the same thing as the code produces. Here is the code: function deleteCatalog($catalog) { $query = "DELETE FROM CatalogNames WHERE sCatalogID='" . $catalog . "'"; mssql_select_db($database_Login, $_SESSION['Login']); $result = mssql_query($query, $Login); ... } The $query echos: DELETE FROM CatalogNames WHERE sCatalogID='CMP' As an example of code that works (in the same file) function addToCatalog($cat_id, $cat_name) { $query = "INSERT INTO CatalogNames (sCatalogID, sCatalogName) " . "VALUES ('" . $cat_id . "', '" . $cat_name . "')"; mssql_select_db($database_Login, $_SESSION['Login']); $result = mssql_query($query, $_SESSION['Login']); ... } The second and third lines are identical (cut and pasted) as in other places in the same code. In fact, it is only in this file that I do all the database work. Every other function works. This is the only delete, however.
View Replies !
Delete Database
using php and mysql how can i tell the my website to move any entry older than 30 days to a different table and then delete it below i have some script i did try but this doesnt work (im using unix timestamp incase that changes anything) mysql_query(" INSERT INTO newsarchive (id, postdate, title, newstext) SELECT id, postdate, title, newstext FROM news WHERE postdate < DATE_SUB(curdate(), INTERVAL 30 DAY)"); mysql_query("DELETE FROM news WHERE postdate < DATE_SUB(curdate(), INTERVAL 30 DAY)"); mysql_close($link); //clean up
View Replies !
|