简体   繁体   English

通过html表单在mysql中插入数据(以及更多..)

[英]Insert data in mysql by html form (and more..)

First of all: I'm new into MYSQL/PHP :). 首先:我是MYSQL / PHP的新手:)。 I'm working on a simple script to learn php/mysql, but I'm stuck with several problems. 我正在研究一个简单的脚本来学习php / mysql,但我遇到了几个问题。 I'm really sorry if some of these are already asked on this website, but I DID search them, but they didn't work in combination with my intentions, so please don't just -1 this post! 我真的很抱歉,如果已经在本网站上询问过其中一些内容,但是我已经对它们进行了搜索,但它们并没有与我的意图相结合,所以请不要只是-1这篇文章!

I've got a database with some tables. 我有一个包含一些表格的数据库。 One of the tables is named 'leerlingen' and I want to add data into that table. 其中一个表名为'leerlingen',我想在该表中添加数据。 I'm from the Netherlands, "leerlingen" means "students". 我来自荷兰,“leerlingen”的意思是“学生”。 So, I'd like to add a student into the table. 所以,我想在桌子上加一个学生。 The table has 4 things: leerling_id, leerling_naam, leerling_nummer and klas_id. 该表有4个东西:leerling_id,leerling_naam,leerling_nummer和klas_id。 The last one is weird, right? 最后一个很奇怪,对吧? Nope, because I've got another table with just one column: klas_id. 不,因为我有另一个只有一列的表:klas_id。 That's for later, though. 不过这是为了以后的事情。

'Klas' means 'group' (yep, I'm learning you guys Dutch haha :D), and I've got several groups. 'Klas'的意思是'小组'(是的,我正在向你学习荷兰语哈哈:D),我有几个小组。 But, you can just add a new group by another form. 但是,您可以通过其他表单添加新组。

Let's get to business: I've created a HTML form: 让我们开展业务:我创建了一个HTML表单:

<!-- start form -->

    <form action="insert_leerling.php" method="post">
<input type="text" name="leerling_naam" class="inp-form">
<input type="text" name="leerling_nummer" class="inp-form">

<?php

$con = mysql_connect("localhost","root","root");
if (!$con)
  {
   die('Could not connect: ' . mysql_error());
  }

mysql_select_db("sms", $con);

 $query = "SELECT * FROM klas ORDER BY klas_id";
 $result = mysql_query($query);

echo "<select class='inp-form' id='klas_id'>\n";

while ($row = mysql_fetch_assoc($result)) {
  if (1 == 1) {
    echo '<option name="'.htmlspecialchars($row['klas_id']).'">'.htmlspecialchars($row['klas_id']).'</option>';
  }
}

echo "</select>";

?>
            <input type="submit" value="Voeg toe" class="form-submit" />

    </form>

<!-- end form  -->

So, there are 3 things inserted: leerling_naam (means student name), leerling_nummer (means student number = mobile phone number) and klas_id. 因此,插入了3个内容:leerling_naam(表示学生姓名),leerling_nummer(表示学生编号=手机号码)和klas_id。 I'm getting the klas_id from the little php script, so the user sees a html dropdown menu with all the klas (groups). 我从小php脚本中获取klas_id,因此用户可以看到包含所有klas(组)的html下拉菜单。 When I run the form, it sends you to this piece of code (another page): 当我运行表单时,它会将您发送到这段代码(另一页):

 <?php
    $con = mysql_connect("localhost","root","root");
    if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("sms", $con);

$leerling_naam = $_POST['leerling_naam'];
$leerling_nummer = $_POST['leerling_nummer'];
$klas_id = $_POST['klas_id'];

$sql_insert = mysql_query("
                           INSERT INTO leerlingen 
                           (leerling_naam, leerling_nummer, klas_id) 
                           VALUES 
                           ('$leerling_naam', '$leerling_nummer', '$klas_id')
                         ");

?>

Now, here are the problems.. 现在,这里有问题..

  1. The 'klas_id' isn't inserted into the table, it's just blanco. 'klas_id'没有插入表中,它只是blanco。
  2. I want the first column (leerling_id, that's the normal id from a mysql table) to be +1 the whole time, I'm not using that piece of code. 我希望第一列(leerling_id,这是mysql表中的普通id)一直是+1,我没有使用那段代码。 When I add new data, the leerling_id IS going +1, but when I'm removing the first row (let's say I kicked a student), the data isn't dating itself up (haha, the word 'updating') 当我添加新数据时,leerling_id IS会变为+1,但是当我删除第一行时(假设我踢了一个学生),数据并没有自己约会(哈哈,“更新”这个词)

If you've read all the way trough here: well done. 如果你已经完全阅读了这里:干得好。 My English is not that good (if you find some dramatic grammar mistakes, you're allowed to tell me :) ). 我的英语不太好(如果你发现一些戏剧性的语法错误,你可以告诉我:))。 I also understand that this was quite hard to follow, sorry for that.. 我也明白这很难理解,对不起...

Thanks! 谢谢!

echo "<select class='inp-form' id='klas_id'>\n";

Should read 应该读

echo "<select name='klas_id' class='inp-form' id='klas_id'>\n";

You didn't give your select form element a name. 您没有为选择表单元素指定名称。 Id and Name are not interchangeable in this situation. 在这种情况下,ID和名称不可互换。

In answer to your second question, why do you want the data to be completely sequential? 在回答第二个问题时,为什么要将数据完全按顺序排列? There are no easy ways to make the database records always sequential, especially if this is the primary key of the row. 没有简单的方法可以使数据库记录始终是顺序的,特别是如果这是行的主键。

Rather than delete records from the database, I would suggest that this is a good case for "soft record deletion", that is, add a new column to your table called 'active' with a data type of ENUM('Y','N') . 不是从数据库中删除记录,我建议这是“软记录删除”的一个好例子,也就是说,在表中添加一个名为“active”的新列,其数据类型为ENUM('Y','N') If you want to delete them, simply mark this field as 'N' , and when querying for students, only look for records with a 'Y' in the active column. 如果要删除它们,只需将此字段标记为'N' ,并在查询学生时,仅查找活动列中带有'Y'记录。

If you want to return your students with a sequential number in your query, look at a rownumber: @rownum:=@rownum+1 although this number will have absolutely no direct link to the id in the database. 如果您想在查询中返回带有序号的学生,请查看一个rownumber: @rownum:=@rownum+1虽然这个数字绝对没有直接链接到数据库中的id

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

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