简体   繁体   English

使用PHP和html表单向MYSQL插入多个输入

[英]Inserting multiple inputs to MYSQL using PHP and html form

I am trying to read the columns and rows entered in the html form to be inserted into MYSQL database using php. 我试图读取在html表单中输入的列和行,使用php插入到MYSQL数据库中。

It is giving me the following error: 它给了我以下错误:

Undefined index: maintenance on line 28 未定义的索引:第28行的维护

Undefined index: outages 未定义的索引:中断

Undefined index: morningcheck 未定义的索引:morningcheck

Undefined offset: 1 未定义的偏移量:1

Please advise as to how to make this work. 请告知如何完成这项工作。

<?php
require_once('config.php');

/*Open the connection to our database use the info from the config file.*/   

$link = mysqli_connect('localhost', DB_USER, DB_PASSWORD, DB_NAME);

if (!$link) {
    die('Could not connect: ' . mysqli_connect_error());
}

if(isset($_POST['add'])) {      
    foreach($_POST['address'] as $k => $address) {
        $address = mysqli_real_escape_string($link, $address);
        $morningcheck = mysqli_real_escape_string($link, $_POST['morningcheck'][$k]);
        $outages = mysqli_real_escape_string($link, $_POST['outages'][$k]);
        $maintenance = mysqli_real_escape_string($link, $_POST['maintenance'][$k]);
        $sql="INSERT INTO demo ( address,morningcheck, outages,  maintenance) VALUES ('$address','$morningcheck','$outages','$maintenance')";  
        if (!mysqli_query($link,$sql)) {
        die('Error: ' . mysqli_error($link));   }       
    }}

mysqli_close($link);
?>

Here is the html form: 这是html表单:

<form action="process.php" method="post">

<style>
td{
    border: 1px solid black;
}
</style>
<input type="hidden" name="formID" value= "demo" />

<table>
<tr>
    <td>
    <br>
    <b>Email Address </><input type="text" name="address[]"  />
    <input type="checkbox" name="morningcheck[]" value="morningcheck"> Morning Checks 
    <input type="checkbox" name="outages[]" value="outages"> Outages
    <input type="checkbox" name="maintenance[]" value="maintenance"> Maintenance<br>
    <br>
    </td>
</tr>
<table>

<table>
<tr>
    <td>
    <br>
    <b>Email Address </><input type="text" name="address[]" placeholder= "Email address 1"  />
    <input type="checkbox" name="morningcheck[]" value="morningcheck"> Morning Checks
    <input type="checkbox" name="outages[]" value="outages"> Outages
    <input type="checkbox" name="maintenance[]" value="maintenance"> Maintenance<br>
    <br>
    </td>
</tr>
</table>


<br>
<input type="submit" value="Subscribe" name="add"/>
<input type="submit" value="Unsubscribe" name="delete"/>


</form>'

The issue is that there is an error being thrown because the variable is not present if it is not selected in the form, so you need to test for the variable when putting it into the sql like this 问题是抛出了一个错误,因为如果没有在表单中选择该变量,则该变量不存在,因此当将变量放入sql时需要测试该变量

if(isset($_POST['add'])) {      
foreach($_POST['address'] as $k => $address) {
    $address = mysqli_real_escape_string($link, $address);
    $morningcheck =(isset($_POST['morningcheck'][$k]))? mysqli_real_escape_string($link, $_POST['morningcheck'][$k]):NULL;
    $outages = (isset($_POST['outages'][$k]))?mysqli_real_escape_string($link, $_POST['outages'][$k]):NULL;
    $maintenance = (isset($_POST['maintenance'][$k]))?mysqli_real_escape_string($link, $_POST['maintenance'][$k]):NULL;
    $sql="INSERT INTO demo ( address,morningcheck, outages,  maintenance) VALUES ('$address','$morningcheck','$outages','$maintenance')";  
    if (!mysqli_query($link,$sql)) {
    die('Error: ' . mysqli_error($link));   }       

}} }}

What this is saying is, if the variable is set (The bit in the brackets) execute assigning the vaiable the bit before the ':'if not set it as null 这就是说,如果设置了变量(括号中的位),则在':'之前执行赋值,如果不将其设置为null

Good luck 祝好运

Keith 基思

To get what you want you should change your HTML like this : 要获得您想要的内容,您应该像这样更改HTML:

<tr>
  <td>
    <br>
    <b>Email Address </><input type="text" name="Example[][address]" placeholder= "Email address 1"  />
    <input type="checkbox" name="Example[][morningcheck]" value="morningcheck"> Morning Checks
    <input type="checkbox" name="Example[][outages]" value="outages"> Outages
    <input type="checkbox" name="Example[][maintenance]" value="maintenance"> Maintenance<br>
    <br>

This way server side you can retrieve the data like this: 这样服务器端就可以像这样检索数据:

if(isset($_POST["Example"]){
  foreach($_POST["Example"] as $example){
    $address = mysqli_real_escape_string($link, $example['address']);
    $morningcheck = mysqli_real_escape_string($link, $example['morningcheck']);
    $outages = mysqli_real_escape_string($link, $example['outages']);
    $maintenance = mysqli_real_escape_string($link, $example['maintenance']);
    $sql="INSERT INTO demo ( address,morningcheck, outages,  maintenance) VALUES ('$address','$morningcheck','$outages','$maintenance')";
    if (!mysqli_query($link,$sql))die('Error: ' . mysqli_error($link));
  }
}

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

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