I have two tables one table has an id and a username the name of the table is user. I have another table called value that table has an id which is to store the id from the user table. The table valuealso has a column called value and item_id which is used to store the item.
I want to write an update statement that updates the following columns within value id, value, item_id however the values I have to execute the statement is username = $username, value = $value and item_id=$item_id (based on the application)
How can I write an update statement that stores the id (username id), value and item_id
The reason for using an update statement is because that user can change the value within the value column at any time
UPDATE `value` SET `value` = $value WHERE item_id = $item_id
AND user_id IN (SELECT id FROM `users` WHERE username = $username)
be sure to sanitize your database inputs.
this will only update, if you want to create the entry (i assume that user_id and item_id are the combined primary key of your second table), you can use a replace into statement.
please tell me if you need that, because i think it was not the question.
it may be not as efficient but i think that will not really matter, but you can of course fetch the user id first with a separate query.
this will probably be better understandable.
Try this
UPDATE value, user
SET value = $value
WHERE item_id = $item_id
AND value.user_id = user.user_id
AND user.username = $username;
Related
this is sign up form pls help me to solve this error
ERROR: Could not able to execute sql insert statement
INSERT INTO customer(customer_id,customer_name,customer_email,customer_password) VALUES (null,'','','').Column 'customer_id' cannot be null
//check connection
if($link===false){
die("ERROR: could not connect. " . mysqli_connect_error());
}
//Escape user inputs for security
$customer_id = mysqli_real_escape_string($link, $REQUEST['customerid']);
$customer_name = mysqli_real_escape_string($link, $REQUEST['Name']);
$customer_email = mysqli_real_escape_string($link, $REQUEST['Email']);
$customer_password = mysqli_real_escape_string($link, $REQUEST['Password']);
//attempt insert query execution
$sql="INSERT INTO customer(customer_id,customer_name,customer_email,customer_password) VALUES (null,'$customer_name','$customer_email','$customer_password')";
if(mysqli_query($link,$sql)){
echo "records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql.".mysqli_error($link);
}
mysqli_close($link);
?>
It says in your error message that the column customer_idcan not be null.
Looking at your query VALUES (null,'$customer_name','$customer_email','$customer_password')"; the values start after the parenthesis. So your first column is customer_id and the first value is null.
The error message tells you that your table is not setup to allow for customer_id to be null. So you either have to change to database table structure or your query. As it is about an ID I suggest you change your query.
If customer_id is an auto incremented field you should simply not set it (remove it from your query)
Edit:
It is not usual to have an identifier set to varchar. Seeing something like customer_id lets most people assume that they are dealing with an integer. The easiest approach would be to make that table column an integer with auto increment in your database (in most cases such a field would also be primary key).
Seeing that your table requires the field customer_id it can not be left out from your query and you would have to generate / create an adequate identifier for your customer. A workaround for your current table structure:
$sql="
INSERT INTO
customer
(customer_id,customer_name,customer_email,customer_password)
VALUES (
(select count(customer_id) from customer)+1,'$customer_name','$customer_email','$customer_password')";
Edit 2:
Some additional information on database column data types: mysql char vs. varchar and mysql integer
if you later on have a more complex database and query multiple tables at once to gather your data you will have to rely on your indices and realtions (foreign keys) to get the results fast. IE: having a join on your customer_id is rather slow. setting it to char lets it operate faster due to varchar having a variable length. I have not yet tested the performance regarding char and integer
You need to pass customer_id instead of null in the query, e.g.:
$sql="INSERT INTO customer(customer_id,customer_name,customer_email,customer_password)
VALUES ('$customer_id','$customer_name','$customer_email','$customer_password')";
You can remove customer_id column like below,
$sql="INSERT INTO customer(customer_name,customer_email,customer_password)
VALUES ('$customer_name','$customer_email','$customer_password')";
If you set customer_id as auto increment in database there no need to include it in query
If your customer_id is an autoincrement id of your customer table then, no need to put it inside your INSERT statement.
Simply run without customer_id
$sql="INSERT INTO customer(customer_name,customer_email,customer_password)
VALUES ('$customer_name','$customer_email','$customer_password')";
Ok. Let me explain you somethings.
Case 1 :
If your customer_id is an integer type and you have not put anything in default value then it will not allow you to insert a null into that field. For that, you need to set default value as NULL.
Case 2
If your customer_id is an autoincrement id of customer table then you can't set null into it during a SQL statement.
I have 2 tables. In the first i have id_user (that is primary key),username,name,password and in the second i have id_post(primary key),post,username,view,id(foreign key that references id_user).
After i use the query:
$query ="INSERT INTO posts (username,post,view) VALUES ('$username','$postare','$isprivate')";
It completes my column id with NULL. I want in the column id to have the id_user of the user that make the query.Not NULL.
You need to look up the value to get that in the data. So, using your construct the query would look something like this:
insert into posts (userid, post, view)
select u.userid, $postare, $isprivate
from users u
where u.username = '$username';
Notes:
Do not store both userid and username in the posts table. Only store the username in the users table and use userid to get the name when you need it.
Don't munge your query string by putting values directly in it. Learn to use parameterized queries.
view is a really bad name for a column, because it is an SQL keyword.
I am wondering if it is possible to insert from another table (which I have managed to do) whilst also inserting a VALUE of a variable from the current php file?
I am aiming to get the user ID from another table, which I have gotten from selecting the email from the user input. I then need to insert a hash which is automatically created via a variable.
This is my current code that gets the correct id from the users table.
$forgot = $pdo->prepare("
INSERT INTO
forgot (
user_id
) SELECT
id
FROM
users
WHERE
email = :email
");
Now I just need to insert the VALUE of :hash too.
Would this need to be done with a separate query?
Thanks.
Try the following:
INSERT INTO
forgot (
user_id, hash
) SELECT
id, :hash
FROM
users
WHERE
email = :email
I think I know the answer to this but want to get confirmation.
If you have a simple update query with a where clause and the value of the variable in the where clause is missing, does that mean every record gets updated?
//let's say $name is empty
UPDATE users SET name= 'Jason' WHERE userid = '$name'
is that the same as
UPDATE users SET name= 'Jason' WHERE userid = ''
Is the behavior that every record would get updated?
And, if so, is there anything you can put in the SQL to prevent this potential catastrophe?
Thanks for guidance.
Both queries will update all records where userid column is empty. But it doesn't means that rows with userid value which is equal to NULL will be affected. If you want to use WHERE against NULL column, read how to working with NULL values.
If the variable is empty, or without a value, or "", and on your table there is no record where userid is empty or null, then no record will be updated as the where-condition would favor no record, however if there is any record with empty userid, then the record will be updated with the values you provided to the respective columns.
This is actually a form to update the team members who work for a specific client, When i deselect a member then it's status turns to 0.
I have a table with all unique records. table consists of four columns -
first column is `id` which is unique and auto_incremented.
second column is `client_id`.
third column is `member_id`. (these second and third columns together make the primary key.)
fourth column is `current` which shows the status (default is 1.).
Now i have a form which sends the values of client_id and member_id. But this forms also contains the values that are already in the table BUT NOT ALL.
I need a query which
(i) `INSERT` the values that are not already in the table,
(ii) `UPDATE` the `current` column to value `0` which are in the table but not in the form values.
here is a screenshot of my form.
If (select count(*) from yourtable where client_id = and member_id = ) > 0 THEN
update yourtable set current = 0;
ELSE
insert into yourtable (client_id,member_id,current) values (value1,value2,value3)
First of all check if the value exists in the table or not, by using a SELECT query.
Then check if the result haven't save value so it will be inserted, else show an error .
This would be a great time to create a database stored procedure that flows something like...
select user
if exists update row
else insert new row
stored procedures don't improve transaction times, but they are a great addition to any piece of software.
If this doesn't solve your problem then a database trigger might help out.
Doing a little research on this matter might open up some great ideas!
Add below logic in your SP
If (select count(*) from yourtable where client_id = <value> and member_id = <value>) > 0 THEN
update yourtable set current = 0;
ELSE
insert into yourtable (client_id,member_id,current) values (value1,value2,value3)
if you want simple solution then follow this:
*) use select with each entry in selected team.
if select returns a row
then use update sql
else
use insert sql.
In your case member_id & client_id together makes the primary key.
So , you can use sql ON DUPLICATE KEY UPDATE Syntax.
Example:
$sql="INSERT INTO table_name SET
client_id='".$clientId."',
member_id='".$member_id."',
current='".$current."'
ON DUPLICATE KEY
UPDATE
current = '".$current."'
";
In this case when member_id & client_id combination repeats , it will automatically executes update query for that particular row.