Issue
I have searched for similar questions but every question that was related wasn't the solution for me.
I am using php, sql and phpmyadmin for this.
I'm trying to add characters to a world. The idea is that you can add characters to a world by selecting them in option field(for now) and change the world_id of that character.
Now i have the id but i don't know how to actually change it in the database.
I will show my database
table characters:
id
name
last_name
image
age
world_id (this table is linked with the table worlds)
table worlds:
world_id (so the world_id's are linked
name
description
my php code:
$pdo = Database::connect();
$sql = 'SELECT * FROM characters ORDER BY id DESC';
echo '<select name="option">';
foreach ($pdo->query($sql) as $row) {
echo '<option value="' . $row['id'] . '">' . $row['name'] . '</option>';
}
echo '</select>';
$selected_val = $_POST['option'];
?>
Hopefully can someone help me.
Solution
You need to run an update query in the database-
$sql = "UPDATE `characters` SET `world_id` = :world_id WHERE `id` = :id";
$world_id = 1; //any world id you want to set
$statement = $pdo->prepare($sql);
$statement->bindValue(":world_id", $selected_val);
$statement->bindValue(":id", $world_id);
$count = $statement->execute();
Answered By - xRahul
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.