使用MySQL表填充的PHP下拉列表将所选内容插入到另一个表中


Using a MySQL table-populated PHP dropdown to insert selection into another table

我正在制作一个网络应用程序,帮助确定轮到谁在办公室泡茶,让我专注于学习PHP/MMySQL的使用。为新手的无知道歉。

对于注册的新用户,我需要用他们从下拉列表中选择的内容填充用户表,下拉列表本身是从一个单独的表中填充的。因此,当用户注册时,我希望他们从下拉列表/饮料表中选择他们最喜欢的饮料的名称,并且我希望该饮料的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();
?>

我救不了吗?

干杯,

Alex

不要关闭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页面模板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";
}