简体   繁体   English

从一个表中获取ID并使用php将其存储到另一个表中

[英]fetch id from one table and store it into another using php

I have a 2 page form. 我有一个2页的表格。 First page takes input from user regarding property ans stores it in retailer_add_property table. 第一页从用户那里获取有关属性的输入,并将其存储在retailer_add_property表中。 after this page the user is redirected to next page admin_add_property_images.php where user can store multiple images (table name is propertyimages )of the property that was entered in the previous page. 在此页面之后,用户将重定向到下一页admin_add_property_images.php ,用户可以在其中存储上一页中输入的属性的多个图像(表名称为propertyimages )。 what I am trying to do is to fetch the last inserted id of retailer_add_property table which will be created in the first form and store it in the propertyimages table 我想要做的是获取将以第一种形式创建的retailer_add_property表的最后插入的ID,并将其存储在propertyimages表中

Form one: admin_add_property.php 表格一:admin_add_property.php

<form class="form-horizontal" role="form" action="admin_insert_property.php" enctype="multipart/form-data" method="post">
    <div class="form-group">
        <label class="col-lg-3 control-label">Property name:</label>
            <div class="col-lg-8">
                <input class="form-control" name="propertyname" value="" type="text" required>
            </div>
    </div>

    <div class="form-group">
        <label class="col-md-3 control-label">Property Type:</label>
            <div class="col-md-8">
                <select name="cancellation"  id="cancellation"  required>                       
                    <option value="yes">Yes</option>            
                    <option value="no">No</option>                  
                </select>               
            </div>
    </div>


    <div class="form-group">
        <label class="col-md-3 control-label"></label>
            <div class="submit">
                <input class="btn btn-primary" value="Save " type="submit" name="submit">

                <span></span>
            </div>  
    </div>

</form>

"admin_insert_property.php" “admin_insert_property.php”

<?php
include('admin_session.php');

$con=mysqli_connect("localhost","qwe","pwd","qwe");

if (mysqli_connect_errno()) 
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

$propertyname = mysqli_real_escape_string($con, $_POST['propertyname']);
$propertytype = mysqli_real_escape_string($con, $_POST['propertytype']);


$sql="INSERT INTO retailer_add_property (propertyname,propertytype) VALUES ('$propertyname','$propertytype')";

if (!mysqli_query($con,$sql)) 
    {
        die('Error: ' . mysqli_error($con));
    }

mysqli_query($con, $sql);   

header("Location: admin_add_property_images.php");

mysqli_close($con);

?>

Second Form: admin_add_property_images.php 第二种形式:admin_add_property_images.php

<form class="form-horizontal" role="form" action="admin_insert_property_images.php" enctype="multipart/form-data" method="post">
    <div class="form-group">
        <label class="col-md-3 control-label">Upload Image:</label>
        <div class="col-md-8">
            <input class="form-control" name="file" id="file" value="" type="file"  required>
        </div>
    </div>

    <div class="form-group">
        <label class="col-md-3 control-label"></label>
            <div class="submit">
                <input class="btn btn-primary" value="Save " type="submit" name="submit">
            </div>  
    </div>

</form>

admin_insert_property_images.php admin_insert_property_images.php

<?php
include('admin_session.php');

$con=mysqli_connect("localhost","qwe","pwd","qwe");
// Check connection
if (mysqli_connect_errno()) 
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
//Replace $mysqli with your $con then. $con->query($sql);   
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($extension, $allowedExts))  
    {
        if ($_FILES["file"]["error"] > 0) 
            {
                echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
            } 
        else 
            {
                if (file_exists("propertyimages/" . $_FILES["file"]["name"])) 
                    {
                        echo $_FILES["file"]["name"] . " already exists. ";
                    } 
                else 
                    {

                        $imagepath = "propertyimages/" . $_FILES["file"]["name"];
                        move_uploaded_file($_FILES["file"]["tmp_name"], $imagepath);
                        $sql="INSERT INTO propertyimages(propertyimage) VALUES ('".$imagepath."')";
                        if (!mysqli_query($con,$sql)) 
                            {
                                die('Error: ' . mysqli_error($con));
                            }
                    }
            }
    } 
else    
    {
        echo "Invalid file";
    }
?>

i tried to carry id from one page to next, but it didn't worked out the way i wanted. 我试图将ID从一页移到下一页,但是并没有按照我想要的方式解决。 an someone tell me how can it be done 有人告诉我怎么做

use mysqli_insert_id($con) fucntion save it in $_SESSION[''] and use it on any page with by adding session_start(); 使用mysqli_insert_id($con)功能将其保存在$_SESSION['']并通过添加session_start();在任何页面上使用它session_start(); at the top. 在顶部。

**Note:**use mysqli_insert_id() function after your insert query; **注意:**在插入查询后使用mysqli_insert_id()函数;

look at your quesiton part below 看看下面的问题部分

**File Name:**admin_insert_property.php" **文件名:** admin_insert_property.php”

Just look at the query secion there. 只需查看那里的查询部分。 it has to be like this 它必须像这样

$sql="INSERT INTO retailer_add_property (propertyname,propertytype) VALUES ('$propertyname','$propertytype')";

if (!mysqli_query($con,$sql)) 
    {
        die('Error: ' . mysqli_error($con));
    }

mysqli_query($con, $sql);   

$_SESSION['insert_id']=mysqli_insert_id($con);
//save the value in the $_SESSION


header("Location: admin_add_property_images.php");

Now you can use $_SESSION['insert_id']; 现在您可以使用$_SESSION['insert_id']; at any page, if you use session_start(); 在任何页面上,如果您使用session_start(); at the top 在顶部

check this link for more info http://php.net/manual/en/mysqli.insert-id.php 检查此链接以获取更多信息http://php.net/manual/en/mysqli.insert-id.php

use $mysqli->insert_id in admin_insert_property.php page and modify code like this and get that id on a dmin_add_property_images.php page and use it as per your requirement. admin_insert_property.php页面中使用$mysqli->insert_id并修改这样的代码,并在dmin_add_property_images.php页面上获取该ID,并根据需要使用它。

mysqli_query($con, $sql);   

$lastId = $mysqli->insert_id;

header("Location: admin_add_property_images.php?id=".$lastId);

You need to make use of the $_GET variable. 您需要使用$ _GET变量。 After the first insert just add in the header the id of the inserted item. 第一次插入后,只需在标题中添加插入项的ID。

header("Location: admin_add_property_images.php?id=". $inserted_id;

This way you can pass it to other pages. 这样,您可以将其传递到其他页面。

暂无
暂无

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

相关问题 从一个表中获取多行并将其存储到php Mysql中的另一个表中 - Fetch multiple rows from one table and Store to another table in php Mysql 从一个模型中获取数据并存储在另一个表中 - fetch data from one model and store in another table 如何使用已记录的用户会话ID将数据从一个表获取到另一个表 - How to fetch data from one table to another using the logged user session id 如何从一个表中获取数据并插入到php中的另一个表中 - how to fetch data from one table and insert into another table in php 从会话中获取ID并将其存储到另一个表中 - fetch id from session and store into a different table 从一个表访问ID并将其插入到另一个表-PHP - Accessing the ID from one table and inserting it into another table - PHP PHP-当一个表的ID必须与另一个表的ID相同时,如何获取帖子? - PHP - How to fetch posts when the ID of one table has to be identical to another table? 使用posgres和php从数据库中的一个表中获取不在另一个数据库中另一个表中的所有ID - Getting all id's from one table in database that aren't in another table in another database using posgres and php 在MySQL php中将ID从一个表插入另一个表 - Insert ID from one table to another in MySQL php 如何在一行中获取具有相同 id 但在另一列中具有不同值的数据并在 php 中的表中显示一个月 - How to fetch data with same id in a row but different value in another column and display one month into table in php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM