我正在制作一个网络应用程序,以帮助确定轮到我在办公室泡茶,让我专注于学习PHP / MySQL的使用。为新手无知道歉。
对于注册的新用户,我需要使用下拉列表中的选项填充用户表,该下拉列表本身是从单独的表填充的。因此,当用户注册时,我希望他们从下拉菜单/饮料表中选择他们喜欢的饮品的名称,并且我希望将该饮料的ID保存在用户表的defaultdrink字段中。我也明白这应该用POST来完成,而不是GET。
到目前为止,我们成功地制作了一个填充数据库的表单,并从数据库填充了一个下拉列表 - 但是两者都没有成功。
表单页面是......
<?php
require "insert_dropdown.php";
?>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form1" method="post" action="insert_ac.php">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td colspan="3"><strong>Sign up to the Tea App</strong></td>
</tr>
<tr>
<td width="71">Name</td>
<td width="6">:</td>
<td width="301"><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<?php
$dropdown = "<select name='drinkname'>";
while($row = mysql_fetch_assoc($dresult)) {
$dropdown .= "\r\n<option value='{$row['drinkname']}'>{$row['drinkname']}</option>";
}
$dropdown .= "\r\n</select>";
echo $dropdown;
?>
表单操作由insert_ac.php ...
引导<?php
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="tea"; // Database name
$tbl_name="users"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form
$name=$_POST['name'];
$pref=$_POST['pref']; // Drink preference
// Insert data into mysql
$sql="INSERT INTO $tbl_name(name, pref)VALUES('$name', '$pref')";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
}
else {
echo "ERROR";
}
// close connection
mysql_close();
?>
我正在使用insert_dropdown.php ...
填充下拉列表<?php
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Write out our query.
$dquery = "SELECT drinkname FROM drinks";
// Execute it, or return the error message if there's a problem.
$dresult = mysql_query($dquery) or die(mysql_error());
// if successfully insert data into database, displays message "Successful".
if($dresult){
echo "Drink Successful";
echo "<BR />";
}
else {
echo "ERROR";
}
// close connection
mysql_close();
?>
我是否超越拯救?
干杯,
亚历
答案 0 :(得分:1)
不要关闭mysql连接 或者 - 甚至更好 - 将实际的数据库行存储到数组中并使用该数组填充下拉列表 并将您的选择框放在表单内。
好吧,如果你想要学习有用而简单的东西
的config.php
<?php
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="tea"; // Database name
// Connect to server and select database.
mysql_connect($host, $username, $password);
mysql_select_db($db_name);
// A function! greatest invention since wheel.
function dbgetarr($query){
$a = array();
$res = mysql_query($query);
if (!$res) {
trigger_error("dbget: ".mysql_error()." in ".$query);
} else {
while($row = mysql_fetch_assoc($res)) $a[]=$row;
}
return $a;
}
主页。
<?php
include 'config.php';
$data = dbGetArr("SELECT drinkname FROM drinks");
$tpl = 'tea.tpl.php';
include 'main.tpl.php';
主要网站模板main.tpl.php
<html>
<body>
<?php include $tpl ?>
</body>
</html>
茶页模板tea.tpl.php
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form1" method="post" action="insert_ac.php">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td colspan="3"><strong>Sign up to the Tea App</strong></td>
</tr>
<tr>
<td width="71">Name</td>
<td width="6">:</td>
<td width="301"><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td width="71">Name</td>
<td width="6">:</td>
<td width="301"><input name="drink" type="text" id="name">
<select name="drinkname">
<?php foreach($data as $row)): ?>
<option value="<?=$row['drinkname']?>"><?=$row['drinkname']?></option>
<?php endforeach ?>
</select>
</td>
</tr>
<tr>
<td colspan="3" align="center">
<input type="submit" name="Submit" value="Submit">
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
insert_ac.php
<?php
include 'config.php';
$tbl_name="users"; // Table name
// Get values from form and formatting them as SQL strings
$name = mysql_real_escape_string($_POST['name']);
$pref = mysql_real_escape_string($_POST['pref']); // Drink preference
// Insert data into mysql
$sql="INSERT INTO `$tbl_name` (name, pref) VALUES('$name', '$pref')";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
}else {
echo "ERROR";
}