/    Sign up×
Community /Pin to ProfileBookmark

Copying the last "id" from one column in a table in MYSQL to another table

Hey Guys!

I try to write a POST-Method where you can upload a question into my moodle database.
So it goes like this:

The student submits a form with a question. Then the new information is being uploaded in the table “mdl_question” which gives every new entry an “id” by AUTO_INCREMENT.

The next step is: They click on a second “form” which isn’t really a form for them, but more of a butten which then uploads certain information into the columns of another table called “mdl_qtype_multichoice_options”.

The pivot point here is, that the table “mdl_qtyp…” has a column which is called “questionid”. Now: every two uploads shall have the same number –[B] mdl_choice:id = mdl_qtype_multichoice_options:questionid[/B]

How do I do that? I have my code here:

<?php
if(isset($_GET[“page”])) {
if($_
GET[“page”] == “2”) {
$sin = 1;
$sa = 1;
$an = “abc”;

$verbindung = mysql_connect(“——“, “——“, “——-“)
or die(“Entschuldigen sie, es entstand ein Fehler im System.”);

mysql_select_db(“moodle”)
or die (“Verbindung zur Datenbank war nicht m&ouml;glich.”);

$id = “SELECT id FROM mdl_question ORDER BY id DESC LIMIT 1”;

$eintrag = “INSERT INTO mdl_qtype_multichoice_options
(single, shuffleanswers, answernumbering, questionid)

VALUES
(‘$sin’, ‘$sa’, ‘$an’, ‘$id’)”;

$eintragen = mysql_query($eintrag);

if ($eintragen == true) {
echo “Vielen Dank f&uuml;r ihr Vertrauen in Get2Med. Wir melden uns in K&uuml;rze bei ihnen! <a href=”index.html”>Zur&uuml;ck</a>”;
}
else {
echo “Fehler im System, bitte versuchen sie es sp&auml;ter noch einmal.”;
}

mysql_close($verbindung);
}
}
?>

I’m grateful for any help! :-)))

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@denizmdauthorNov 14.2016 — So I want to implement the newly created "id" from "mdl_question" into "questionid" in "mdl_qtype_multichoice_options"
Copy linkTweet thisAlerts:
@ginerjmNov 14.2016 — #1 - please stop using the MySQL_* functions. If you look at the manual you will see that these are deprecated and not available in the lateset version of PHP. Switch now before your application breaks.

#2 - Have you read up on this mysqlI function? http://php.net/manual/en/mysqli-stmt.insert-id.php OR - if you choose to switch to PDO instead of mysqlI - this one: http://php.net/manual/en/pdo.lastinsertid.php
Copy linkTweet thisAlerts:
@NogDogNov 14.2016 — This does not get the ID from the DB, it just assigns the SQL string to $id:
[code=php]
$id = "SELECT id FROM mdl_question ORDER BY id DESC LIMIT 1";
[/code]

You'll need to run that through the query function, then fetch the result (if there is one), and use that value in the insert query.
Copy linkTweet thisAlerts:
@denizmdauthorNov 15.2016 — Thanks guys!
×

Success!

Help @denizmd spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...