【问题标题】:fetch id from one table and store it into another using php从一个表中获取 id 并使用 php 将其存储到另一个表中
【发布时间】:2014-10-10 10:27:47
【问题描述】:

我有一个 2 页的表格。第一页从用户那里获取有关属性的输入,并将其存储在retailer_add_property 表中。在此页面之后,用户被重定向到下一页admin_add_property_images.php,用户可以在其中存储上一页中输入的属性的多个图像(表名为propertyimages)。我要做的是获取retailer_add_property 表的最后插入ID,该表将以第一种形式创建并将其存储在propertyimages 表中

表格一: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”

<?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);

?>

第二种形式: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

<?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";
    }
?>

我尝试将 id 从一页转移到下一页,但没有达到我想要的效果。有人告诉我怎么做

【问题讨论】:

    标签: php mysql sql mysqli


    【解决方案1】:

    使用mysqli_insert_id($con)功能将其保存在$_SESSION['']中,并在任何页面上使用它,方法是在顶部添加session_start();

    **注意:**在插入查询后使用 mysqli_insert_id() 函数;

    看看你下面的问题部分

    **文件名:**admin_insert_property.php"

    只要看看那里的查询部分。应该是这样的

    $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");
    

    现在你可以在任何页面使用$_SESSION['insert_id'];,如果你 在顶部使用session_start();

    查看此链接了解更多信息http://php.net/manual/en/mysqli.insert-id.php

    【讨论】:

    • 所有这些工作都是在用户登录后完成的,所以我使用 admin_session 创建了登录系统,我可以在该会话中存储 id 的值还是必须创建另一个? ?
    • k,我试过了,但是当我试图以第二种形式回显 id 的值时,我得到的值是 '0'
    • 我已经输入了多个条目并在每个条目之后检查了值,无论是第一个条目还是第五个条目,我只得到 id 0 的值
    • 我浏览了手册并添加了语句 mysqli_query($con, $sql);在插入查询之后,现在我得到了 id,但是出现了一个新问题.. 每当我输入值时,它都会被添加两次
    • 抱歉回复晚了,感谢您的帮助,现在脚本正在运行
    【解决方案2】:

    admin_insert_property.php 页面中使用$mysqli-&gt;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);
    

    【讨论】:

    • 我已经尝试过了,但问题是我正在使用第二种形式的另一个表,所以当我使用这种方法时,我得到一个类似这样的 url:../admin_add_property_images。 php?id=3 我收到一条消息,指出此页面不存在
    • 然后正确检查您的页面名称和位置。由于传递变量,它没有任何错误
    • 我收到此错误,因为 admin_add_property_images.php 页面有另一个表,并且不存在 ID 为 3 的行
    【解决方案3】:

    您需要使用 $_GET 变量。 在第一次插入之后,只需在标题中添加插入项的 id。

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

    这样你就可以把它传递给其他页面了。

    【讨论】:

    • 我已经尝试过了,但问题是我正在使用第二种形式的另一个表,所以当我使用这种方法时,我得到一个类似这样的 url:../admin_add_property_images。 php?id=3 我收到一条消息,指出此页面不存在
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-30
    • 2021-08-25
    • 2018-01-22
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多