Replace table value in fields - php

I have a database name q8marketz there is a table product inside. This table has many fields eg:- product id, model, quantity, shipping, etc...
I have uploaded many products to my table... there is a field shipping, the shipping value is already set to 1for all products, so now I want to set all product shipping values to 0.
anybody can help me?
Sorry my English is not perfect...
Two product details given below...
===Database q8marketz
== Table structure for table product
|------
|Field|Type|Null|Default
|------
|//**product_id**//|int(11)|No|
|model|varchar(64)|No|
|downpayment|varchar(32)|No|
|sku|varchar(64)|No|
|upc|varchar(12)|No|
|ean|varchar(14)|No|
|jan|varchar(13)|No|
|isbn|varchar(13)|No|
|mpn|varchar(64)|No|
|location|varchar(128)|No|
|quantity|int(4)|No|0
|stock_status_id|int(11)|No|
|image|varchar(255)|Yes|NULL
|manufacturer_id|int(11)|No|
|shipping|tinyint(1)|No|1
|price|decimal(15,4)|No|0.0000
|points|int(8)|No|0
|tax_class_id|int(11)|No|
|date_available|date|No|
|weight|decimal(15,8)|No|0.00000000
|weight_class_id|int(11)|No|0
|length|decimal(15,8)|No|0.00000000
|width|decimal(15,8)|No|0.00000000
|height|decimal(15,8)|No|0.00000000
|length_class_id|int(11)|No|0
|subtract|tinyint(1)|No|1
|minimum|int(11)|No|1
|sort_order|int(11)|No|0
|status|tinyint(1)|No|0
|date_added|datetime|No|0000-00-00 00:00:00
|date_modified|datetime|No|0000-00-00 00:00:00
|viewed|int(5)|No|0
== Dumping data for table product
|886|BA-HANDBAGS-001ss| | | | | | | |aaaa|95|7|data/hand-bags.jpg|7|1|32.5000|25|0|2012-09-03|0.00000000|1|0.00000000|0.00000000|0.00000000|1|1|1|1|1|2012-09-25 13:00:18|0000-00-00 00:00:00|0
|883|BA-HANDBAGS-001ss| | | | | | | |aaaa|95|7|data/hand-bags.jpg|7|1|32.5000|25|0|2012-09-03|0.00000000|1|0.00000000|0.00000000|0.00000000|1|1|1|1|1|2012-09-17 14:08:08|2012-09-25 13:00:06|9
== Table structure for table product
|------
|Field|Type|Null|Default
|------
|//**product_id**//|int(11)|No|
|model|varchar(64)|No|
|downpayment|varchar(32)|No|
|sku|varchar(64)|No|
|upc|varchar(12)|No|
|ean|varchar(14)|No|
|jan|varchar(13)|No|
|isbn|varchar(13)|No|
|mpn|varchar(64)|No|
|location|varchar(128)|No|
|quantity|int(4)|No|0
|stock_status_id|int(11)|No|
|image|varchar(255)|Yes|NULL
|manufacturer_id|int(11)|No|
|shipping|tinyint(1)|No|1
|price|decimal(15,4)|No|0.0000
|points|int(8)|No|0
|tax_class_id|int(11)|No|
|date_available|date|No|
|weight|decimal(15,8)|No|0.00000000
|weight_class_id|int(11)|No|0
|length|decimal(15,8)|No|0.00000000
|width|decimal(15,8)|No|0.00000000
|height|decimal(15,8)|No|0.00000000
|length_class_id|int(11)|No|0
|subtract|tinyint(1)|No|1
|minimum|int(11)|No|1
|sort_order|int(11)|No|0
|status|tinyint(1)|No|0
|date_added|datetime|No|0000-00-00 00:00:00
|date_modified|datetime|No|0000-00-00 00:00:00
|viewed|int(5)|No|0

You can easily use an update query like this:
update product set shipping=0;
This will update all the rows to 0.
If you want to only specify certain selected rows, you can add a where clause to limit the number of rows that you update like this:
update product set shipping=0 where points>2;
This would update all the rows where points has a value greater than 2.
Edit: The code can be run directly from the mysql console, or via a database call from within PHP like this:
$dbh = new PDO($hostname, $username, $password);
$sql='update product set shipping=0 where points>2';
$stmt = $dbh->query($sql);

You can use the SQL UPDATE statement to update existing records in your table.
SQL UPDATE Syntax :
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
For your examlpe use :
UPDATE product
SET shipping=0;
==>The code can be run directly from the mysql console.
To use PHP Call try the code:
<?php
$dataBase = new PDO($hostname, $username, $password);
$sqlQuery ='update product set shipping=0';
$stmt = $dataBase->query($sqlQuery );
?>

Related

Possible to insert into two tables where one of the table is shortage of auto_increment_ID?

Table Product
pid product_name
Table Category
cid category_name
Table Product_category
cid pid
For table Product and Category, their primary keys are "pid and cid" with auto_increment
For product_category table, its used to store the data where 1 product is assigned to multiple categories and thats why the table columns are "cid and pid"
I am using php+mysql to write some functions like creating new product, category and then assign product to one or multiple categories.
Assume that we have created a 2 categories with cid-> 1,2
3 steps to assign product to category
add new product so that we have pid->123
then assign this product into category cid->1
insert into database table Product_category with pid->123 and cid->1
I want to do something simple like this instead
1. add new product and assign this product to category at the same time
However, for Table Product_category, it needs 2 value which are pid and cid , so that means we only know this pid after creating new product as its auto_increment. So my question is how to know this pid when adding new product? OR its impossible to do it this way?
public function conn(){
$this->db_host="localhost";
$this->db_name="bs";
$this->db_user_name="root";
$this->db_pass="";
return mysqli_connect($this->db_host, $this->db_user_name, $this->db_pass, $this->db_name);
}
$query = "INSERT item(uid,item_id,item_name,item_price,item_quantity)
VALUES('$this->user_uid','$this->item_id','$this->item_name','$this->item_price','$this->item_quantity')";
if (!mysqli_query($this->conn(), $query)) {
echo "Failed to Add item!" . mysqli_error($query);
return false;
}else{
printf("Last inserted record has id %d\n", mysqli_insert_id($this->conn()));
return true;
}
I try to test by adding new records, but it always get this
"Last inserted record has id 0"
The cause of your mistake is the method conn. Because you create a new connection for every call $this->conn(). A new connection doesn't know anything about INSERT statement that was performed in previous connection that is why mysqli_insert_id returns 0. A quick solution is to define a new var $conn = $this->conn(); and use this one in all queries.
I think your SQL required mysql_last_id so it should be like this
INSERT INTO Product VALUES(null, product_name);
$product_last_id = LAST_INSERT_ID();
INSERT INTO Category VALUES($product_last_id, category_name);
$category_last_id = LAST_INSERT_ID();
INSERT INTO Product_category VALUES($product_last_id, $category_last_id);
Have a look at LAST_INSERT_ID().
You need to set 2 database call first is product table. Insert a product. Then you will get the return value as pid, and I think you will get the cid from the page itself. You will select the option the new products belongs to which category then you can pass the cid. Then you can set the second database call insertProduct_categoryTable(cid,pid).

Insert IP address of downloaded files into mysql

By according to this question I have 2 table Source and details .
The Source table is as follows:
+----+----------+---------------+-----------+
| id | item_name|items_download | category |
+----+----------+---------------+-----------+
| |
+----+----------+---------------+-----------+
The details table is as follows:
+------+----------+-----+------+
| name | download | time| ip |
+------+----------+-----+------+
| |
+------+----------+-----+------+
At first step I want to get data from Source table (in real time) and put into details table by this code:
$get= "INSERT INTO `details` (`name`, `download`) SELECT `Source`.`item_name`,`Source`.`items_download` FROM `Source`"
At next step I want to get visitor IP address for each file.
for example if someone downloaded testfile I want to have this output:
+----------+---------+--------------+-----------+
| name | download | time | ip |
+----------+----------+-------------+-----------+
| testfile | 32 |download time|192.168.0.0|
+----------+----------+-------------+-----------+
| file2 | 0 | | |
+----------+----------+-------------+-----------+
To do this i use this code:
$ip = $_SERVER['REMOTE_ADDR'];
$update = "UPDATE details SET ip = '$ip', dldate = NOW()"
But its happened for all files, all of the file get same IP and time. I know its need a condition WHERE but I don't know what should I type as a condition to get IP address for each file that download.
Imho you don't need any UPDATE query. You just do an INSERT everytime a user requests a file:
<?php
$fileid = $_GET['fileid'];
$pdo = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');
$sql = "SELECT * FROM Source WHERE id=" . (int)$fileid;
foreach ($pdo->query($sql) as $row) {
$statement = $pdo->prepare("INSERT INTO details (name, download, time, ip) VALUES (?, ?, NOW(), ?)");
$statement->execute(array(
$row['item_name'],
$row['items_download'],
$_SERVER['REMOTE_ADDR'],
));
}
?>
Some hints on the code above:
Use prepared statements - never inject any value directly into an SQL string.
It might be useless to insert file_name and items_download into the details table everytime. You have this information in your table "Source" anyway. So usually you would just put Source.id into your details table.
You should use your id column, which you have in your first table view, but it stragely disappears in the later ones.
Your id column should also be your PRIMARY_KEY AUTO INCREMENT column. Then each row has its own unique and indexed id.
But aside from that, what do you use to identify which file the user downloads? If it's the filename then simply use that:
EDIT: Add an id column to your details table!
$update = "UPDATE details SET ip = '$ip', dldate = NOW()
WHERE name = '$fileNameValue' LIMIT 1"
On a related note, you can not update multiple columns with the same shorthand reference of device = ip = '$ip' you have to specify each column in isolation and with an absolute target data to insert (here, the variable) .
If this is a typo you should edit and update your question.
Please also see Gerfried's answer regarding using prepared statements, they are the way you should be doing these things.
I think you need to get the session of user when he downloads the file and add it to the WHERE condition.

Replace to another column if exits in MySql

I have a HTML table having values from Mysql. My HTML table looks like this
------- ------ ------ ------
type name b_id Edit
------- ------ ------ ------
Laptop mac E1:23 edit
Desktop dell D2:45 edit
I am inserting values from input textboxes for type and name. And a select dropdown for b_id.
I can edit each row values by clicking editand i have a dropdown displaying all my b_id's.
I need to update b_id values so that, if it already exists replace with new delete the old.
For suppose i'l hit edit on Desktop dell row. And i'l select E1:23(which is already mapped for laptop mac). On saving this, E1:23 should map to desktop dell and previous mapping (laptop mac) should be deleted. I mean it should be empty
My Query
$id = $_REQUEST['id']; // Auto Increment
$b_id = $_REQUEST['b_id']; //b_id from select dropdown on edit
$sql = "SELECT * FROM table_name WHERE b_id='$b_id'";
$rs=parent::_executeQuery($sql);
$rs=parent::getAll($rs);
if($rs) // if b_id already exists
{
//First empty old b_id which has mapped already
$sql2 = "UPDATE table_name SET b_id='$b_id' WHERE id='$id'";
$rs=parent::_executeQuery($sql);
}
So how to empty previous mapped b_id. Not the type and name
If you want to empty the data in b_id, your query should be something like this:
if($rs) // if b_id already exists
{
//First empty old b_id which has mapped already
$sql2 = "UPDATE table_name SET b_id='' WHERE b_id='$b_id'";
$rs=parent::_executeQuery($sql2);
}

SQL - Inserting data where values are an array

I want to be able to add an array of strings to a table so that each string is a new row (in PHP).
This is it in psuedo-code:
$Array = "10000,10001,10002,10003";
$Data = "ImportantData";
mysqli_query($db, "INSERT INTO MyTable(`id`,`data`) VALUES($Array, $Data)");
So that a previously empty table would look like:
id | data
------------------------
10000 | ImportantData
10001 | ImportantData
10002 | ImportantData
10003 | ImportantData
In an update script, with those rows already established, I could just say:
mysqli_query($db, "UPDATE MyTable SET data = $Data WHERE `id` IN($Array));
However I want it to create rows, not just update old ones.
Is there any way I can do this?
Just create a foreach loop on $Array, and insert the data. I assume you want to update it if it exists as it makes little sense to create a new record with the same PK, so use the following (assumes you are using PHP PDO
INSERT INTO MyTable (id,data) VALUES (:id,:data) ON DUPLICATE KEY UPDATE data=:data;
Use REPLACE INTO:
REPLACE INTO table SET id = 10001, data = 'new important data';
MySQL documentation: http://dev.mysql.com/doc/refman/5.0/en/replace.html

Need MYSQL query to insert value in different columns?

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

Categories