How to Insert new value into table using where - php

I have one table name "Grade" with this structure:
ID Student_ID First_Name Last_Name Grade
1 1 John Smith 60
2 2 Garry Poul 70
3 1 John Smith 80
And I want to add a new grade for Student_ID = 1 in the table Grade, I am using PHP with MySQL DB.
I used this but gives me error!
$sql = "INSERT INTO Grade (Grade) VALUES ('85') WHERE Student_ID=1 ";
During search I found that I can't use WHERE with INSERT in MySQL, how can solve it?
Thanks for all

For updating existing records use UPDATE and not INSERT
UPDATE Grade
SET Grade = 85
WHERE Student_ID = 1

Insert:
If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query
Update:
when updating records in a table! Notice the WHERE clause in the UPDATE statement. The WHERE clause specifies which record(s) that should be updated. If you omit the WHERE clause, all records in the table will be updated!
UPDATE Grade
SET Grade = 85
WHERE Student_ID=1 ;

Related

Can I use information in 2 different columns to specify where to update information in UPDATE query?

Consider this table:
Email | Message | Votes
------+---------+-------
Email1| Msg1 | 2
------+---------+-------
Email2| Msg2 | 1
------+---------+-------
Email3| Msg2 | 3
Now, I want to update the 3rd cell under Votes column using following query:
UPDATE tablename SET Votes=4 WHERE Message=Msg2
But the problem is that it will update the both cells under Votes, where Msg2 is present. But I want to update the cell where Email=Email3 and Message=Msg2.
Can it be done using UPDATE query?
Use multiple condtitions and join them with AND:
UPDATE tablename
SET Votes = 4
WHERE Message = 'Msg2'
AND Email = 'Email3';
or using row-constructor:
UPDATE tablename
SET Votes = 4
WHERE (Message, Email) = ('Msg2','Email3');
SqlFiddleDemo
EDIT:
I tried but it didn't work. It didn't update anything
So you have different data than you provided. Probably whitespaces. Try:
UPDATE tablename
SET Votes = 4
WHERE TRIM(Message) = 'Msg2'
AND TRIM(Email) = 'Email3';

Sql – Insert value in first empty cell of column and show ID

I have a SQL statement that will insert a value into the first empty cell. If I run the PHP script again then it inserts into the next null cell etc.
Problem: I also want to find out the ID of that row, and the value of another column in that row. In the MySQL table below, I want a value inserted in the first ‘null’ of COLUMN A, and also know the ID and value in COLUMN B corresponding to that (i.e. ID= 3 and COLUMN B= 11).
My_TABLE
ID COLUMN A COLUMN B
1 6 78
2 7 90
3 NULL 11
4 NULL 5
5 NULL 123
The following sql statement in PHP script will make it possible to insert value to the first empty cell in COLUMN A:
UPDATE My_TABLE
SET COLUMN A = 83
WHERE COLUMN A IS NULL
LIMIT 1;
Result will be:
ID COLUMN A COLUMN B
1 6 78
2 7 90
3 83 11
4 NULL 5
5 NULL 123
I also want to have an SQL script that will print within PHP (echo) the corresponding values of the ID and COLUMN B of the last updated COLUMN A null value (i.e. ID= 3; COLUMN B= 11).
How do I do that?
Here you go
<?php
// just in case, for comparison
$q = 'SELECT `id`,`colB` FROM My_TABLE WHERE `colA` IS NULL ORDER_BY `id` LIMIT 1';
$row = $pdo->query($q)->fetch(PDO::FETCH_ASSOC);
if ($row) {
// voialà, the value for COLUMN B is now known
list($id, $colB) = $row;
$q = 'UPDATE My_TABLE SET `colA`=? WHERE `id`=?';
$stmt = $pdo->prepare($q);
// voilà, your row is updated
$stmt->execute(array($valueForColumnA,$id));
}
Beware, do not copy and paste whis nilly-willy. Read it, look at it, smell it and understand it. Any questions?

MYSQL returning duplicate rows

I have a Database table in MYSQL, it looks like this:
Project_ID user_ID name
11 1 fred
11 2 rick
11 1 fred
I want to get the names in the table, but when I display it I get the same name twice because the user_ID 1 appears twice in the table. I tried to display it with GROUP BY and SUM, but I don't want to do it this way. How do I get it not to return duplicate rows?
Use DISTINCT
SELECT DISTINCT user_ID
, verschil_ma
FROM project_uren
WHERE project_ID = 11
GROUP BY user_ID
Point 1 is that should the user be assigned to the project twice?
Point 2 - Use the DISTINCT keyword to return only unique records - http://dev.mysql.com/doc/refman/5.0/en/distinct-optimization.html
SELECT DISTINCT user_ID
FROM TABLE
WHERE Project_id = 11
That will return you 1 and 2 (You won't get 1 twice)
Thanks
$results = // query
$results = array_unique($results);

Mysql update a row with another row value in same table

I have a table. I want to update the 5th row with 10th row values from the same table. For example:
SlNo Name Quali Exp
1 x B.E 2
2 y BSC 3
3 Z B.A 1.5
4 A MSC 2
5 B MBA 5
Here i want to update second row with the value of 5th row.
Here is my current query:
UPDATE table
SET Name=(select Name from table where slNo='5'),
Quali=(select Quali from table where slNo='5'),
Exp=(select Exp from table where slNo='5')
where slNo='3';
this is working fine ... but if there are more than 20 columns it becomes laborious to write a query this way, because for each column I have to include another sub-query... is there any other way to write query to update the whole row with all values from the other row in the same table?
Use a self-join with the multiple table UPDATE syntax:
UPDATE `table` AS t1 JOIN `table` AS t2 ON t2.slNo = 5
SET t1.Name = t2.Name, t1.Quali = t2.Quali, t1.Exp = t2.Exp
WHERE t1.slNo = 3

PHP Mysql trying to pull multiple rows from different tables (works for 3 rows on 2 tables but not 4 rows from 3 tables)

I'm trying to pull multiple rows from different tables in a database.
If I only have the first 3 rows from two tables, it works fine. As soon as I add the third table and try and pull another row it breaks!
Ultimately what I'm trying to do is pull the image, the dayid, the name of a person, and then name of a cause.
The imageURL and the Outfitday_id are in the same table, the name of the person is in the table Pilot, and the name of the cause is in the table cause.
For some reason it will pull the first two rows (imageUrl, outfitDay_id) from the table Outfitimage, then pull the 3rd row (name) from Pilot, but fails if I add the name and try and pull it from the Cause table.
$link = mysql_connect("host","user","pass");
if ($link) {
mysql_selectdb("up",$link);
// Select records from the DB
$query = "SELECT imageUrl,outfitDay_id,name,name FROM OutfitImage,Pilot,Cause ORDER BY Rand(" . date("Ymd") . ") LIMIT 1";
$image = mysql_query($query);
// Display records from the table
echo "";
while ($row = mysql_fetch_array($image, MYSQL_NUM)) {
echo "<IMAGE SOURCE='$row[0]'/><br>";
echo "<div id='info'>Day $row[1] of ";
echo "$row[2] Uniform Project for$row[3] </div>";
}
echo "";
} else {
echo "Can't connect to the database!";
}
It probably happens because the name column is ambiguous. I'm guessing you have a name column in both the Cause and Pilot (or some other table). MySQL can't guess what table you want to retrieve the column value from, and will spit an error (Column name in field list is ambiguous).
Make sure you prefix the column name with the table name to remove ambiguity:
SELECT
imageUrl, outfitDay_id, Cause.name, Pilot.name
FROM
OutfitImage, Pilot, Cause
You are attempting a cross-join across all three tables, but you aren't creating any conditions to filter them out. The clause FROM table1, table2, table3 attempts a cross-join across all three tables. A cross-join means every row from one table is paired up with every row from another table. For example, say I have three tables, each have two rows. The first table I call names with the two rows jack and jill, the second table I will call ages with rows 21 and 24, the third table will be genders, and have rows male and female. If I do a clause FROM names, ages, genders, it will return a table with 2 X 2 X 2 = 8 rows.
names ages genders
jack 21 male
jack 21 female
jack 24 male
jack 24 female
jill 21 male
jill 21 female
jill 24 male
jill 24 female
Normally, when you cross-join, you also add a condition to WHERE to filter out rows that don't make sense. For example, from example FROM clause you might have a WHERE genders.id = names.genders_id. In this case, you are linking a name with a specific gender row from the genders table. This will probably also solve your need for a LIMIT 1.

Categories