SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2006
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    inserting data into a users row in mysql

    Hi Guys,

    i'm not sure if this is possible but this is what i'm attempting to do:

    1)I Have made a registration page for a user to fill in and submit <-- ok

    2)Once they do they can login to thier account <-- ok

    3)the minute they are in thier account they can upload a photo/avatar and it submits the path/filename e.g to the database "grahamspicture.jpg" for example.

    but what i'm not sure about is HOW to post that logged in users data back into the mysql database, once that particular user logs in how to make mysql know to post the path in grahams mysql row under photo (i hope that made sense)

    heres a basic mock up of my_account.php

    PHP Code:
    <?php
         
    // Start the session.../////////////////////////////////////////////////////////////
         
    session_start();
         
         
    // Register the session...//////////////////////////////////////////////////////////
         
    if(!session_is_registered("username")) {
         
    header("location:login.php");
         }

         
    //Includes... //////////////////////////////////////////////////////////////////////
         
    include("includes/db_connection.php");
         include(
    "includes/constants.php");
         include(
    "includes/header.php");
         include(
    "includes/loginnav.php");
    ?>
    <p align="left">Welcome, <b><font color="red"><? echo $_SESSION['username']; ?></font></b>! (<a href="logout.php">Logout</a>)</p>
    <form enctype="multipart/form-data" action="<?php echo $PHP_SELF?>" method="POST"> 
    Photo: <input type="file" name="photo"><br> 
    <input type="submit" value="Add"> 
    </form>
    <?php
      
    //This is the directory where images will be saved
      
    $target "members/";
      
    $target $target basename$_FILES['photo']['name']);

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

      
    // Connects to your Database  
      
    mysql_connect("localhost""root""milkybar") or die(mysql_error());
      
    mysql_select_db("cdm") or die(mysql_error());

      
    //Writes the information to the database  
      
    mysql_query("INSERT INTO `membership` VALUES ('$pic')");

      
    //Writes the photo to the server
      
    if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))  {  
     
      
    //Tells you if its all ok
      
    echo "The file "basename$_FILES['uploadedfile']['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.";  
    }  
    ?> 
    <?php 

         
    include("includes/footer.php"
    ?>
    Thanks guys

    Graham

  2. #2
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is what I have created for my portal, you may change the code as you wish
    pictures.sql
    Code:
    CREATE TABLE `pictures` (
      `id` int(11) NOT NULL auto_increment,
      `picture` varchar(50) default NULL,
      `username` varchar(50) default NULL,
      PRIMARY KEY  (`id`)
    );
    up.php
    PHP Code:
    <?php
    header
    ("Content-type: text/html");
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    header("Last-Modified: " gmdate("D, d M Y H:i:s") . " GMT");
    header("Cache-Control: no-cache, must-revalidate");
    header("Pragma: no-cache"); 
    echo 
    "<?xml version=\"1.0\"?>\n"
    echo 
    "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
        \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
    <html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
    ;
    ?>
    <head>
    <title>Untitled Document</title>
    <script type="text/javascript"><!--
    function add_file(id, i) {
    if (document.getElementById(id + '_' + i).innerHTML.search('uploadinputbutton') == -1) {
    document.getElementById(id + '_' + i).innerHTML = '<input type="file" class="uploadinputbutton" maxsize="" name="' + id + '[]" onchange="return add_file(\'' + id + '\', ' + (i+1) + ');" /><br /><span id="' + id + '_' + (i+1) + '"><input type="button" value="Add other" onclick="add_file(\'' + id + '\', ' + (i+1) + ');" /><\/span>\n';
    }
    }
    //--></script>
    </head>
    <body>
    <form name="form" enctype="multipart/form-data" method="post" action="upload.php">
    <table border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td style="vertical-align: top; width: 50%;">
                <div class="header">Files</div><div style="font-size: 90%;">Files entered here will be transferred to the FTP server.</div><br />
            </td>
        </tr>
        <tr>
            <td style="vertical-align: top; width: 50%;">
                <input type="file" class="uploadinputbutton" name="file[]" onchange="add_file('file', 1);" /><br />
                <span id="file_1"><input type="button" value="Add another" onclick="add_file('file', 1);" /></span><br />
            </td>
        </tr>
    </table>
    <input type="submit" name="submit" value="submit" />
    </form>
    </body>
    </html>
    upload.php
    PHP Code:
    <?php
    session_start
    ();
    $dbhost 'localhost';
    $dbuser 'root'
    $dbpass 'milkybar';    
    $dbname 'cdm';
    $db mysql_connect($dbhost$dbuser$dbpass) or die(mysql_error());
    mysql_select_db($dbname);
    header("Content-type: text/html");
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    header("Last-Modified: " gmdate("D, d M Y H:i:s") . " GMT");
    header("Cache-Control: no-cache, must-revalidate");
    header("Pragma: no-cache"); 
    echo 
    "<?xml version=\"1.0\"?>\n"
    echo 
    "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
        \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
    <html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
    ;
    ?>
    <head>
    <title>picture upload</title>
    </head>
    <body>
    <?php
    if(isset($_POST['submit'])){
    foreach(
    $_FILES['file']['error'] as $key => $error){
      if(
    $error == UPLOAD_ERR_OK){
      
    $img "members/".$_FILES['file']['name'][$key];
      
    $t=1;
      while(
    file_exists($img)){
      
    $img "members/".$_FILES['file']['name'][$key];
      
    $img=substr($img,0,strpos($img,"."))."-$t".strstr($img,".");
      
    $t++;
      }
      
    mysql_query("INSERT INTO pictures SET picture='$img' , username='".$_SESSION['username']."'") or die(mysql_error());
      
    $tmp_name $_FILES['file']['tmp_name'][$key];
      
    move_uploaded_file($tmp_name$img);
      echo 
    $_FILES['file']['name'][$key]." uploaded successfully<br />\n";
      }
    }
    }
    ?>
    </body>
    </html>
    If you have any questions just ask. I would be happy to help!
    my mobile portal
    ghiris.ro

  3. #3
    SitePoint Evangelist
    Join Date
    Dec 2006
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Ernie,

    Thank you very much for the code there, i have tested on my site and it's working perfectly

    the only other thing is, i have a "profile.php" page where all the users data is queried from the "membership" table on signup and echoed out to the page, because the "pictures" table is in another row is there a way when someone views:

    profile.php?id=71 <-- for example

    not only the membership tables data echoed out but the users picture which is now in the "pictures" table of that specific user?

    hope that made sense lol

    thanks again mate

    Graham

  4. #4
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    put the user_id in session, when the user uploads the picture the user_id will be there.
    PHP Code:
    mysql_query("INSERT INTO pictures SET picture='$img' , username='".$_SESSION['user_id']."'") or die(mysql_error()); 
    in profile.php
    PHP Code:
    include("opendb.php");
    $id=isset($_GET['id']) ? (int) $_GET['id'] : false;
    $query=mysql_query("SELECT pictures.user_id, pictures.directory, users.id
      FROM pictures
       INNER JOIN users ON users.id = pictures.user_id
        WHERE users.id =$id");
    $row=mysql_fetch_array($query);
    <img src="<?php echo $row['directory']; ?>" alt="<?php echo $row['id']; ?>"/><br/>
    Last edited by Ernie1; Mar 31, 2007 at 06:22.
    my mobile portal
    ghiris.ro

  5. #5
    SitePoint Evangelist
    Join Date
    Dec 2006
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey mate,

    Thanks a lot for the help mate it did indeed help a lot, i have a better understanding now

    cheers mate

    Graham


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •