简体   繁体   English

使用一种形式上载到多个数据库表

[英]Using one form to upload into multiple database tables

I need to use one form to upload a "holiday home". 我需要使用一种表格上传“度假屋”。 But the details needed for the property are stored in multiple tables - Properties, Prices, County and Image. 但是,该属性所需的详细信息存储在多个表中-属性,价格,县和图像。 This is my form code: 这是我的表单代码:

<form class="pure-form pure-form-stacked" action="propsubmit.php" 
method="post" name="uploadproperty"  enctype="multipart/form-data" 
style="margin-left: 20px; color:black">
    <fieldset><br>

        <label for="propertyname" class="button button-alt" 
style="background-color:white" >Property Name:</label>
        <input id="propertyname" name="propertyname" type="text" placeholder="Enter name here.... " size="50" required="required"/><br>

        <label for="description" class="button button-alt" style="background-color:white "> Description:</label>
        <textarea rows="10" cols="100" name="description" placeholder="Enter description here...." required="required"></textarea><br>    

        <label for="county" class="button button-alt" style="background-color:white">County:</label>
        <select id="county" name="county" placeholder="Choose County" required="required">
        <option value="antrim">Antrim</option>
        <option value="down">Down</option>
        <option value="derry">Derry</option>
        </select>

        <label for="amin" class="button button-alt" style="background-color:white" >Autumn minimum price:</label>
        <input id="amin" name="amin" type="text" placeholder="Price.... " size="10" required="required"/><br>

        <label for="amax" class="button button-alt" style="background-color:white" >Autunm maximum price:</label>
        <input id="amax" name="amax" type="text" placeholder="Price.... " size="10" required="required"/><br>

        <label for="spmin" class="button button-alt" style="background-color:white" >Spring minimum price:</label>
        <input id="spmin" name="spmin" type="text" placeholder="Price.... " size="10" required="required"/><br>

        <label for="spmax" class="button button-alt" style="background-color:white" >Spring maximum price:</label>
        <input id="spmax" name="spmax" type="text" placeholder="Price.... " size="10" required="required"/><br>

        <label for="sumin" class="button button-alt" style="background-color:white" >Summer minimum price:</label>
        <input id="sumin" name="sumin" type="text" placeholder="Price.... " size="10" required="required"/><br>

        <label for="sumax" class="button button-alt" style="background-color:white" >Summer maximum price:</label>
        <input id="sumax" name="sumax" type="text" placeholder="Price.... " size="10" required="required"/><br>  

        <label for="wmin" class="button button-alt" style="background-color:white" >Winter minimum price:</label>
        <input id="wmin" name="wmin" type="text" placeholder="Price.... " size="10" required="required"/><br>  

        <label for="wmax" class="button button-alt" style="background-color:white" >Winter maximum price:</label>
        <input id="wmax" name="wmax" type="text" placeholder="Price.... " size="10" required="required"/><br>       

        <label for="propimage"  class="button button-alt" style="background-color:white">Images:</label> 
        <input name="propimgs" type="file" multiple="required"/><br>  





        <br>

        <button type="submit" class="button button-alt" name="search">Upload Property</button>
        <button type="reset" class="button button-alt">Reset</button>


          </fieldset>
  </form>

This is my submitprop code: 这是我的submitprop代码:

<?php
include("../connections/conn.php");



$sentpropname = $_POST['propertyname'];
$sentpropdesc = $_POST['description'];

$insertquery1 = "INSERT INTO Properties (ID, Name, Description, County_ID)
                    VALUES (NULL, '$sentpropname', '$sentpropdesc', NULL)";
$result1 = mysqli_query($conn, $insertquery1);

$sentpropcounty = $_POST['county'];

$insertquery2 = "INSERT INTO County (ID, County)
                    VALUES (NULL, '$sentpropcounty')";
$result2 = mysqli_query($conn, $insertquery2);

$filename = $_FILES['propimgs']['name']; 
$filetemp = $_FILES['propimgs']['tmp_name'];


move_uploaded_file($filetemp,"../images/$filename");


$insertquery3 = "INSERT INTO Image (ID, Property_ID, Path) VALUES (NULL, 0, 
'$filename')";
$result3 = mysqli_query($conn, $insertquery3);



$sentpriceautummin = $_POST['amin'];
$sentpriceautummax = $_POST['amax'];
$sentpricespringmin = $_POST['spmin'];
$sentpricespringmax = $_POST['spmax'];
$sentpricesummermin = $_POST['sumin'];
$sentpricesummermax = $_POST['sumax'];
$sentpricewintermin = $_POST['wmin'];
$sentpricewintermax = $_POST['wmax'];

$insertpricequery = "INSERT INTO Prices"
  . "(id, Property_ID, AutumnMinPrice, AutumnMaxPrice, SpringMinPrice, "
  . "SpringMaxPrice, SummerMinPrice, SummerMaxPrice, WinterMinPrice, 
  WinterMaxPrice)"
  . "VALUES(0, 0, '$sentpriceautummin', '$sentpriceautummax', "
  . "'$sentpricespringmin', '$sentpricespringmax', '$sentpricesummermin', "
  . "'$sentpricesummermax', '$sentpricewintermin', '$sentpricewintermax')";

      $resultpricequery = mysqli_query($conn, $insertpricequery) or die(mysqli_error($conn));
      mysqli_close($conn);

When I click upload, the property is not uploading into the database. 当我单击上载时,该属性未上载到数据库中。 Is there any specific thing I've missed out in my code? 我在代码中错过了什么具体的东西吗?

Try this 尝试这个

  $tableNames = ['one','two','three'];

    foreach($value in $tableNames){

    $query = "insert into '".$value."' values()";
    mysqli_query($conn, $query);
    }

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

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