简体   繁体   English

PHP单个mySQL表加上通过单个HTML表单的未定义数量的多个mySQL表条目

[英]PHP single mySQL table plus undefined number of multiple mySQL table entries through a single HTML form

this is my first question posted on the internet ever, I was usually able to find some sort of a solution, but now I'll either have to do this some other more basic way or, as I hope get help from someone on Stack Overflow. 这是我有史以来第一个发布在互联网上的问题,通常我可以找到某种解决方案,但是现在我要么不得不以其他一些更基本的方式来做,要么希望从Stack Overflow上获得帮助。 。 I have an idea how to do it another way but I would really like if I can make it work like this. 我有一个其他方法的想法,但是我真的很想让它像这样工作。

I am trying to build an internet application through which people or organizations would be able to inform the shelter about the clothes they would like to donate to be picked up by shelter's mobile team. 我正在尝试构建一个Internet应用程序,通过该应用程序,人们或组织可以将他们想要捐赠的衣物通知庇护所,以供庇护所的移动团队提取。

I'm now putting a simplified version of my HTML form, PHP script, JavaScript function, and mySQL used to make the tables in which I want to store the info from the HTML form. 现在,我将HTML表单,PHP脚本,JavaScript函数和mySQL的简化版本用于创建要在其中存储HTML表单信息的表。

HTML form: 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>

This is simplified version of JavaScript which adds form fields into 这是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 />";
}

These are mySQL expression used to create tables in the shelter_db databse: 这些是用于在housing_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)
);

My idea is for the data from these two tables to be connected by the timestamp when the records were made, since there most certainly wouldn't be simultaneous entries by different parties. 我的想法是将这两个表中的数据按记录创建时的时间戳进行连接,因为最肯定的是不会由不同方同时进行输入。 The person assigned to contact donors could then see what that particular party wishes to donate and can send a mobile team with a car or a pickup truck accordingly. 然后,分配给联系捐赠者的人可以看到该特定方希望捐赠的东西,并可以相应地派出一支机动小汽车或小货车。

What I'm struggling with is how to formulate the php/sql page, and after going through dozens of web pages I couldn't find one solution that would fit my needs. 我苦苦挣扎的是如何编写php / sql页面,在浏览了数十个网页后,我找不到适合我需求的解决方案。 I'm assuming that I should use implode function but after trying various solutions I wasn't able to find the right one. 我以为我应该使用内爆函数,但是在尝试了各种解决方案之后,我找不到合适的解决方案。 Here is the code: 这是代码:

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

I would be grateful to anyone who could offer me the code that would fit my example. 我将感谢任何能为我提供适合我的示例的代码的人。 Sorry for the longer post but they said to be thorough. 很抱歉,发布的时间较长,但是他们说的很周到。 Thanks in advance. 提前致谢。

First of all, correlating by timestamp is definitely not a good idea. 首先,通过时间戳进行关联绝对不是一个好主意。 First of all, it's possible for two people to use your web site at exactly the same time, and their submissions would get confused. 首先,两个人可能会完全同时使用您的网站,他们的提交会变得混乱。 Second, the time could change between inserting into the application table and the clothes table (datetimes are precise to the second, and maybe the first insert was at time T.999 and the second at T+1.000; this second problem can be resolved by assigning a variable to the time at the beginning of the script, and using that in all your inserts. But because of the first problem, you should still not use timestamps for this. 其次,插入到应用程序表和衣服表之间的时间可能会发生变化(日期时间精确到第二次,也许第一次插入是在T.999,第二次插入是在T + 1.000;此第二个问题可以通过解决在脚本开始时为时间分配一个变量,并在所有插入操作中使用它,但是由于第一个问题,您仍然不应该为此使用时间戳。

Instead, you should assign a unique ID to the application. 相反,您应该为应用程序分配一个唯一的ID。 This is easily done in mysql by having an ID column with the auto-increment option. 在mysql中,通过使用带有auto-increment选项的ID列可以轻松完成此操作。 When you add a row to the table, you leave this column out (or give its value as NULL), and the server will automatically assign a value that's 1 higher than the last value assigned. 当向表中添加一行时,将该列保留为空白(或将其值设置为NULL),服务器将自动分配一个比上次分配的值高1的值。 The function mysql_insert_id() will return the value that was assigned in the last insertion you did. 函数mysql_insert_id()将返回您在上一次插入中分配的值。 In the clothes table, you add an application_id column, which contains that ID. 在衣服表中,添加一个application_id列,其中包含该ID。 You use this to correlate the two tables. 您可以使用它来关联两个表。

The next thing you have to deal with is how to handle multiple clothes column in the form. 接下来需要处理的是如何处理表单中的多个衣服栏。 In each row that you added, you gave the form elements the same names as in the previous row. 在添加的每一行中,为表单元素赋予与上一行相同的名称。 When PHP is processing the form, $_POST['kind'] can only get one of them. 当PHP处理表单时, $_POST['kind']只能获取其中之一。 The way this is handled is by giving them named ending in [] , eg <select name="kind[]"> . 处理方法是给它们命名以[]结尾的<select name="kind[]"> ,例如<select name="kind[]"> This tells PHP that it should create an array of these inputs. 这告诉PHP应该创建这些输入的数组。 You can then get the kind of the first row as $_GET['kind'][0] , the second row is $_GET['kind'][1] , and so on. 然后,您可以得到第一行的类型为$_GET['kind'][0] ,第二行为$_GET['kind'][1] ,依此类推。 So when you're writing to the database, you can do: 因此,当您写入数据库时​​,可以执行以下操作:

foreach ($_GET['kind'] as $i => $kind) {
  $sql = "INSERT INTO clothes (Application_ID, Kind, Pieces, Size) VALUES ($id, ".$kind.", "$_GET['piec'][$i]", '"$_GET['size'][$i]"')";
  mysql_query($sql, $con) or die ('Error: ' . mysql_error());
}

$id contains the value you got from mysql_insert_id() . $id包含您从mysql_insert_id()获得的值。

Finally, I wouldn't be doing my duty as a SO answerer if I didn't point out that the mysql_XXX functions are deprecated and should not be used in any new code. 最后,如果我没有指出mysql_XXX函数已被弃用,并且不应该在任何新代码中使用,我就不会以SO mysql_XXX职责。 They have been superceded by mysqli_XXX and PDO. 它们已被mysqli_XXX和PDO取代。 These allow you to create "prepared" statements, so you don't have to substitute variables into strings as above, which potentially allows SQL-injection attacks unless you're careful to sanitize your data (I didn't do so above for expediency). 这些允许您创建“ prepared”语句,因此您不必如上所述将变量替换为字符串,这可能会导致SQL注入攻击,除非您谨慎清理数据(出于方便起见,我没有这样做) )。

BTW, I haven't looked at your JavaScript. 顺便说一句,我还没有看过您的JavaScript。 It doesn't look like there's much to it. 看起来没有太多的东西。 If someone else notices problems in it, please pipe up. 如果其他人发现其中的问题,请进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM