I am using Php to insert values into MySQL table.
What i am trying to do is:
There are three columns that i have to check. 'namel1', 'namel2' and 'namel3'.
Conditions:
If '$name' does't exist in any of the three column then put value in 'namel1'.
If '$name' exist in 'namel1' then put value in 'namel2' and if 'namel2' contains the value then put it in 'namel3'.
My current MySQL query to insert name and image path is this i want to modify it to meet above conditions:
$chk_img_db = mysql_query("select * from cvapptable where img_path='$cvh_myimg_url'");
if(mysql_num_rows($chk_img_db)<1) {
mysql_query("insert into cvapptable(namel1,img_path) values ('$name','$cvh_myimg_url')");
}
I unable to get any solution from web.
Please help. Thank you.
It's not easy to find on the net because it's a situation you shouldn't get yourself into.
You should consider normalizing the table.
Instead of having a table with the columns:
cvapp: id | img_path | namel1 | namel2 | namel3
Consider changing it to two tables:
cvapp: id | img_path
names: id | cvapp_id | name
To then select every name, you just do a query like so:
SELECT name
FROM cvapp INNER JOIN names on cvapp.id = names.cvapp_id
WHERE <condition>
That way, you can have as many names as you want, and it's much easier to insert a new one:
INSERT INTO names (cvapp_id, name) VALUES (56, "Name 1");
INSERT INTO names (cvapp_id, name) VALUES (56, "Name 2");
INSERT INTO names (cvapp_id, name) VALUES (56, "Name 3");
you can try self join and search column of you tables
Related
so I'm storing order in my table like this:
ID, NAME, ORDER
1, name_1, 1000
2, name_2, 2000
3, name_3, 3000
so basically:
ORDER = ID*1000
and what I do is after inserting row into this table like this
INSERT INTO table(name) VALUES('name_x');
I update order value in this row like this:
UPDATE table SET order = mysql_insert_id()*1000 WHERE id = mysql_insert_id();
Is there any other way to not make this into two statements? Like some magical mysql function ACT_ID()
so it would be like
INSERT INTO table(name, order) VALUES('name_x',ACT_ID()*1000);
Thanks in advance for all your responses.
Yes there is:
INSERT INTO tbl2 (id,text) VALUES(LAST_INSERT_ID(),'text');
See MySQL Docs here
I am using this auto complete form, that gets the data from 1 table,
now i am using that form to insert data from its table to another table.
here is my SQL for the inserting into the table "products"
$image = addslashes(file_get_contents($_FILES['prod_pic']['tmp_name']));
$sql="INSERT INTO `inventory` (`prod_brand`,`prod_name`,`prod_category`,`prod_price`,`prod_desc`,`prod_quantity`,`prod_pic`)
VALUES
('$_POST[prod_brand]','".mysql_real_escape_string($_POST['prod_name'])."','$_POST[prod_category]' ,'$_POST[prod_price]',
'".mysql_real_escape_string($_POST['prod_desc'])."','$_POST[prod_quantity]','{$image}')";
the prod_category is the column i need to fill. I have data from the table named "categories" with column name "categories"
so how do i input the data from categories to the column = prod_category in the products table?
Check example and try this way...may it's help you.
INSERT INTO student (s_id, s_name, s_email)
SELECT t_id, t_name, t_email FROM teacher
WHERE teaher.tid='25';
As i understand in POST['prod_category '] contain string, like you have text input. The best way to do what you need - is change category for select in html, like this
<select>
foreach(categories as $category){
<option value='category->id' >category1->name </option>
}
</select>
Then you will get in post category id from the table named "categories"
If you dont like it your should replace '$_POST[prod_category]' from youre query to subquery
select id from categories where categories = '$_POST[prod_category]'
I´m trying to insert data into diferent tabels with php, but dosent get it to work.
Heres my db structure: 1
Table: event
Structure: FromDate | ToDate | locationID
Heres my db structure: 2
Table: jointable
Structure: EventID | locationID
The thing i want to do more specific, i have inputs for "Fromdate" and "todate" and "locationid". I want to input fromdate and todate into table1, and locaionid to table2
Here is my sql query:
mysql_query("INSERT INTO event (FromDate, ToDate, locationID)
VALUES ('$_POST[startdate]','$_POST[enddate]','$_POST[locationID)");
Any idea how i can "sort out" locationID to input it on my jointable instead?
Excuse for my bad enlish, i hope you understand what im trying to do.
Try this:
mysql_query("INSERT INTO `events` VALUES('".$_POST["startdate"]."','".$_POST["enddate"]."','".$_POST["locationID"]."')");
Call another query for inserting into other table:
mysql_query("INSERT INTO `jointable` VALUES('','".$_POST["locationID"]."')");
Write 2 different queries
Something Like This :
$startDate = $_POST[start_date];
$endDate = $_POST[to_date]
$locationId = $_POST[location_id];
mysql_query("INSERT INTO event VALUES ('$startDate','$endDate','$locationId");
mysql_query("INSERT INTO jointable VALUES ('','$locationId')");
Note : mysqL_* functions are being depreciated . Avoid the,
I have some insurance information in a website, and I'd like to only edit certain fields the user wants to change like for example:
user, id, phone, address, city
and the user wants to change his city and phone...do i have to make a query for each specific case or is there a code that can help me retrieve the key(phone) and value (9397171602)??
to then send it in a query
Basic update would take the form of:
UPDATE table_name SET column_1 = value_1, column_2 = value_2 WHERE column_3 = value_3
Where col1, col2 would be your city and phone, and col3 would be the user id. Check out the MySQL website http://dev.mysql.com/doc/refman/5.0/en/update.html for more info
There are number of ways to update a record safely. Conside the following pseudo code + php program.
class Example
{
function UpdateRecord($editedRecord)
{
//Fetch existing record from the database
$originalRecord=DB::fetchExample($editedRecord->getId())
//validate each edited field and it is valid then assign its value
//to the originalRecord's field
if(IsValid($editedRecord->getName())
{
$originalRecord->setName($editedRecord->getName());
}
.....
//update the record.
$originalRecord->Update();
}
}
Just add some sql to it:
$sql = "UPDATE example SET col_1 = val_1, col_9 = val_9 WHERE col_7 = val_7";
mysql_query($sql);
Then replace the columns and values with you stuff. For further info: PHP MySql Update
How can I uniquely identify two or more columns, that I have used table named address in the database, now address is has fields like street name, suite name and street num.
strnum | strnam | sutname
1 | xyz | 32
1 | xyz | 32
now how can I uniquely these three columns. That is I want to check whether these three column are already inserted or not. If any field valus is changed than its ok, it will insert new one. but in case all three similar field..Help me to combinely identify these three fields.
You do it by adding unique constraint.
ALTER TABLE your_table ADD UNIQUE(strnum, strnam, sutname);
Then you do the following:
INSERT IGNORE INTO your_table (strnum, strnam, sutname) VALUES ('1', 'xyz', 'etc');
If the value exists already - no insert will happen and no errors will be raised (that's what the IGNORE part is).
By the way why do you use such short and vague column names? It's not the DOS era any more, you can be descriptive with your column names.
$query = "SELECT * FROM `address` WHERE `strnum` = '$strnum' AND `strnam` = '$strnam' AND `sutname` = '$sutname' LIMIT 1";
$result = mysql_query($query);
if (!mysql_num_rows($result)) {
// If you get to here, there is no existing record
$query = "INSERT INTO `address` (`strnum`,`strnam`,`sutname`) VALUES ('$strnum','$strnam','$sutname')";
if (!mysql_query($query)) print('Insert failed!');
} else print('Record already exists!');
EDIT I just added a missing ; so this parses...
just add them as unique keys in table structure and you'll not be able to insert two of them
you can do something like this
SELECT * FROM table WHERE col1 = $something AND col2 = $something2 AND col3 = $something3
(remember about escpaing php variables)
if the record is returned it means it exists. You can also add LIMIT 1 to make it faster.
if your question is about ENSURING that no duplicates occur in the table (for those 3 columns), then probably the best solution is to add UNIQUE index on those three columns.