这是我在互联网上发布的第一个问题,我通常能够找到某种解决方案,但现在我要么必须以其他更基本的方式来解决这个问题,要么我希望能从 Stack Overflow 上的某个人那里得到帮助. 我有一个想法如何以另一种方式做到这一点,但我真的很想如果我能让它像这样工作。
我正在尝试构建一个互联网应用程序,人们或组织可以通过该应用程序通知收容所他们想捐赠的衣服,以供收容所的移动团队领取。
我现在将我的 HTML 表单、PHP 脚本、JavaScript 函数和 mySQL 的简化版本用于制作我想要存储来自 HTML 表单的信息的表。
HTML 表单:
<form action="apply.php" method="post">
<p>Please enter your personal info so we can make contact with you:<br />
Name: <input type="text" name="name" /><br />
Address: <input type="text" name="addr" /><br />
Phone: <input type="text" name="phon" /><br />
e-mail: <input type="text" name="emai" /><br /><br />
<!--previous part of the form needs to be entered only once in a table named
"application" in real form there are more fields-->
<div id="dod">
<p><b>I wish to donate:</b> (in case you wish to donate more stuff click on
"Add more"):</p>
<p> <select name="kind">
<option value="Jacket">Jacket</option>
<option value="Shirt">Shirt</option>
<option value="Pants">Pants</option>
</select></p>
<!--there are more options in the actual form-->
<p>How many items of this type would you like to donate?
<input type="text" name="piec"></p>
<p>Size of the items: <select name="size">
<option value="S">S</option>
<option value="M">M</option>
<option value="L">L</option>
<option value="Other">Other</option>
<option value="Various">Various</option>
</select></p><br />
<!--This part should be entered several times depending on how many types of clothes
and in which sizes people wish to donate. There are more options in actual form-->
</div>
<center>
<a href="#" onclick="JavaScript:addClothes()" style="font-family: 'verdana'">Add more
</a><br /><br />
<input type="submit" value="Submit"></input>
</center>
</form>
这是 JavaScript 的简化版本,它将表单字段添加到
function addClothes() {
document.getElementById('dod').innerHTML+="<p><select name='kind'><option\n\
value='Jacket'>Jacket</option>\n\
<option value='Shirt'>Shirt</option>\n\
<option value='Pants'>Pants</option>\n\
</select>\n\
</p><p>How many items of this type would you like to donate? <input type='text'\n\
name='piec'></p>
<p>Size: <select name='size'><option value='S'>S</option>\n\
<option value='M'>M</option>\n\
<option value='L'>L</option>\n\
<option value='Other'>Other</option>\n\
<option value='Various'>Various</option></select></p><br />";
}
这些是用于在 shield_db 数据库中创建表的 mySQL 表达式:
CREATE TABLE shelter_db.application
(
Name varchar(80) NOT NULL,
Address varchar(80) NOT NULL,
Phone varchar(20) NOT NULL,
Email varchar(50) NOT NULL,
Stamp timestamp NOT NULL,
PRIMARY KEY (Stamp)
);
CREATE TABLE shelter_db.clothes
(
Kind varchar(40) NOT NULL,
Pieces integer(6) NOT NULL,
Size varchar(20),
Stamp timestamp,
FOREIGN KEY (Stamp)
REFERENCES application (Stamp)
);
我的想法是让这两个表中的数据通过记录时的时间戳连接起来,因为不同方肯定不会同时输入。然后,被分配联系捐赠者的人可以看到该特定方希望捐赠什么,并可以相应地派出带有汽车或皮卡车的移动团队。
我正在苦苦挣扎的是如何制定 php/sql 页面,在浏览了几十个网页之后,我找不到适合我需要的解决方案。我假设我应该使用 implode 函数,但是在尝试了各种解决方案之后,我找不到合适的解决方案。这是代码:
<?php
$con = mysql_connect('localhost','root','');
if (!$con) {die('Unable to connect: ' . mysql_error());}
mysql_select_db('shelter_db', $con);
//the following part needs only one entry into application table of the db
$sql="INSERT INTO application (Name, Address, Phone, Email, Stamp)
VALUES
('$_POST[name]','$_POST[addr]','$_POST[phone]','$_POST[emai]',time());
//the following part needs multiple entries depending on how many times the Add more
//button in the HTML form has been clicked
INSERT INTO clothes (Kind, Pieces, Size, Stamp)
VALUES
('$_POST[kind]','$_POST[piec]','$_POST[size]',time())";
if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }
else {
echo 'Database entry successful.';
}
mysql_close($con)
?>
我将感谢任何可以为我提供适合我示例的代码的人。对不起,较长的帖子,但他们说要彻底。提前致谢。