duanlun4411 2011-10-16 12:19
浏览 70

将上传图片的位置路径插入用户的pic_location

How do I INSERT the location path of the uploaded picture to the user's pic_location in the database?

So I managed to upload the picture via php to a folder BUT my problem now is that and I don't know how the query will look if I want to achieve that...

The member is logged in when he uploads the pic so the SESSION is running.

I will give you my code to have a look see and if you can spot what to do...

Thanks

My picUpload.php (Where I uploaded the picture and where I want to INSERT the location path into the user's pic_loaction column in the table users)

    <?php
         include 'connect.php';
         include 'header.php';

         if(isset($_SESSION['signed_in']) && $_SESSION['signed_in'] == true)
        {
             //This is the directory where images will be saved 
             $target=$_SERVER['DOCUMENT_ROOT'] . "/avatars/" . basename( $_FILES['file']['name']); 

             //This gets all the other information from the form 
             $pic_location=($_FILES['file']['name']); 

             //Writes the information to the database
             $sql = "UPDATE users SET pic_location='$target' WHERE user_id=" . $_SESSION['user_id'];

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

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

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

        }
        else
        {
            //nothing
        }
?> 

my connect.php

<?php 
    session_start();
    //connect.php

    $server = 'localhost';
    $username = 'root';
    $password = '';
    $database = 'mydatabase';

    if(!mysql_connect('localhost', 'root', ''))
    {
        exit('Error: could not establish database connection');
    }
    if(!mysql_select_db($database))
    {
        exit('Error: could not select the database');
    }
?>

my header.php

<!DOCTYPE HTML>
    <head>
        <title> ShareLink </title>
        <link rel="stylesheet" href="style.css" type="text/css">

        <script src="jquery-1.6.4.min.js" type="text/javascript" charset="utf-8"></script>
        <script type="text/javascript" src="script.js"></script>

        <!-- Back to Top easing jQuery -->
            <link rel="stylesheet" type="text/css" media="screen,projection" href="css/ui.totop.css" />

        <!-- jquery --> 
        <script src="js/jquery-1.3.2.min.js" type="text/javascript"></script>
        <!-- easing plugin ( optional ) -->
        <script src="js/easing.js" type="text/javascript"></script>
        <!-- UItoTop plugin -->
        <script src="js/jquery.ui.totop.js" type="text/javascript"></script>


        <script type="text/javascript">
            $(document).ready(function() {
                /*
                var defaults = {
                    containerID: 'moccaUItoTop', // fading element id
                    containerHoverClass: 'moccaUIhover', // fading element hover class
                    scrollSpeed: 1200,
                    easingType: 'linear' 
                };
                */

                $().UItoTop({ easingType: 'easeOutQuart' });

            });
        </script>
    </head>
    <body>
    <h1> ShareLink </h1>
        <div id="wrapper">
        <div id="menu">
            <a class="item" href="/index.php">Home</a> -
            <a class="item" href="/create_topic.php">Create a topic</a> -
            <a class="item" href="/create_cat.php">Create a category</a> - 
            <a class="item" href="/members.php"> Members </a> - 
            <a class="item" href="/search_form.php"> Search </a> - 
            <a class="item" href="/profile.php"> Profile </a>


            <div id="userbar">
            <?php
            if($_SESSION['signed_in'])
            {
                echo 'Hello <b>' . htmlentities($_SESSION['user_name']) . '</b>. <a class="item" href="signout.php">Log out</a>';
            }
            else
            {
                print'<a class="item" href="signin.php">Log in</a> or <a class="item" href="signup.php">Register</a>';
            }
            ?></div>
        </div>
            <div id="content">

and here is my users table in my database.sql file

CREATE TABLE users (  
user_id     INT(8) NOT NULL AUTO_INCREMENT,  
user_name   VARCHAR(30) NOT NULL,  
user_pass   VARCHAR(255) NOT NULL,  
user_email  VARCHAR(255) NOT NULL,  
user_date   DATETIME NOT NULL,  
user_level  INT(8) NOT NULL,  
pic_location  VARCHAR(255) NOT NULL,  
UNIQUE INDEX user_name_unique (user_name),  
PRIMARY KEY (user_id)  
);
  • 写回答

2条回答 默认 最新

  • dongshan7708 2011-10-16 12:37
    关注

    store the variable $target as pic_location and the query is not insert it should be update query.

    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法