다른 이름의 테이블에 외래 키가있는 ID를 업데이트하려고합니다. 드롭 메뉴가 있고 드롭 메뉴에 NAME_TEST 테이블의 이름이 있습니다. 나는 이름을 선택해야하지만, 내가 원하는 삽입은 다음과 같습니다이름을 id로 변환하는 방법
INSERT INTO (test) values (the value that i need is the ID of selected name)
코드 :
<html> <h1>Update form</h1></html>
<?php
if (isset($_POST['submit'])) {
$connect = mysqli_connect('localhost', 'root', '', 'test');
$query = "UPDATE test SET location_name='".$_POST['new_location']."' WHERE id='".$_POST['location']."' LIMIT 1";
$res = mysqli_query($connect, $query);
if (!$res) {
die("Something went wrong");
}
}
// This is the code to load the select options
$connect = mysqli_connect('localhost', 'root', '', 'test');
$query = 'SELECT * FROM name_test';
$res = mysqli_query($connect, $query);
echo "Choose setup";
$options = array();
while($row = mysqli_fetch_assoc($res)) {
$options[] = $row;
}
?>
<form method="POST" action="<?= $_SERVER['PHP_SELF'] ?>"><BR><BR>
<select name="location">
<option value="0">--- Select an option ---</option>
<?php foreach ($options as $option): ?>
<option value="<?= $option['id'] ?>"><?= $option['name'] ?></option>
<?php endforeach; ?>
</select><br /><BR><BR>
<B> New name:</B> <BR> <input type="text" name="new_location"><br /><BR><BR>
<input type="submit" name="submit" value="Update" />
</form>
드롭 메뉴에서 선택한 색인이 필요하다는 의미입니까? – Haris
드롭 메뉴에서 선택한 이름 –