简体   繁体   中英

How to use UPDATE query to update another table

In the Exam1 table, ID, examname, and exam_id values are added fine. But when I try to use the UPDATE query to update the points to another table (Question), it does not work.

        for($i = 1; $i<$arraysize; $i++){ //For every question

            $questionid = $array[$i]['questionid'];
            $points     = $array[$i]['points'];
            $ID = $ID+1;

            $queue ="INSERT INTO Exam1 (ID, examname, exam_id) VALUES
            ('$questionid','$examname', '$exam_id')";
            $result = mysqli_query($connection,$queue);


            $queue1 ="INSERT INTO points (ID, points) VALUES
            ( '$ID' , '$points')";
            $result1 = mysqli_query($connection,$queue1);



         $sql = "UPDATE Question SET points='$points' where ID ='$questionid'";

           $result1 = mysqli_query($connection,$sql);

        }

在此处输入图片说明

 $myquery = "UPDATE Question SET points='$points' WHERE ID ='" . $questionid . "'";

   $resultset = mysqli_query($connection, $myquery);

Try this please

First of all be careful with int types and putting values for it in UPDATE s and INSERT s into a quote: There is type cast happening, and it may come to results, which you may not expect.

Second, I still have big trouble to understand your data model of your database: You are joining the Exam1.ID over with the Question.ID . Both appear to be the surrogates for the entity to me. Otherwise I would not understand what the meaning of the column Exam_ID (most likely coming from table Question in your case) should be. It therefore looks to me that the WHERE clause of your UPDATE statement is incomplete (did you also mean to restrict on EXAM_ID ? Otherwise you might be setting all points to 20 for all questions irrespectively of the exam...?)

If I got you wrong, please provide a detailed overview about your database schema setup including the primary keys of at least the following tables:

  • Exam1
  • points
  • Question

to allow us further help you (and me adjusting the answer here). Also giving us some insight how you think the foreign key relationship is between the tables might help us to help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM