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 Complete Forum Thread with Replies
Related Forum Messages:
- OOP Page: Where To Put Update, Insert, Delete
- Update, Delete And Insert Values
- Simplify Insert-update-delete From Database
- Insert, Delete, Update And Query Data Via DBX
- UPDATE And INSERT In Mysql
- MySQL: Insert...select - Update?
- Combine INSERT And UPDATE MySQL?
- How Can I Update/insert One Complete Table Into Mysql Table
- Update Without Delete
- SQL Insert And Delete?
- INSERT And DELETE
- Insert Into New Table And Delete From Old
- Mysql Delete - $query = Mysql_query("DELETE
- Database Update/edit And Delete
- 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 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
- Dynamic Update/insert Statement?
- When To Update Or Insert Based On Checkboxes
- Update Query - Retireving The Last Insert Id
- INSERT, UPDATE Into A Memory Table?
- Can Not Update A Field Where The It's Id Equals An Id Recieved From The Last Insert.
- Present An Array Of Data, And The Corresponding Primary Key And Let The Code Work Out Whether To INSERT Or UPDATE It.
- Mysql DELETE
- Delete For MySQL
- Update Table While Insert Data To Another Table
- Delete MySql Row By Date
- How Do You Do A CASCADE Delete In MySQL?
- MySQL - Delete Across Two Tables. OT
- DELETE Problem With MySQL
- Mysql Delete From A Form
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 !
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 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 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 !
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 !
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 !
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 !
Mysql Delete - $query = Mysql_query("DELETE
how would i do the following ? $query = mysql_query("DELETE notes, datestamp, abs_value, ID FROM absence_mgt WHERE datestamp='$date' AND ID='$vtc_login' ") or die(mysql_error()); im just getting "Unknown table 'notes' in MULTI DELETE" ?
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 !
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 !
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 !
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 !
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 !
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 !
Mysql DELETE
im a stuck again, could some one please tell me why this dosent work; mysql_query("DELETE FROM messages WHERE from='$fromid', sento='$myid'");
View Replies !
Delete For MySQL
I'm an experienced java/C++/SQL programmer who hasnt used PHP. I'm looking for some generic database access pages for doing dds, edits, deletes, etc., hopefully with some error handling. I'd rather start with a basic shell of code, so I don't have to reinvent the wheel, and can just change connection string, database fields, etc.
View Replies !
Update Table While Insert Data To Another Table
it seem like mine coding din work. [php]<? include("checkin.html"); $conn=mysql_connect('localhost','root','') or die ('Could not connect to server'); $today = date("Y.m.d"); mysql_select_db('hms', $conn); $ok = mysql_query("INSERT INTO check_in(ID,room_no,datein)values ('$ID','$room','$today')", $conn); if( $ok ){ $up = mysql_query("UPDATE studtable SET checked = 'checkin' where [ID] = '%ID'); if(mysql_affected_rows( $up ) == 1) { echo'Checked IN!' }else { echo'No such student to check in' }} else { echo'Check In FAILED, please check again'}?>[php]
View Replies !
Delete MySql Row By Date
Can someone help me with the following problem: I have a table that includes a column for "last application date", let's simply call it "date". Now the problem is how to automatically delete rows where the information in the "date" column has gone old, let's say by -86400 (1 day). Example: today's date is April 11th, which means that all rows with the info April 10th in the date column would get deleted.
View Replies !
How Do You Do A CASCADE Delete In MySQL?
I'd like to delete a record and all its children records at one time. How do I do that? Can you, in one SQL statement, delete from table 1 where id = 3 delete from table 2 where id = 12 delete from table 3 where id = 12 ......
View Replies !
MySQL - Delete Across Two Tables. OT
i cant find the MySQL newsgroup, however i am hoping to pick up on some expert advice from php/mysql gurus here. I'm having some trouble performing a delete across two tables. The tables i have are: questionnaires (id, name); questionnaire_questions (questionnaires_id, id, name, qf_type) The questionnaire_questions table contains a list of questions for a specific questionnaire (indicated by the questonnaires_id). I would like to remove a questionnaire, and when doing so all corresponding questionnaire_questions who have matching id's (questionnaires.id = questionnaire_questions.questionnaires_id). I have this statement, however it only works if a questionnaire has questions... however i would like it to delete even if no questions exist for that specific questionnaire. The SQL i am using is: delete questionnaires, questionnaire_questions FROM questionnaires, questionnaire_questions WHERE questionnaires.id = questionnaire_questions.questionnaires_id AND questionnaires.id = THE_QUESTIONAIRE_TO_DELETE_ID
View Replies !
DELETE Problem With MySQL
I've been learning using PHP and MySQL from the tutorial at freewebmasterhelp.com. Everything works fine, except deleting things. The script is invoked with a URL like this: http://www.example.org/test/delete_entry.php?id=35 And here's the script: <? include("dbinfo.inc.php"); $id = $_POST['id']; mysql_connect($server, $user, $password); $query = "DELETE FROM test1 WHERE id = '$id'"; @mysql_select_db($database) or die("Unable to select database."); mysql_query($query); mysql_close(); header("Location: entries.php"); ?> The dbinfo.inc.php file defines the variables $server, $database, $user and $password. The login part works.
View Replies !
Mysql Delete From A Form
i'm trying to find out how to get this to work. You type in a name of a record in the database and it removes it. In this case it's deleting the record that when you type in the name of the server it looks for it in the column "servername". You type in the servername and it will delete the whole record. Code:
View Replies !
|