1

我是 PHP 和 MySQL 的新手,正在尝试构建一个基本的联系表单跟踪数据库。请原谅我的术语。

我知道如何连接、插入变量,但我试图弄清楚如何处理将复选框提交插入数据库并让它与提交它的客户相关。

一个基本的例子:

HTML 表单:名称 电子邮件 感兴趣的领域

复选框的感兴趣区域部分有 5 个选项。我需要他们能够检查多个复选框,然后在数据库中插入和跟踪该数据。

我需要多张桌子吗?如果是这样,我如何将两者联系起来,以便将来报告信息?

我希望这是有道理的。我难住了。


很好的例子...谢谢。我自己发现了许多拼写错误并已更正,我添加了一些字段并重命名以满足我的需要,但仍然遇到非对象错误时对成员函数 fetch_assoc() 的相同调用

请看一下

<?php 
$mysqli=new mysqli("localhost","flyoasis_admin","oasis225826","flyoasis_main");

if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}

$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$email_address=$_POST['email_address'];
$phone=$_POST['phone'];
$refer=$_POST['refer'];

$first_name=$mysqli->real_escape_string($first_name);
$last_name=$mysqli->real_escape_string($last_name);
$email_address=$mysqli->real_escape_string($email_address);
$phone=$mysqli->real_escape_string($phone);
$refer=$mysqli->real_escape_string($refer);
$interest=array();

$mysqli->query("INSERT INTO 'contacts2'('first_name', 'last_name', 'email_address', 'phone', 'refer')    VALUES('".$first_name."','".$last_name."','".$email_address."','".$phone."','".$refer."')");
$result=$mysqli->query("SELECT 'id' FROM 'contacts2' WHERE 'email_address'='".$email_address."'");
$result=$result->fetch_assoc();
$customer_id=$result['id'];

$sql="";
foreach ($_POST['interest'] as $interest) {
$interest=$mysqli->real_escape_string($interest);
$sql.="('".$customer_id."','".$interest."'),";
}
$sql=substr($sql,0,-1);

$mysqli->query("INSERT INTO 'interest_areas'('customer_id','interest') VALUES".$sql);

$mysqli->close();

?>

到目前为止,我非常感谢您的帮助。

4

2 回答 2

2

我会有这个 SQL 表设置:

id,name,email
--------------
1,user224,user224@example.com
2,dave,dave@example.com

创建表将是:

CREATE TABLE `customers` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(32),
  `email` varchar(32),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uc_email` (`email`)
);

然后在另一张桌子上,我会保留所有客户的兴趣:

id,customer_id,interest
-----------------------
1,1,Golf
2,1,Badminton
3,1,Soccer
4,2,Badminton

如您所见,user244 的兴趣是高尔夫、羽毛球和足球,而 dave 的兴趣是羽毛球。您可以使用以下命令创建此表:

CREATE TABLE `customer_areas` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `customer_id` int(11),
  `interest` varchar(32),
  PRIMARY KEY (`id`),
  KEY `customer_id` (`customer_id`),
  CONSTRAINT `customer_areas_customer_id_ibfk_1` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`) ON DELETE CASCADE
);

现在,进入有趣的部分。我们需要一个可以处理多个复选框的 html 表单:

<!DOCTYPE html>
<html>
    <head>
        <title>Gathering Info</title>
    </head>
    <body>
        <form method="post" action="index.php">
            Name: <input type="text" name="name"><br>
            Email: <input type="text" name="email"><br>
            <input type="checkbox" name="interests[]" value="golf"> Golf<br>
            <input type="checkbox" name="interests[]" value="soccer"> Soccer<br>
            <input type="checkbox" name="interests[]" value="badminton"> Badminton<br>
            <input type="submit" value="Submit">
        </form>
    </body>
</html>

在后端:

<?php
//assuming you checked $_POST content already

$mysqli=new Mysqli("127.0.0.1","root","password","database");

$name=$_POST['name'];
$email=$_POST['email']

$name=$mysqli->real_escape_string($name);
$email=$mysqli->real_escape_string($email);
$interests=array();

$mysqli->query("INSERT INTO `customers`(`name`,`email`) VALUES('".$name."','".$email."')");
$result=$mysqli->query("SELECT `id` FROM `customers` WHERE `email`='".$email."'");
$result=$result->fetch_assoc();
$customer_id=$result['id'];

$sql="";
foreach ($_POST['interest'] as $interest) {
    $interest=$mysqli->real_escape_string($interest);
    $sql.="(".$customer_id.",'".$interest."'),";
}
$sql=substr($sql,0,-1);

$mysqli->query("INSERT INTO `customer_areas`(`customer_id`,`interest`) VALUES".$sql);
?>
于 2013-05-03T21:39:24.283 回答
0

You need a customer table. Related to the checkboxes you could do one of two things; either have 5 columns in customer which are the areas of interest or have abother table called customerInterests. If you have another table you will have 2 columns ( customerId, area) . For every interest a customer has you would have one row. For how to read the checkbox values a php tutorial is sufficient such as: http://www.html-form-guide.com/php-form/php-form-checkbox.html

于 2013-05-03T21:39:09.560 回答