我有一个基本表单,我可以在mysql db中插入值。我在显示先前从表单中插入的值时遇到问题。 mysql数据库中有三个表共享academy_id
作为键。我的SELECT
查询出现了php错误。从多个共享外键的表中正确显示值的最佳方法是什么? SITE
if(isset($_POST['submit'])) {
$db_insert = $db_con->prepare("INSERT INTO academy (name, type, status, academy_id, street_address, zipcode, city, state, comments) VALUES (?,?,?,?,?,?,?,?,?)");
$db_insert->bind_param('sssisssss', $_POST['name'], $_POST['type'], $_POST['status'], $_POST['acad_id'], $_POST['street'], $_POST['zipcode'], $_POST['city'], $_POST['state'], $_POST['acad_comm']);
$db_insert->execute();
$academy_id = $_POST['acad_id'];
//Query to Read Values from database based on academy_id
$db_select = $db_con->prepare("
SELECT a.name AS 'Academy Name:',
a.academy_id AS 'Academy ID:',
a.status AS 'STATUS:',
a.type AS 'Type:',
a.street_address AS 'Street:',
a.city AS 'City:',
a.state AS 'State:',
a.zipcode AS 'Zip Code:',
a.comments AS 'Comments:',
c.course_name AS 'Courses Name:',
ac.start_date AS 'Course Start Date:',
FROM academy a
WHERE academy_id = $academy_id
INNER JOIN courses_by_academy ac ON a.id = ac.academy_id
INNER JOIN courses_selection_list c ON c.id = ac.course_id
");
$db_select->bind_param("i", $academy_id);
$db_select->execute();
}
?>
<form action="test9.php" method="POST">
Name: <input type="text" name="name"></br>
Academy Status:
<select>
<option value="ACTIVE">ACTIVE</option>
<option value="INACTIVE">INACTIVE</option>
</select>
Type:
<select>
<option value="Upper-Secondary">Upper-Secondary</option>
<option value="Post-Secondary">POST SECONDARY</option>
</select>
Courses being offered?
<select name="courses_offered">
<option value="1">1</option>
<option value="2">2</option>
</select>
<div id="course_catalog"></div>
Academy ID: <input type="text" id="acad_id" name="acad_id"></br>
Address: <input type="text" id="street" name="street"></br>
Zip Code: <input type="text" id="zip_input" name="zipcode"></br>
City: <input type="text" id="city" name="city" value=""></br>
State: <input type="text" id="state" name="state" value=""></br>
Overall Notes/Comments:</br><textarea id="acad_comm" name="acad_comm" rows="4" cols="50"></textarea></br>
<input value="SAVE" name="submit" type="submit">
</form>
答案 0 :(得分:1)
我认为你需要在WHERE子句之前构建连接。 您似乎可能会收到一个模糊的字段错误,因为您在WHERE子句中提到“academy_id”,但您没有指定要检查的表。我相信当你以这种方式尝试时,你会得到一个MySQL引擎错误。
//Query to Read Values from database based on academy_id
$db_select = $db_con->prepare("
SELECT a.name AS 'Academy Name:',
a.academy_id AS 'Academy ID:',
a.status AS 'STATUS:',
a.type AS 'Type:',
a.street_address AS 'Street:',
a.city AS 'City:',
a.state AS 'State:',
a.zipcode AS 'Zip Code:',
a.comments AS 'Comments:',
c.course_name AS 'Courses Name:',
ac.start_date AS 'Course Start Date:'
FROM academy a
INNER JOIN courses_by_academy ac ON a.id = ac.academy_id
INNER JOIN courses_selection_list c ON c.id = ac.course_id
WHERE a.academy_id = $academy_id
");
此外,此处未发布错误,因此很难猜出您的问题是什么。
答案 1 :(得分:1)
当我发现可疑的媒体播放器更新框时,我关闭了你的网站...所以我不能肯定地说,但是我可以看到你没有做任何事情你得到的结果DB。你提到的PHP错误必须是不同的。
你需要像
这样的东西foreach ($db_select->fetchAll() as $row)
{
echo $row['name'].", ".$row['academy_id']; //do whatever with code
}
此外,您应始终将SQL语句放在try catch块中,
try{
$db_select = $db_con->prepare("
...
}catch(PDOException $e) {
echo $e->getMessage();
}