简体   繁体   English

如何在PHP中的另一个表中插入另一个表ID

[英]How to insert another table id into another table in PHP

I am using PHP and MySQL. 我正在使用PHP和MySQL。 XAMPP version 5.6.0.0 XAMPP版本5.6.0.0

I have two tables cat and image . 我有两张桌子catimage

cat table has two fields cat_id and cat_name . cat表具有两个字段cat_idcat_name

image table has four fields img_id , cat_id , name and img_path . image表具有四个字段img_idcat_idnameimg_path

There is a dropdown list and it's appearing the values of cat table. 有一个下拉列表,它显示cat表的值。

I want to store DISTINCT cat_id from the cat table to the cat_id in the image table when I choose a value from the dropdown list. 当我从下拉列表中选择一个值时,我想将DISTINCT cat_idcat表存储到image表中的cat_id

My code is following. 我的代码如下。 But everytime the cat_id field in the image table has 0 (zero) value. 但是每次image表中的cat_id字段具有0 (零)值。

<?php 
$hostname_phpimage = "localhost";
$database_phpimage = "phpimage";
$username_phpimage = "root";
$password_phpimage = "";

$con = mysql_pconnect($hostname_phpimage, $username_phpimage, $password_phpimage) or trigger_error(mysql_error(), E_USER_ERROR);
error_reporting(0);
?>
<p>Select a Category : 
<select name="image_upload">
        <?php
            $getData = mysql_query("SELECT cat_id,cat_name FROM cat");
            while($viewData = mysql_fetch_array($getData)) 
            { ?>
                <option id="<?php echo $viewData['cat_id']; ?>"><?php echo $viewData['cat_name']; ?></option>
            <?php } ?>
    </select>
    </p>

    <?php

   if($_POST['submit'])
  {
    $name = basename($_FILES['file_upload']['name']);
    $t_name = $_FILES['file_upload']['tmp_name'];
    $dir = 'upload';

    $cat=$_POST['cat'];

    if(move_uploaded_file($t_name, $dir."/".$name))
    {
        mysql_select_db($database_phpimage,$con);
        $getData = mysql_query("SELECT cat_id,cat_name FROM cat");

        $cat_id = $_POST['cat_id'];

        $getQuery="INSERT INTO image (img_id, cat_id, name, img_path) VALUES ('', '$_GET[cat_id]', '$name', 'upload/$name')";
        $viewQuery=mysql_query($getQuery,$con);

        echo "File Upload Successfully";        
    }
    else
    {
        echo "Upload Failed";
    }   
    }
  ?>

 <html>
 <title></title>
 <head></head>
 <body>

 <form action="upload.php" method="post" enctype="multipart/form-data">

<input type="file" name="file_upload" /><br/><br/>  
<input type="submit" name="submit" value="Upload" />

</form>

</body>
</html>

I found the solution myself and here is the answer. 我自己找到了解决方案,这就是答案。

 <html>
 <title></title>
 <head></head>
 <body>

 <form action="upload.php" method="post" enctype="multipart/form-data">

 <?php 
 include "conn.php";
 error_reporting(0);
 ?>
 <p>Select a Category : 
 <select name="cid">
        <?php
            $getData = mysql_query("SELECT * FROM cat");
            mysql_select_db($database_phpimage,$con);
            while($viewData = mysql_fetch_array($getData)) 
            { ?>
                <option value="<?php echo $viewData['cat_id']; ?>"><?php echo $viewData['cat_name']; ?></option>
            <?php } ?>
    </select>
    </p>


    <?php

   if($_POST['submit'])
   {
    $name = basename($_FILES['file_upload']['name']);
    $t_name = $_FILES['file_upload']['tmp_name'];
    $dir = 'upload';

    if(move_uploaded_file($t_name, $dir."/".$name))
    {

        $sql2 = "SELECT * FROM cat";
        mysql_select_db($database_phpimage,$con);
        $rowData = mysql_query($sql2);

        $cid = $_GET['cat_id'];

        $sql = mysql_query("SELECT * FROM `image` WHERE name = '$name'");
        if(mysql_num_rows($sql) > 0){
            echo "<script type='text/javascript'>alert('Sorry.... That Input Is DUPLICATE.')</script>";
            exit();
        }

        $sql3="INSERT INTO image (img_id, cid, name, img_path) VALUES ('', '$_POST[cid]', '$name', 'upload/$name')";
        mysql_query($sql3);

        echo "File Upload Successfully";        
      }
      else
     {
        echo "Upload Failed";
     }  
    }
 ?>

   <input type="file" name="file_upload" /><br/><br/>   
 <input type="submit" name="submit" value="Upload" />

 <h1>View Albums</h1>

 <?php 
 $sql4 = mysql_query("SELECT * FROM cat");
 mysql_select_db($database_phpimage, $con);

 while($rowData4 = mysql_fetch_array($sql4))
 { ?>
    <p><a href="image_view.php?cid=<?php echo $rowData4['cat_id']; ?>" target="_blank"><?php echo $rowData4['cat_name']; ?></a></p>

 <?php } ?>

  <br/><br/>
 <p><a href="image_names.php" target="_blank">Image Names</a></p>

 </form>
 </body>
 </html>

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

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