How to insert another table id into another table in PHP

时间:2015-08-07 02:32:50

标签: php mysql

I am using PHP and MySQL. XAMPP version 5.6.0.0

I have two tables cat and image.

cat table has two fields cat_id and cat_name.

image table has four fields img_id, cat_id, name and img_path.

There is a dropdown list and it's appearing the values of cat table.

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.

My code is following. But everytime the cat_id field in the image table has 0 (zero) value.

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

1 个答案:

答案 0 :(得分:0)

我自己找到了解决方案,这就是答案。

 <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>