Move number down if number is missing mysql - php

I've got a database with question numbers between 1 and 21 all with answers / values etc.
But I want to be able to delete for example question 7.
And if I delete that question that is between other numbers that the question numbers will automatically change from 1-21 to 1-20 instead of 1-6, 8-21.
Is this possible? I tried searching on the web but I couldn't find what I was looking for.
With kind regards,
How do I make this shorter? I know it can be done with innerjoin but I dont know how to correctly use it because the explanations are unclear to me..
`$sql = "DELETE FROM insertquestion
WHERE nummer='".$questionNumber."'";
$deleteFromQuestion = $db->prepare($sql);
$deleteFromQuestion->execute();
$updateSql = "UPDATE insertquestion SET nummer = nummer - 1 WHERE nummer >= '".$questionNumber."'";
$updateSql = $db->prepare($updateSql);
$updateSql->execute();`
and that another time but then with another table (vraag)

You can use Triggers to do this operation ref:
http://www.mysqltutorial.org/mysql-triggers.aspx
or use a update query to set the values (however this method is costly, if tthe number of rows is great in number)
Update table set q_no=q_no-1 where q_no> 5 //(if you delete a question with number 5)

There may be many ways for this requirement. One of them is you can achieve it by trigger. Write an after delete trigger which will update all question numbers of the table.
CREATE TRIGGER tblquestion_delete AFTER DELETE on tblquestion
FOR EACH ROW
BEGIN
UPDATE tblquestion
SET qno = qno - 1
WHERE qno > old.qno;
END
Another way is simply you can write this query immediately after delete query.
$sql = "DELETE FROM insertquestion WHERE number='".$questionNumber."' ;
UPDATE tblquestion SET number = number - 1 WHERE number > '".$questionNumber."' ; ";

Related

Mysql only update if row has been inserted before

I want to only run the update query if row exists (and was inserted). I tried several different things but this could be a problem with how I am looping this. The insert which works ok and creates the record and the update should take the existing value and add it each time (10 exists + 15 added, 25 exists + 15 added, 40 exists... I tried this in the loop but it ran for every item in a list and was a huge number each time. Also the page is run each time when a link is clicked so user exits and comes back
while($store = $SQL->fetch_array($res_sh))
{
$pm_row = $SQL->query("SELECT * FROM `wishlist` WHERE shopping_id='".$store['id']."'");
$myprice = $store['shprice'];
$sql1 = "insert into posted (uid,price) Select '$uid','$myprice'
FROM posted WHERE NOT EXISTS (select * from `posted` WHERE `uid` = '$namearray[id]') LIMIT 1";
$query = mysqli_query($connection,$sql1);
}
$sql2 = "UPDATE posted SET `price` = price + '$myprice', WHERE shopping_id='".$_GET['id']."'";
$query = mysqli_query($connection,$sql2);
Utilizing mysqli_affected_rows on the insert query, verifying that it managed to insert, you can create a conditional for the update query.
However, if you're running an update immediately after an insert, one is led to believe it could be accomplished in the same go. In this case, with no context, you could just multiply $myprice by 2 before inserting - you may look into if you can avoid doing this.
Additionally, but somewhat more complex, you could utilize SQL Transactions for this, and make sure you are exactly referencing the row you would want to update. If the insert failed, your update would not happen.
Granted, if you referenced the inserted row perfectly for your update then the update will not happen anyway. For example, having a primary, auto-increment key on these rows, use mysqli_insert_id to get the last inserted ID, and updating the row with that ID. But then this methodology can break in a high volume system, or just a random race event, which leads us right back to single queries or transaction utilization.

Deleting MySQL Records Based on a Number

I'm using MySQL 5.5. I need to delete records that are higher then 180 from the table and rather then having to do a loop through all records and delete one at a time I wanted to know is there a way to give MySQL a delete command with any field Slide value of 181 or greater?
For example:
conn.execute "DELETE FROM " & CONN_DATABASENAME & ".tbl_course_progress WHERE slide=>'181' AND aid='2'")) & "';"
Thanks,
Frank
You pretty much have it right there
DELETE FROM table WHERE slide >= 181;
Here's a very trivial way to do it in PHP
$slide_id = 181;
$query = sprintf("DELETE FROM table WHERE slide >= %d", $slide_id);
mysql_query($query);

Mysql UPDATE (from PHP) - only runs first few rows.. Alls CELL locked after one UPDATE

I run a MYSQL UPDATE from PHP. It produces output, which is shown in the browser - No probz.
The mysql db updates various number of rows.. If i try 10 rows, it produces first 5 rows. If i try 4 it produces first and last.
I started making INSERT for all rows, and it inserted 1000+ rows in few seconds for this excact same database. The UPDATE seems to be way off in some way...
Maybe people have some inputs to why this could happend ?
The main concern, is that after i have produced updates on the rows, the rows are "locked" for updates through PHP. This in my mind is really a weird point and i don't get what is going on. I can offcourse make updates through the phpMYadmin.
CODE as requested:
mysql_query(" UPDATE `search` SET `pr_image` = '$primage', `pr_link` = '$pr_link' WHERE `s_id` = '$id' ");
Thanks in advance.
UPDATE `search` SET `pr_image` = $primage, `pr_link` = $pr_link WHERE `s_id` = $id
Try with this query.

Php MySQL query where array value is not found

I have an array of data that generates unique data on the fly in a manor of speaking. It's actually an array with 5 hashes.
What I want to do is a basic select query with a where clause that checks each via OR basically a one line query rather than a query for each array item.
I'm attempting to ensure that no one hash that enters the db is the same as another which I know the probability is virtually null to that actually happening but it's a possibility none the less, safer than sorry is my perspective on the matter
Anyway the query I'm thinking of makes no sense as if a match is found the query will result in such what I wanna do is from the original array find the one that's not found and use it where if all 5 aren't found I'll just randomly pick one I guess in the end I want to form a result that is 1 to 5 in a new array so I can randomly pick from that result
Is this possible or would it just be easie to cycle over each one with a songle query?
"SELECT
CASE hashes.hash
WHEN $hashes[0] THEN 0
WHEN $hashes[1] THEN 1
WHEN $hashes[2] THEN 2
WHEN $hashes[3] THEN 3
...
END
FROM hashes WHERE hashes.hash IN(".implode($hashes).")"
This should tell you exactly which of the hashes you sent to the server have been found on the server.
The result set would be the index keys (0, 1, 2, 3) of the array that generated the query.
If you sent a query based on an array of 100 hashes and you get a result set of 99 hashes, that means at least one hash was not found in the db.
You could cycle through the result set like this:
while($row = $pdo->fetch()) {
$index = $row[0] // first column of the result set
unset($hashes[$index]);
}
When while finishes the only hashes left in the array should be the ones that weren't found in the database.
My opinion is that it would be easier to to cycle over each one with a single query. From what you say there appears to be no major benefit in doing it all at once.
In that case I would suggest:
alter table myTable create id_bkp int;
update myTable set id_bkp=account_id;
update myTable set account_id=56 where id_bkp=100;
update myTable set account_id=54 where id_bkp=56;
alter table myTable drop id_bkp;
Of course that will depend on what DB system you are using.
Do you mean something like this?
$sql = "SELECT * FROM `table` WHERE `field` = ";
$where_string = "'" . implode("' OR `field` = '",$my_array) . "'";
$sql .= $where_string;
You could use:
$my_array = array_unique($my_array);
To remove duplicate values.

adding to numerical value of column

Hi so I was wondering what the best way to add or subtract from a field in my table would be. The way I know it would work is if I query the value do the addition and then UPDATE the value.
I would rather include the addition as part of the update query like:
UPDATE users SET points = +10
Or something like that. Is it possible?
You just need to name the column on the right hand side of the = operator:
UPDATE `users` SET `points` = `points`+10
Since there is no WHERE clause this will give all users 10 more points then they currently have.
You can just do this:
UPDATE users SET points = points + 10

Categories