I'm attempting to copy a record from one table to another (and eventually deleting the original with one click) I basically have a repair tracking page, when those repairs are completed I want to remove that record by filling a checkbox and clicking the submit button, and move that record to the archive/history table. I've been successful in copying the records, however every record is copied, I'm guessing the issue is related to my WHERE clause
mysql_select_db("testmoverecord", $con);
$id = $_POST['checkbox'];
$sql =("INSERT INTO history (id, firstname, lastname, age)
SELECT repair.id, repair.firstname, repair.lastname, repair.age FROM repair
WHERE id = $id");
echo "<input type='checkbox' id='checkbox' value='{$row['id']}' name='checkbox'";
How do I pass the ID correctly? I tried WHERE repair.id = history.id but it says Unknown column even though it exists. I've tried putting in a hidden input for id and posting the id but haven't got those to work. I'm using mysql, it's open to injection blah blah blah, save yourself the typing, I plan on updating soon, it cannot be touched remotely and its storing data that isn't even close to being sensitive so I don't mind. Could someone please point me in the right direction? Thanks for any assistance.
EDITED: This code works
$sql = sprintf("INSERT INTO history (id, firstname, lastname, age)
SELECT * FROM repair
WHERE id = '%s'",
mysql_real_escape_string($id));
Related
For a class assignment I had to create a database and put 3 entries in. There were 8 fields to have information input, so to save time and not retype everything, I just hit the up arrow to modify the input, but I forgot to change the ID, so all 3 entries have the ID of 1.
My original code to for input into my table was.
Insert into People (id, FirstName, LastName, StreetAddr, City, State, Zip, Phone)
Values (1, "My firstname", "My lastname", "123 Main Street", "my City", "My State", "my Zip", "My phone number");
So, in my attempt to save time I messed up and put 1 as all 3 ID numbers, and now I don't know the correct way to edit a single part of an entry.
Here's a pop quiz that might get you to the point of fixing your problem for yourself...
What does your CREATE TABLE say?
Did the teacher talk about PRIMARY KEY?
Shouldn't id by AUTO_INCREMENT?
What value do you feed to id to get it to increment automatically?
What happens if you leave id and 1 out of the INSERT?
You can fix it by updating the values.
UPDATE People SET id = <put new id here> WHERE FirstName = '<FirstName goes here>' AND LastName = '<LastName goes here>';
This assumes that the FirstName and LastName values are unique enough to find only a single row. Replace the WHERE condition values to match the correct ones and choose a new value for the id.
Read the UPDATE documentation for more information about the syntax.
I'm working on a software for managing the Rally company of my boss, where he can manage all the volunteers, their affectations, and many other things.
But the volunteers and these others things vary depending on the event, so they all have a column representing the event that they are linked/related to.
My boss requested that I add a "duplicate" button, that would duplicate an event (from my events table) and also duplicate all the volunteers, and values from any other table that is linked to that event, so the new duplications are linked to the new event.
The reason for this, is that he is constantly organizing rallies, and often it happens that the data from a Rally (event) to another is almost the same, so instead of adding it all manually, he'd rather make an entire duplication of the event and all the data related to it, then manually add and remove the errors in it.
I would like to know, is there any way in MySQL that I could duplicate an event, and everything that is linked to it's ID, even though they are in different tables, and make the duplications have the ID of the new event?
Sadly I don't have very much time, but until I get an answer I can work on other tasks my boss gave me.
Thank you so much to anybody who helps me or gives me any hint!!
EDIT:
Here's the schema of my Database (I know it's kinda dirty and there's issues with it, my boss gave me indications on how to create the database since he used to work in the domain before, but he didn't tell me how to make the links and he wants to make them himself)
And I apologize for the French language and the weird names..
Basically I wish to duplicate an entry in the "event" table, all the "affect" and "lieu" entries that are linked to it, and all the "tache" entries related the the duplicated "lieu" entries.
EDIT 2:
Thank you MrMadsen for the Query!
I had to fix it a bit, but here's what it looks like after.
SET #NEWEVEN = (SELECT MAX(NO_EVE) from db_rallye.event)+1;
INSERT INTO db_rallye.event
SELECT #NEWEVEN,
NM_EVE,
AN_EVE,
DT_EVE,
NM_REG_EVE
FROM event
WHERE NO_EVE = event_to_duplicate;
SET #NO_AFFECT_LIEU = (SELECT MAX(NO_AFF) from db_rallye.affect);
INSERT INTO db_rallye.affect
SELECT #NO_AFFECT_LIEU:=#NO_AFFECT_LIEU+1,
CO_AFF,
DT_AVI_AFF,
DS_STA_AFF,
CO_STA_AFF,
NO_BRA_EVN,
NO_PERS,
#NEWEVEN,
NO_EQU,
NO_LIE,
NO_PERS_RES,
IN_LUN,
IN_BAN,
DS_HEB,
IN_HEB_JEU,
IN_HEB_VEN,
IN_HEB_SAM,
NO_BOR,
NO_TUL_CAH,
NB_SPEC
FROM affect
WHERE NO_EVEN = event_to_duplicate;
INSERT INTO db_rallye.lieu
SELECT NO_LIE,
#NEWEVEN,
CO_LAT,
CO_LON,
FI_IMA,
FI_CRO,
FI_TUL,
DS_LIE,
DS_COU,
DS_LON,
NB_BLK,
VL_KM,
IN_FUS,
VL_DIS_FUS,
NO_LIE_FUS_SUI
FROM lieu
WHERE NO_EVEN = event_to_duplicate;
SET #NO_AFFECT_TACHE = (SELECT MAX(NO_TAC) from db_rallye.tache);
INSERT INTO db_rallye.tache
SELECT #NO_AFFECT_TACHE:=#NO_AFFECT_TACHE+1,
NO_LIE,
#NEWEVEN,
NO_AFF,
DS_REP,
DS_TAC
FROM tache
WHERE NO_LIE IN
(SELECT NO_LIE FROM lieu WHERE NO_EVEN = event_to_duplicate);
If a lot of the events contain similar information than I would create a template (or templates and the ability to create/modify templates) containing all of the information that you would duplicate.
Then when a new event is created he can just choose a starting template and then only add whatever data is unique to that event. In my opinion this would be much better than constantly duplicating the data.
As far as how to duplicate a row and all the associated rows, this is completely dependent on your database schema and how the tables relate to one another. If you post the relevant part of that we can help you more.
Edit
Here are the queries I came up with, test them in a dev database first but I think they will work. Let me know. Good luck!
INSERT INTO `event`
SELECT NULL NO_EVE,
NM_EVE,
AN_EVE,
DT_EVE,
NM_REG_EVE
FROM `email_log`
WHERE id NO_EVE = id_of_event_to_duplicate
INSERT INTO `affect`
SELECT NULL NO_AFF,
CO_AFF,
DT_AVI_AFF,
DS_STA_AFF,
CO_STA_AFF,
NO_BRA_EVN,
NO_PERS,
NO_EVEN,
NO_EQU,
NO_LIE,
NO_PERS_RES,
IN_LUN,
IN_BAN,
DS_HEB_JEU,
IN_HEB_VEN,
IN_HEB_SAM,
NO_BOR,
NO_TUL_CAH,
NB_SPEC
FROM `affect`
WHERE NO_EVE = id_of_event_to_duplicate
INSERT INTO `lieu`
SELECT NULL NO_LIE,
NO_EVEN,
CO_LAT,
CO_LON,
FI_IMA,
FI_CRO,
FI_TUL,
DS_LIE,
DS_COU,
DS_LON,
DB_BLK,
VL_KM,
IN_FUS,
VL_DIS_FUS,
NO_LIE_FUS_SUI
FROM `lieu`
WHERE NO_EVEN = id_of_event_to_duplicate
INSERT INTO `tache`
SELECT NULL NO_TAC,
NO_LIE,
NO_EVEN,
NO_AFF,
DS_REP,
DS_TAC
FROM `tache`
WHERE NO_LIE IN
(SELECT NO_LIE FROM `lieu` WHERE NO_EVEN = id_of_event_to_duplicate)
I have a sessions table and what I am trying to do is run a query to check if a certain account is logged in.
My query looks like this:
SELECT id FROM sessions WHERE data LIKE '%4%'
But with a query like that, if the user_id of "14" is logged in, that query will return True. How do I check to see if a certain number exists and matches exactly?
Include the delimiters in your search:
WHERE data LIKE '%:4;%'
why don't you add another field for user_id to the session table?
EXAMPLE:
I have a site where a user can only be logged in from one location at a time. If they try and log in from another location (IE start a new session) then i kill all of their previous logins.
So as part of my login script:
// log this user out of any other active sessions
$sql = sprintf("DELETE
FROM sessions
WHERE id_user=%s",
mysql_real_escape_string($_SESSION['id_user'])
);
// Associate this session with this user
$sql = sprintf("UPDATE sessions
SET id_user=%s
WHERE id=%s",
mysql_real_escape_string($_SESSION['id_user']),
session_id()
);
and so i can have id_user as an additional field in my session FKed to my user table... its a little more normalized and lets you do quick "Who is currently using this site" queries without too much parsing and fuss.
I have a site where users can log-in and add items to a list.
The user logs in and the session stores their e-mail, which I use to identify them in a user table.
Then, they can input a list item and add to a list table that contains their ID and their list items.
Sometimes the ID is added and sometimes it comes up null (however, the list item text is always added). This seems erratic because most of the time the ID is included.
Any ideas? The table type is MyISAM. I'm new to programming, btw.
Here's an example of my code:
<?php
session_start();
$item = $_REQUEST['item'];
$email = $_SESSION['email'];
if ($item)
{
mysql_connect("localhost","root","") or die("We couldn't connect!");
mysql_select_db("table");
$query = mysql_query("SELECT ID FROM users WHERE email='".$_SESSION['email']."'");
$result = mysql_result($query,0);
$user_id = $result;
mysql_query("INSERT INTO items (user_ID,item_name) VALUES('$user_id','$item')");
So every time I test it by logging into my site myself, no problems. But increasingly, I have users who try to add items and it creates a record where item_name shows up correctly but user_ID is set to 0 (default).
First off, read what I said about SQL injection attacks in the comments to your question.
It would be a good idea to store the user_id in the $_SESSION so you wouldn't have to query for it every time based on the email... but if you insist on just having the email in the $_SESSION, then you actually only need one query. Adjusted code:
<?php
session_start();
$item = mysql_real_escape_string($_REQUEST['item']);
if (!empty($item) && isset($_SESSION['email']))
{
mysql_connect("localhost","root","") or die("We couldn't connect!");
mysql_select_db("table");
mysql_query("INSERT INTO items (user_ID, item_name) VALUES ((SELECT ID FROM users WHERE email='{$_SESSION['email']}'), '$item')");
}
Like Jeff Watkins said, the session could be timed out, so it would be a good idea to first check if it's set using isset().
Otherwise if you stored the userid in the session, you could just reference it directly as $_SESSION['user_id'] instead of doing a subquery in the insert.
I am trying to "syphon" data over from one table to another. The problem I run into is that some of the data which is going in the new table is static, other data is being copied from an existing table. Let me start by showing the query I was trying to run:
INSERT INTO current_cart
(cart_ID,
cart_PO,
cart_user,
cart_date,
cart_qty,
cart_sku,
cart_description,
cart_price,
cart_linetotal)
VALUES
('$cartID',
'$poNumberNew',
'$email',
'$lineDate',
SELECT
orderdetail_qty,
orderdetail_sku,
orderdetail_description,
orderdetail_price,
orderdetail_linetotal
FROM orderdetail
WHERE orderdetail_custemail = $email AND orderdetail_po = $poNumber)
Obviously, all the PHP variables are declared beforehand. Essentially what I have running is a shopping cart. This query would take the items from a previous order and enter them in a new shopping cart so our customer can start a new order based on a previous order.
The problem I run into is how to insert records into a table when some of the data for the record is static (cartID, poNumberNew, email and lineDate) and other information is coming out of a different table? I am hoping I can do this without creating a loop to repeat a query for however many items there were in the order the customer is duplicating... I think that would bog down our site significantly.
I've seen a myriad of awesome answers on many other web dev questions I've had in the past, I'm hoping the stackoverflow community can help me out here...
Thanks!
in a SELECT statement, you can have a column that returns an constant. For example,
INSERT INTO current_cart
(cart_ID,
cart_PO,
cart_user,
cart_date,
cart_qty,
cart_sku,
cart_description,
cart_price,
cart_linetotal)
SELECT
'$cartID',
'$poNumberNew',
'$email',
'$lineDate',
orderdetail_qty,
orderdetail_sku,
orderdetail_description,
orderdetail_price,
orderdetail_linetotal
FROM orderdetail
WHERE orderdetail_custemail = $email AND orderdetail_po = $poNumber)