PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 24, 2022

[FIXED] Change id from selected option

 January 24, 2022     mysql, php, phpmyadmin     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing