如何在一个表中插入和检索数据到mysql数据库中的另一个表 [英] How to insert and retrive data from one table to another table in mysql database
问题描述
我有两个表页面和上传
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)
我非常成功地获得page_id到upload_root,但有一个问题,当页面有子菜单,并输入page_parent。
我的插入命令如下:
$ name = $ _POST ['name'];
$ root = $ _POST ['root'];
$ insert_sql =INSERT INTO tbl_upload VALUES('',\$ name\,\$ root\);
$ root = $ _POST ['page_id'];?>
< form action =enctype =multipart / form-datamethod =post>
< div>名称:< input name =titletype =textsize =50/>< / div>
< input name =page_idtype =hiddenvalue =<?php echo$ page_id;?>
< input type =submitvalue =上传id =上传class =上传/>
< / form>
我的fetch命令如下:
$ fetch_sql =SELECT upload_id,upload_name,upload_root from tbl_upload WHERE upload_root = \$ page_id\;
我无法获取正确的网页网址。我没有得到正确的话来描述我的问题。但我真正想要的是将page_id(这是自动递增)的确切值转换成upload_id,如果不可能,然后到upload_root。
我想要做的是:
上传:
$ b bupload_id,upload_name,upload_root
(4,test,4)
(5,test-sub,4)
-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 = yesenctype =multipart / form-datamethod =post>
< div>名称:< input type =textname =titlesize =50>< / div>
< input type =hiddenname =page_idvalue =<?php echo $ page_id;?>>
< input type =submitvalue =上传id =上传class =上传>
< / form>
显然,请插入页面名称your_page.php
您的代码容易遭受SQL注入。请查看如何为未来的项目实施PDO。
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.
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_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.
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 code is vulnerable to SQL injection. Please look up how to implement PDO for future projects.
这篇关于如何在一个表中插入和检索数据到mysql数据库中的另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!