2012-03-09 10 views
0

私は自分のオフィスで誰が自分のオフィスで紅茶を作るのかを判断するのに役立つWebアプリケーションを作っています。 。初心者の無知に対する謝罪。MySQLのテーブルに埋め込まれたPHPドロップダウンを使用して別のテーブルに選択を挿入する

新規ユーザー登録ユーザーテーブルにドロップダウンから選択したものを入力する必要があります。それ自体は別のテーブルから作成されます。そこで、ユーザーがサインアップすると、ドロップダウン/ドリンクテーブルからお気に入りのドリンクの名前を選択してもらい、そのドリンクのIDをuserテーブルのdefaultdrinkフィールドに保存します。私はこれがGETではなくPOSTを使って行われるべきであることも理解しています。

これまでにDBにデータを入力し、DBからデータを取り込んだフォームを作成しましたが、両方を実行するにはまだ成功していません。

フォームページがある...

<?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(); 
?> 

私は貯蓄を超えていますか?

乾杯、

アレックス

+1

通常の別の部分insert_ac.php PHPのクラップコード...通常のSQLインジェクションヒステリックな警告に続く...とusua私は解決するためにいたずらコード...神、これは退屈です。 –

+0

データベース名は一貫していますが、 "茶"ではなく "テスト"です – Alexhilton

+0

あなたの常識。多くのお詫びは、私はちょうどこれをクラックすることができれば、私は多くのより興味深い問題を持つことができます;) – Alexhilton

答えて

1

ない近いmysqlの接続を行います。
さらに、実際のdb行を配列に格納し、その配列を使用してドロップダウンを設定します。
を入力し、フォームの中に選択ボックスを置きます。まあ

あなたはヨーヨーはまだ簡単な何か役に立つ

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> 

<?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"; 
} 
+0

ありがとう、http:// stackoverflowを使用して、配列をドロップダウンリストに取り出しました。com/questions/7773264/populate-dropdown-is-not-working – Alexhilton

+0

これは素晴らしいことです。ありがとうございました。 奇妙なことに、予期せぬ「<」がここにありますが、私はそれを見つけることができません。 ありがとうございました – Alexhilton

+0

スケッチから正しく動作しているとは言わないでください:) –

関連する問題