简体   繁体   English

如何在mysql数据库中将数据从一张表插入和检索到另一张表

[英]How to insert and retrive data from one table to another table in mysql database

I have two Tables Page and Upload我有两个表格页面和上传

PAGE:页:

page_id,   page_name,   page_parent 
( 4,   test,   0)
( 5,   test-sub,   4)
( 6,   test-sub2,   4)

UPLOAD:上传:

upload_id,   upload_name,   upload_root
(1,    test,   4)
(2,    test-sub,   4)
(3,   test-sub2,   4)

I was quite successful to get the page_id into upload_root but got problem when the page has submenu and is entered into page_parent.我非常成功地将 page_id 放入 upload_root 中,但是当页面有子菜单并进入 page_parent 时出现问题。

My insert command goes like this:我的插入命令是这样的:

$name =  $_POST['name'];
$root = $_POST['root'];
$insert_sql = "INSERT INTO tbl_upload VALUES ('' , \"$name\",  \"$root\")";

$root = $_POST['page_id'];?>
<form action="" enctype="multipart/form-data" method="post">
<div>Name:   <input name="title" type="text" size="50"/></div> 
<input name="page_id" type="hidden" value= "<?php echo"$page_id"; ?>"
<input type="submit" value="Upload" id="upload" class="upload"/>
</form>

My fetch command goes like this:我的 fetch 命令是这样的:

$fetch_sql = "SELECT upload_id, upload_name, upload_root from tbl_upload WHERE upload_root = \"$page_id\"";

I cannot get the correct url of the pages.我无法获得页面的正确网址。 I'm not getting the correct words to describe my problem.我没有得到正确的词来描述我的问题。 But what I exatly want is to retrive the exact value of page_id (which is auto increment) into upload_id if not possible then to upload_root.但是我非常想要的是将 page_id(这是自动增量)的确切值检索到upload_id 中,如果不可能,然后到upload_root。

What I want to do is :我想做的是:

UPLOAD:上传:

upload_id,   upload_name,   upload_root
(4,   test,   4)
(5,   test-sub,   4)
(6,   test-sub2,   4)
<?php
if (isset($_GET['submit']))
{
$name =  $_POST['title'];
$pageid = $_POST['page_id'];
$insert_sql = "INSERT INTO tbl_upload (upload_id, upload_name, upload_root) VALUES ('".$pageid."','".$name."','')";
mysql_query($insert_sql);
}
?>
<form action="your_page.php?submit=yes" enctype="multipart/form-data" method="post">
<div>Name:   <input type="text" name="title" size="50"></div> 
<input type="hidden" name="page_id" value= "<?php echo $page_id; ?>">
<input type="submit" value="Upload" id="upload" class="upload">
</form>

Obviously, insert the name of page where it says 'your_page.php'显然,在显示“your_page.php”的地方插入页面名称

Your code is vulnerable to SQL injection.您的代码容易受到 SQL 注入的影响。 Please look up how to implement PDO for future projects.请查看如何为未来的项目实施 PDO。

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

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