News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

How do I convert INSERT to UPDATE in this way?

Started by Jade Elizabeth, October 08, 2014, 02:26:07 PM

Previous topic - Next topic

Jade Elizabeth

I have no idea what I am doing. Here's the original query:

function add_university($university_id, $university_name, $teaching_score, $int_outlook_score, $research_score, $ind_income_score, $citations_score) {
    global $db;
    $query = "INSERT INTO university
                 (university_id, university_name, teaching_score, int_outlook_score, research_score, ind_income_score, citations_score)
              VALUES
                 ('$university_id', '$university_name', '$teaching_score', '$int_outlook_score', '$research_score', '$ind_income_score', '$citations_score')";
    $db->exec($query);
}


I want to emulate that, and get it to update all of those values for a single university_id only....this is my best attempt. I've never done an update query before. They scare me. Stack overflow has a bunch of methods but they'd be HUGE if I tried them with this query...

This is my best attempt ha ha

function modify_university($university_id, $university_name, $teaching_score, $int_outlook_score, $research_score, $ind_income_score, $citations_score) {
    global $db;
    $query = "UPDATE university
              SET (university_id, university_name, teaching_score, int_outlook_score, research_score, ind_income_score, citations_score)
              VALUES
                 ('$university_id', '$university_name', '$teaching_score', '$int_outlook_score', '$research_score', '$ind_income_score', '$citations_score')
  WHERE university_id = '$university_id'";
    $db->exec($query);
}
Once proud Documentation Writer and Help Squad Leader | Check out my new adult coloring career: Color With Jade/Patreon.

Jade Elizabeth

So Oldies said there's no bulk/compact way to do it and I have to do it in this very annoying long way that I didn't want to....so using this has worked :D

function modify_university($university_id, $university_name, $teaching_score, $int_outlook_score, $research_score, $ind_income_score, $citations_score) {
    global $db;
    $query = "UPDATE university
              SET university_id = '$university_id', university_name = '$university_name', teaching_score = '$teaching_score', int_outlook_score = '$int_outlook_score', research_score = '$research_score', ind_income_score = '$ind_income_score', citations_score = '$citations_score'
              WHERE university_id = '$university_id'";
    $db->exec($query);
}


If anyone knows another way please show me, I'd love to see it :D
Once proud Documentation Writer and Help Squad Leader | Check out my new adult coloring career: Color With Jade/Patreon.

Advertisement: