需要在数据库中插入图像并在php和mysql中检索它

时间:2022-09-25 20:07:08

i am new to php to developing simple content management system i have a field like menu, position, visible, img_des, content, information_id .. it almost all working correctly now i need to insert image field to that . i have a separate file for forms and new page creation now the following code

我是php新手开发简单的内容管理系统我有一个像菜单,位置,可见,img_des,内容,information_id等字段..它几乎所有正常工作现在我需要插入图像字段。我有一个单独的文件表格和新页面创建现在以下代码

  if (isset($_POST['submit'])) {                   
  $errors = array();

// clean up data before putting in the database

//在放入数据库之前清理数据

$information_id = mysql_prep($_GET['info']);
$menu = trim(mysql_prep($_POST['menu']));  // cut out whitespace for menu

$position = mysql_prep($_POST['position']);
$visible = mysql_prep($_POST['visible']);
$image = mysql_prep($_POST['image']);
$img_des = mysql_prep($_POST['img_des']);
$content = mysql_prep($_POST['content']);

 if(empty($errors)){
 $query = "INSERT INTO pages (
 menu, position, visible, img_des, content, information_id
 ) VALUES (
 '{$menu}', {$position}, {$visible},'{$img_des}', '{$content}',  {$information_id}
    )";
 if ($result = mysql_query($query, $connection))    {
 $message = "The new page was created successfully";
 $new_page_id = mysql_insert_id();  
 redirect_to("content.php?page={$new_page_id}");
 } else {
 $message = "The page failed to create";

 }
 ?>

before img_des i want to insert image. what type i have i have to use and what code i have to put to insert and retrieve image.

在img_des之前我想插入图片。我必须使用什么类型以及我必须插入和检索图像的代码。

2 个解决方案

#1


0  

You shouldn't be storing images in the database. Save the image on the server and save a link to the files location in the database.

您不应该将图像存储在数据库中。将图像保存在服务器上,并保存指向数据库中文件位置的链接。

see this question for more information: What should I use to save a image in database

有关详细信息,请参阅此问题:我应该使用什么来保存数据库中的图像

#2


0  

  try this code.


<?php


        $images_arr = array();
            //This is the directory where images will be saved
        $target_dir = "uploads/";
        $target = $target_dir.$_FILES['photo']['name'];

            //$target = $target . basename( $_FILES['photo']['name']);

            //This gets all the other information from the form
        $name=$_POST['nameMember'];
        $bandMember=$_POST['bandMember'];
        $pic=($_FILES['photo']['name']);
        $about=$_POST['aboutMember'];
        $bands=$_POST['otherBands'];


            // Connects to your Database
        mysql_connect("Localhost", "remote", "remote123") or die(mysql_error()) ;
        mysql_select_db("test") or die(mysql_error()) ;

            //Writes the information to the database
        mysql_query("INSERT INTO dbProfiles (nameMember,bandMember,photo,aboutMember,otherBands)
        VALUES ('$name', '$bandMember', '$pic', '$about', '$bands')") ;

        //Writes the photo to the server
        if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)){

                //Tells you if its all ok
            echo "The file ". $target_dir.$_FILES['photo']['name']. " has been uploaded, and your information has been added to the directory";
            $images_arr[] = $target;
        }
        else {

            //Gives and error if its not
            echo "Sorry, there was a problem uploading your file.";
        }

?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="icon" href="http://www.thesoftwareguy.in/favicon.ico" type="image/x-icon" />
                <!--iOS/android/handheld specific -->
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="Upload multiple images create thumbnails and save path to database with php and mysql">
        <meta name="keywords" content="php, mysql, thumbnail,upload image, check mime type">
        <meta name="author" content="Shahrukh Khan">
        <title>Upload multiple images create thumbnails and save path to database with php and mysql </title>
        <link rel="stylesheet" href="style.css" type="text/css" />
        <style>
             .files{height: 30px; margin: 10px 10px 0 0;width: 250px; }
            .add{ font-size: 14px; color: #EB028F; border: none; }
            .rem a{ font-size: 14px; color: #f00; border: none; }
            .submit{width: 110px; height: 30px; background: #6D37B0; color: #fff;text-align: center;}
        </style>
        <script src="jquery-1.9.0.min.js"></script>
        <script>
            $(document).ready(function() {
                $(".add").click(function() {
                $('<div><input class="files" name="user_files[]" type="file" ><span class="rem" ><a href="javascript:void(0);" >Remove</span></div>').appendTo(".contents");

                });
                $('.contents').on('click', '.rem', function() {
                $(this).parent("div").remove();
                });

            });
        </script>
    </head>
    <body>
        <form name="f1" action="index.php" method="post" enctype="multipart/form-data">  
            <input type="hidden" name="image_form_submit" value="1"/>
                <div id="container">
                    <div id="body">
                        <div class="mainTitle" >Upload multiple images create thumbnails and save path to database with php and mysql</div>
                        <div class="height20"></div>
                        <article>
                            <div class="height20"></div>
                            <div style="width: 380px; margin: 0 auto;">
                                <h3 style="text-align: center;">Image will be resized to 100px X 100px </h3>

                                <p>Please Enter the Band Members Name.</p>
                                <p> Band Member or Affiliates Name:</p>
                                <input type="text" name="nameMember"/>
                                <p>
                                     Please Enter the Band Members Position. Example:Drums.
                                </p>
                                 <p>
                                    Band Position:
                                 </p>
                                <input type="text" name="bandMember"/>
                                <p>
                                    Please Upload a Photo of the Member in gif or jpeg format. The file name should be named after the Members name. If the same file name is uploaded twice it will be overwritten! Maxium size of File is 35kb.
                                </p>
                                <p>
                                    Photo:
                                </p>
                                <input type="hidden" name="size" value="350000">
                                <input type="file" name="photo"> 
                            <p>
                                Please Enter any other information about the band member here.
                            </p>
                            <p>
                                 Other Member Information:
                            </p>
                            <textarea rows="10" cols="35" name="aboutMember">
                            </textarea>
                             <p>
                                 Please Enter any other Bands the Member has been in.
                             </p>
                            <p>
                                     Other Bands:
                            </p>
                                <input type="text" name="otherBands" size=30 />
                            <br/>
                            <br/>
                                <input TYPE="submit" name="upload" title="Add data to the Database" value="Add Member"/>




                        </div>
                        <table class="bordered">
                         <?php
          // fetch all records
          $sql = "SELECT * FROM dbProfiles WHERE 1 "; ?>
          <?php
         $sql="SELECT * FROM dbProfiles";
 $result_set=mysql_query($sql);
 while($row=mysql_fetch_array($result_set))
 {
  ?>
        <tr>
       <td><p><?php echo $row['nameMember'] ?>"</p></td>
        <td><img src="uploads/<?php echo $row['photo'] ?>" width="200px" height="200px;"/><p><?php echo $row['nameMember'] ?>"</p></td>
        </tr>
        <?php
 }
 ?>
 </table>

                    </article>
                </div>

            </div>
            <div class="height10"></div>

    </form>

  </body>
</html>
<?php

function errorMessage($str) {
  return '<div style="width:50%; margin:0 auto; border:2px solid #F00;padding:2px; color:#000; margin-top:10px; text-align:center;">' . $str . '</div>';
}

function successMessage($str) {
  return '<div style="width:50%; margin:0 auto; border:2px solid #06C;padding:2px; color:#000; margin-top:10px; text-align:center;">' . $str . '</div>';
}
?>

#1


0  

You shouldn't be storing images in the database. Save the image on the server and save a link to the files location in the database.

您不应该将图像存储在数据库中。将图像保存在服务器上,并保存指向数据库中文件位置的链接。

see this question for more information: What should I use to save a image in database

有关详细信息,请参阅此问题:我应该使用什么来保存数据库中的图像

#2


0  

  try this code.


<?php


        $images_arr = array();
            //This is the directory where images will be saved
        $target_dir = "uploads/";
        $target = $target_dir.$_FILES['photo']['name'];

            //$target = $target . basename( $_FILES['photo']['name']);

            //This gets all the other information from the form
        $name=$_POST['nameMember'];
        $bandMember=$_POST['bandMember'];
        $pic=($_FILES['photo']['name']);
        $about=$_POST['aboutMember'];
        $bands=$_POST['otherBands'];


            // Connects to your Database
        mysql_connect("Localhost", "remote", "remote123") or die(mysql_error()) ;
        mysql_select_db("test") or die(mysql_error()) ;

            //Writes the information to the database
        mysql_query("INSERT INTO dbProfiles (nameMember,bandMember,photo,aboutMember,otherBands)
        VALUES ('$name', '$bandMember', '$pic', '$about', '$bands')") ;

        //Writes the photo to the server
        if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)){

                //Tells you if its all ok
            echo "The file ". $target_dir.$_FILES['photo']['name']. " has been uploaded, and your information has been added to the directory";
            $images_arr[] = $target;
        }
        else {

            //Gives and error if its not
            echo "Sorry, there was a problem uploading your file.";
        }

?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="icon" href="http://www.thesoftwareguy.in/favicon.ico" type="image/x-icon" />
                <!--iOS/android/handheld specific -->
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="Upload multiple images create thumbnails and save path to database with php and mysql">
        <meta name="keywords" content="php, mysql, thumbnail,upload image, check mime type">
        <meta name="author" content="Shahrukh Khan">
        <title>Upload multiple images create thumbnails and save path to database with php and mysql </title>
        <link rel="stylesheet" href="style.css" type="text/css" />
        <style>
             .files{height: 30px; margin: 10px 10px 0 0;width: 250px; }
            .add{ font-size: 14px; color: #EB028F; border: none; }
            .rem a{ font-size: 14px; color: #f00; border: none; }
            .submit{width: 110px; height: 30px; background: #6D37B0; color: #fff;text-align: center;}
        </style>
        <script src="jquery-1.9.0.min.js"></script>
        <script>
            $(document).ready(function() {
                $(".add").click(function() {
                $('<div><input class="files" name="user_files[]" type="file" ><span class="rem" ><a href="javascript:void(0);" >Remove</span></div>').appendTo(".contents");

                });
                $('.contents').on('click', '.rem', function() {
                $(this).parent("div").remove();
                });

            });
        </script>
    </head>
    <body>
        <form name="f1" action="index.php" method="post" enctype="multipart/form-data">  
            <input type="hidden" name="image_form_submit" value="1"/>
                <div id="container">
                    <div id="body">
                        <div class="mainTitle" >Upload multiple images create thumbnails and save path to database with php and mysql</div>
                        <div class="height20"></div>
                        <article>
                            <div class="height20"></div>
                            <div style="width: 380px; margin: 0 auto;">
                                <h3 style="text-align: center;">Image will be resized to 100px X 100px </h3>

                                <p>Please Enter the Band Members Name.</p>
                                <p> Band Member or Affiliates Name:</p>
                                <input type="text" name="nameMember"/>
                                <p>
                                     Please Enter the Band Members Position. Example:Drums.
                                </p>
                                 <p>
                                    Band Position:
                                 </p>
                                <input type="text" name="bandMember"/>
                                <p>
                                    Please Upload a Photo of the Member in gif or jpeg format. The file name should be named after the Members name. If the same file name is uploaded twice it will be overwritten! Maxium size of File is 35kb.
                                </p>
                                <p>
                                    Photo:
                                </p>
                                <input type="hidden" name="size" value="350000">
                                <input type="file" name="photo"> 
                            <p>
                                Please Enter any other information about the band member here.
                            </p>
                            <p>
                                 Other Member Information:
                            </p>
                            <textarea rows="10" cols="35" name="aboutMember">
                            </textarea>
                             <p>
                                 Please Enter any other Bands the Member has been in.
                             </p>
                            <p>
                                     Other Bands:
                            </p>
                                <input type="text" name="otherBands" size=30 />
                            <br/>
                            <br/>
                                <input TYPE="submit" name="upload" title="Add data to the Database" value="Add Member"/>




                        </div>
                        <table class="bordered">
                         <?php
          // fetch all records
          $sql = "SELECT * FROM dbProfiles WHERE 1 "; ?>
          <?php
         $sql="SELECT * FROM dbProfiles";
 $result_set=mysql_query($sql);
 while($row=mysql_fetch_array($result_set))
 {
  ?>
        <tr>
       <td><p><?php echo $row['nameMember'] ?>"</p></td>
        <td><img src="uploads/<?php echo $row['photo'] ?>" width="200px" height="200px;"/><p><?php echo $row['nameMember'] ?>"</p></td>
        </tr>
        <?php
 }
 ?>
 </table>

                    </article>
                </div>

            </div>
            <div class="height10"></div>

    </form>

  </body>
</html>
<?php

function errorMessage($str) {
  return '<div style="width:50%; margin:0 auto; border:2px solid #F00;padding:2px; color:#000; margin-top:10px; text-align:center;">' . $str . '</div>';
}

function successMessage($str) {
  return '<div style="width:50%; margin:0 auto; border:2px solid #06C;padding:2px; color:#000; margin-top:10px; text-align:center;">' . $str . '</div>';
}
?>