1

Sorry if the title is strangely worded. This is my first time asking anything here, and I'm pretty new to PHP so I'm having a tough time figuring this out.

Basically, I am trying to write a PHP script that will allow me to change the rules for a certain game instance from our website, so I don't have to go into the database and run a SQL query every time.

This is what I have so far:

public function update_rules() 
    {
        $rules = $this->input->post('rules');
        $domains = $this->input->post('domains_multiselect');

        $qarr = array();
        $sql = "
                    UPDATE domains
                    SET rules = ?
                    WHERE domain_id = ?
                  ";

        $qarr[] = $rules;
        $qarr[] = $domain_id;

        $query = $this->db->query($sql,$qarr);

        redirect ("admin/insert_rules");

    }

I am unsure how to to a substr_replace() to change the "?" placeholders in the query to be able to input both the manually typed rules and the domain_id, which I think will be generated when the domain name is selected.

I could be completely off-base here, but if anyone could point me in some kind of direction, that'd be great. thank you.

1
  • It looks like you're working with a PDO prepared statement. You shouldn't do a string replace, but rather a prepare() execute() sequence. Documentation here: php.net/manual/en/pdostatement.execute.php Commented May 13, 2016 at 17:46

1 Answer 1

1

you can use either named placeholder or position dependent placeholders...

//Position dependent: 
$stmt = $this->db->prepare("UPDATE domains SET rules = ? WHERE domain_id = ?");
$stmt->bindParam(1, $rules);
$stmt->bindParam(2, $domain_id);
$stmt->execute();

//Named Placeholder: 
$stmt = $this->db->prepare("UPDATE domains SET rules = :rules WHERE domain_id = :domain");
$stmt->bindParam(':rules', $rules);
$stmt->bindParam(':domain', $domain_id);
$stmt->execute();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.