I have a basic question for you experts,
I want to update a row in mysql database by using the User_Id of an entry. I created User_Id as auto increment, primary key. Now for the last user I calculated its necessary variables, now I want to add that variable to the last user's necessary column through its User_id.
The name of the variable I want to add is $improvement. I wrote the code below, however I dont now how to do that depending on its User_Id.
$sql = "
UPDATE users
SET Improvement='$improvement'
WHERE ... )
";
mysql_query($sql, $accounts);
I dont know what to write to the "..." part in the code. Please give me feedback if you see a problem in my question. I can give my table if you need.
mysql updating last inserted id
I guess I found the answer in that link:
$sql = "
UPDATE users
SET Improvement='$improvement'
WHERE User_id = LAST_INSERT_ID() )
";
However I am not sure if "SET Improvement='$improvement'" part is the correct syntax
If your user_id is primary index then you can use
$sql = "
UPDATE users
SET Improvement='$improvement'
order by user_id DESC limit 1 )
";
mysql_query($sql, $accounts);
Related
Trying to put only the value of column id from table existence:
$sql_query_existence = "SELECT * FROM existence ORDER BY id DESC LIMIT 1";
$sql_test = $sql_query_existence['id'];
The second line is my idea, how do I do this right? Any suggestion is appreciated.
So you want to get the value of the id column? If so, use this query
SELECT id FROM existence ORDER BY id DESC LIMIT 1;
and if you want to set the value of the id column only, use either
INSERT INTO existence(id) VALUES (<Your ID>);
or
UPDATE existence SET id=<Your ID> WHERE ...;
This answer is SQL based since I don't know php, but this answer will help you do the necessary php work involved.
I want to show highest record id from mysql database on live. I am using following code and it's working on localhost but not on live site.
<?php
$q ="SELECT LAST_INSERT_ID()";
$result = mysqli_query($q);
$data = mysqli_fetch_array($result);
echo $data[0];
?>
As Jon mentioned you need to add MAX(). So did you setup your live database exactly the same as the localhost one? Maybe tell us the error?
(Can't comment yet, sorry)
use sub select query if id is unique.
select row
from table
where id=(
select max(id) from table
)
if id is not unique then use this.
select row from table ORDER BY id DESC LIMIT 1
you can use id column if you are using as PK and auto increment.
Select * from TABLE order BY id DECS LIMIT 1
if you are talking about concurrent queries then i have done something different to tackle this situation.
insert a timestamp in a new column and then fetch the same
please check the below code
$token= data();
insert into TABLE ('val1', 'val2', $token);
and then
you can user $token to get id of last inserted row by something like this
Select * from TABLE where token = $token
I have two main tables:
Products
Category
Then I have a 3rd many-to-many table called Prices with the rows:
IdProducts (Primary key and Foreign Key from Products)
IdCategory (Primary key and Foreign Key from Category)
Price
The 3rd table doesn't have an individual ID for each row, just those 3 rows.
That said, I've already been able to insert data with no problems on the 3rd table using the data from Products and Category. Now I'm trying to edit this data in a small program but I just can't get if there is a coincidence to pre-fill the edit form. I'm using the following code:
$sql1 = mysqli_query($con,"SELECT * FROM table3 WHERE (idProducts='$variable1' AND idCategory='$variable2') LIMIT 1") or die(mysql_error());
$PriceCount = mysqli_num_rows($sql1);
if($productCount>0){
while($row = mysqli_fetch_array($sql1)){
$priceshow= $row["price"];
$idCategoryshow= $row["idCategory"];
$idProductshow= $row["idProduct"];
}
}
So with that I could get the price, category id, and product id ready to show in html.
I don't know if I'm doing something really wrong but my best bet is that the error is in the MySql query:
"SELECT * FROM table3 WHERE (idProduct='$variable1' AND idCategory='$variable2') LIMIT 1"
This returns no values even if I send data that I know it should be using.
Is that how I'm supposed to try to get all the rows? Or maybe something like this can't be done and I need an individual ID for the prices?
EDIT: I found a dirty work around by sending the variables on a href Link... anyway Im facing the almost very same problem when trying to update the desired row, mysql code for update is this:
"UPDATE table3 SET (price1='$var1' AND price2='$var2') WHERE (price.idProduct='$idProduct' AND price.idCategory='$idCategory') "
and i recieve the following error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET (price='123231211.00' AND price='1212654.00') WHERE (table3.idProduct='QEW21' AND ta' at line 1
Finally I can go to sleep xd
"UPDATE table3 SET price1='$var1', price2='$var2' WHERE
price.idProduct='$idProduct' AND price.idCategory='$idCategory'"
I was using "SET(X AND Y)" and "WHERE(X AND Y)" and checking the right uses of AND ends up it was actually like: "SET X, Y" and "WHERE X AND Y"
I want to log what a user enters into a PHP form, and make sure they are not entering data that already exists in a database table.
I have the code already that enters the data into the table from user input, but I'm not sure how to check for duplicates. For example I want to check that there is no product under the same name being added again.
This is what I have so far:
$sql = "INSERT INTO user_data (product_name, code, comments)
VALUES ('$_POST[product_name]','$_POST[code]','$_POST[comments]')";
This is terrible SQL coding practice and, as stated before, is vulnerable to SQL Injection Attacks but something along these lines should work.
$sql = "
INSERT INTO user_date
SELECT
product_name = '$_POST[product_name]'
,code = '$_POST[code]'
,comments = '$_POST[comments]'
WHERE
NOT EXISTS(SELECT * FROM user_data WHERE product_name = '$_POST[product_name]') ";
The best way to do this is by adding a uniqueness constraint to your table itself. This way you can prevent duplicate records from being added.
In your case I would go with this:
ALTER TABLE `user_data` ADD UNIQUE (
`product_name`
)
Also, you could check for the record before you add it:
$sql="SELECT count(*) AS number FROM user_data WHERE product_name LIKE '".$_POST['product_name']."'";
If the column "number" 1 (or bigger), you know it already exists. Also when you get 2, or bigger, you will know that you already have duplicate records.
I'm currently using:
SELECT MAX(id) FROM table
To discover the current id of a certain table, but i heard this can bring bad results. What is the proper way of doing that? Please, notice that i'm not INSERTING or DELETING anything before that query. I just want to know the current ID, without prior INSERT or DELETE.
Perform the following SQL:
SHOW TABLE STATUS LIKE 'TABLENAME'
Then check field AUTO_INCREMENT
You can use the following query:
SELECT id FROM table ORDER BY id DESC LIMIT 1;