Jump to content

[SOLVED] adding username to image


runnerjp

Recommended Posts

Yes, but just to be sure you didn't leave out the other call to mysql_query(), here it is in completeness.

          $sql = "DELETE FROM `user_images` WHERE `user_id`={$Clean['user_id']}";
          $q = mysql_query($sql);
          if($q === FALSE){
            dbg_out('ERROR: ' . mysql_error());
          }else{
            dbg_out('Database delete successful');
          }
          $sql = "
            INSERT INTO `user_images` 
              (`user_id`, `ext`, `created`, `modified`)
            VALUES (
              {$Clean['user_id']}, {$Clean['ext']}, NOW(), NOW()
            )
          ";
          $q = mysql_query($sql);
          if($q === FALSE){
            dbg_out('ERROR: ' . mysql_error());
          }else{
            dbg_out('Database insert successful');
          }

Link to comment
Share on other sites

  • Replies 128
  • Created
  • Last Reply

hey i did it and it doesnt delete the image

Array
(
    [user_id] => 1
    [logged_in] => 1
)

Array
(
    [image] => Array
        (
            [name] => logoo.bmp
            [type] => image/bmp
            [tmp_name] => /tmp/phpkbw3Ja
            [error] => 0
            [size] => 135054
        )

)

image/bmp

/home/runningp/public_html/members/images/1.bmp

SUCCESS!

Database delete successful

Database insert successful 

Link to comment
Share on other sites

Well, if it deletes the old image and then inserts a new one, the record will still be there.  To test it, upload a .gif followed by a .jpg and make sure in the DB the record changes from gif to jpg.  Or just check that the created field has changed.

 

 

Link to comment
Share on other sites

hey i treid this code to show image but gets notting and no error shown :S

 

$sql = "SELECT * FROM user_image WHERE user_id=".$_GET["id"];
$result = mysql_query ($sql, $conn);
if (mysql_num_rows ($result)>0) {
$row = @mysql_fetch_array ($result);
$image_type = $row["ext"];
Header ("Content-type: $image");
print $image;
}
?>

Link to comment
Share on other sites

If it were correct it would be working, which you already said it wasn't.  You're using a variable $image in your header() call but I don't see where you're setting it.

 

Anyways, you don't have to make any calls to header().  The file has the extension attached so the browser will know what to do with it.  All you need to do is look up the record in the table for the appropriate extension.

 

<?php
$sql = 'SELECT `ext` FROM `user_image` WHERE `user_id`=' . $_GET['id'];
$q = mysql_query($sql);
if($q){
  $row = mysql_fetch_array($q);
  if(!empty($row)){
    echo '<img src="http://www.yourdomain.com/members/images/'
         . $_GET['id'] . '.' . $row['ext']
         . '" />';
  }
}else{
  echo 'Error: Image not found.';
}
?>

Link to comment
Share on other sites

Try echo'ing the SQL statement and running it directly in phpMyAdmin.  Try echo'ing mysql_error().

 

At some point you need to learn how to debug your application.  Other than copying and pasting what I put, running it once, and then coming back to say it doesn't work, what have you done to try and troubleshoot it yourself?

Link to comment
Share on other sites

mesingarround i got #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '<?php

$sql = 'SELECT `ext` FROM `user_image` WHERE `user_id`=' . $_GET['id']' at line

Link to comment
Share on other sites

That error message is telling you that this is your query:

'<?php
$sql = 'SELECT `ext` FROM `user_image` WHERE `user_id`=' . $_GET['id']'

 

Whatever you are doing, mysql is running something like this:

mysql_query("<?php\n$sql = 'SELECT `ext` ...");

which is surely wrong.

 

So if this code is in a single file that you are referring to in your browser, yah it should work.  But if you are including() or requiring() that code I gave you, that could explain it.

Link to comment
Share on other sites

no single file like so...

<?php
  
  session_start();
  require_once '../settings.php';

  // see if a name was passed via the url.
  if (isset($_GET['username'])) {
    $username = mysql_real_escape_string($_GET['username']);
  } elseif (isset($_SESSION['username'])) {
    $username = $_SESSION['user_id'];
  } else {
    die("No profile to search for");
  }
  
  $query = "SELECT * FROM users WHERE Username = '$username' LIMIT 1"; 
  if ($result = mysql_query($query)) {
    if (mysql_num_rows($result)) { 
      $array = mysql_fetch_assoc($result);
      $pemail = $array['Email'];
      $puser = $array['Username'];
    } else {
      echo "No users found with id $id<br />";
    }
  } else {
    echo "Query failed<br />$sql<br />" . mysql_error();
  }

?>
<?php $sql = 'SELECT `ext` FROM `user_image` WHERE `user_id`=' . $_GET['id'];


$q = mysql_query($sql);
if($q){
  $row = mysql_fetch_array($q);
  
	 echo 'emma';
  if(!empty($row)){
    echo '<img src="http://www.runningprofiles.com/members/images/'
         . $_GET['id'] . '.' . $row['ext']
         . '" />';
  }
}else{
  echo 'Error: Image not found.';
}
?>

so it should work

Link to comment
Share on other sites

What does this print:

<?php
  
  session_start();
  require_once '../settings.php';

  // see if a name was passed via the url.
  if (isset($_GET['username'])) {
    $username = mysql_real_escape_string($_GET['username']);
  } elseif (isset($_SESSION['username'])) {
    $username = $_SESSION['user_id'];
  } else {
    die("No profile to search for");
  }
  
  $query = "SELECT * FROM users WHERE Username = '$username' LIMIT 1"; 
  if ($result = mysql_query($query)) {
    if (mysql_num_rows($result)) { 
      $array = mysql_fetch_assoc($result);
      $pemail = $array['Email'];
      $puser = $array['Username'];
    } else {
      echo "No users found with id $id<br />";
    }
  } else {
    echo "Query failed<br />$sql<br />" . mysql_error();
  }

echo '<pre style="text-align: left;">id: '  . $_GET['id'] . '</pre>';
$sql = 'SELECT `ext` FROM `user_image` WHERE `user_id`=' . $_GET['id'];
$q = mysql_query($sql);
echo '<pre style="text-align: left;">sql: '  . $sql . '</pre>';
echo '<pre style="text-align: left;">q: '  . $q . '</pre>';
echo '<pre style="text-align: left;">error: '  . mysql_error() . '</pre>';
if($q){
  $row = mysql_fetch_array($q);
  
	 echo 'emma';
  if(!empty($row)){
    echo '<img src="http://www.runningprofiles.com/members/images/'
         . $_GET['id'] . '.' . $row['ext']
         . '" />';
  }
}else{
  echo 'Error: Image not found.';
}
?>

Link to comment
Share on other sites

id:

 

sql: SELECT `ext` FROM `user_image` WHERE `user_id`=

 

q:

 

error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

 

 

why is it not getting the id??

Link to comment
Share on other sites

It won't work because the URL looks like this:

http://www.runningprofiles.com/members/test

 

Where if you're going to use $_GET it needs to look like this:

http://www.runningprofiles.com/members/test?id=<users_number_id>

 

So either you change the URL or you change the script to not use $_GET.  I recommend changing the script to not use $_GET since you are already passing the username into it.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.