not working code, please help!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

elenait
Forum Newbie
Posts: 21
Joined: Thu Dec 09, 2010 8:23 am

Re: not working code, please help!

Post by elenait »

Hello, again !
I made what you suggested
:

Code: Select all

$row = mysql_fetch_array($result); if ($row) {echo "row";}
  while ($row = mysql_fetch_array($result)) {
  $mail = $row['mail'] ;}
  ?>
<p>
  <form action='<?=$_SERVER['PHP_SELF']; ?>' method="post">
    Email: <input type="text" name='mail' 
             value='<?php echo $mail; ?>'><br>
-----------------------------------------------------------
what I found is $row = mysql_fetch_array($result); doesn't work. I checked with : if ($row) {echo "row";}, and no" row" is there ...
Last edited by Benjamin on Tue Dec 28, 2010 3:49 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: not working code, please help!

Post by social_experiment »

elenait wrote:what I found is $row = mysql_fetch_array($result); doesn't work. I checked with : if ($row) {echo "row";}, and no" row" is there ...
It's possible that there is a problem with the query ($result). Do you receive an error from mysql_error()?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
elenait
Forum Newbie
Posts: 21
Joined: Thu Dec 09, 2010 8:23 am

Re: not working code, please help!

Post by elenait »

I don't have an $result error , even I try with if ($result) {echo "result";} and there is "result"
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: not working code, please help!

Post by social_experiment »

Code: Select all

<?php
 // $row does exist in this instance so there will be "row"
 // echoed to the browser.
 // remove the first '$row'
 $row = mysql_fetch_array($result); if ($row) {echo "row";}
 while ($row = mysql_fetch_array($result)) {
 $mail = $row['mail'] ;}
?>
Remove the first line of the code an try again. When this piece of script is called, you assign the array with data you want (mysql_fetch_array($result)) to the variable $row. The problem comes when you AGAIN assign $row to another mysql_fetch_array. Try substituting the following:

Code: Select all

<?php
 while ($array = mysql_fetch_array($result)) {
  $mail = array['mail'];
} ?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
elenait
Forum Newbie
Posts: 21
Joined: Thu Dec 09, 2010 8:23 am

Re: not working code, please help!

Post by elenait »

I did it, but again- no result :oops:
elenait
Forum Newbie
Posts: 21
Joined: Thu Dec 09, 2010 8:23 am

need an upload image to folder php code

Post by elenait »

Hello!
I need a working php code to upload images to folder, and later to display them and no mather the file extension every pic to be transformed in .jpg. If it is possible I prefer not to input an image name into input form, only the image file.
I have a mysql table :

Code: Select all

CREATE TABLE IF NOT EXISTS images (
        id INT(11) NOT NULL AUTO_INCREMENT,
        user VARCHAR(255) NOT NULL,
        pic_name VARCHAR(255) NOT NULL,
        
        PRIMARY KEY (id)
        )
and i tried few things but something went wrong..

Thank you in advance!
Last edited by Benjamin on Tue Dec 28, 2010 3:50 am, edited 1 time in total.
Reason: Added [syntax=sql] tags.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: not working code, please help!

Post by social_experiment »

Do you have any existing file upload code created?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
elenait
Forum Newbie
Posts: 21
Joined: Thu Dec 09, 2010 8:23 am

Re: not working code, please help!

Post by elenait »

I have some, but I still don't know how to modify it. I want images names to be the same in the folder and in the mysql table and to ble like this : image_dir/imagename.id.jpg. Now in the table there is only image_caption which the user put, and in the folder there is the id like image_name (1, 2,3 ...)

this is the table :

Code: Select all

CREATE TABLE IF NOT EXISTS images (
        image_id INT(11) NOT NULL AUTO_INCREMENT,
        image_caption VARCHAR(255) NOT NULL,
        username VARCHAR(255) NOT NULL,
        image_date DATE NOT NULL,
        PRIMARY KEY (image_id)
file:

Code: Select all

<? ob_start(); //Its turning on the output buffer. So any output is kept in the buffer. And ob_flush() is to flush the buffer ?><?php
session_start();

$username = $_SESSION['username'];
echo $username;
include "config.php";
//make variables available
if (isset($_POST['Submit'])) {
$image_caption = $_POST['image_caption'];
//$username = $_POST['user'];
$image_tempname = $_FILES['image_filename']['name'];
 date_default_timezone_set('Europe/Helsinki');
$today = date("Y-m-d");

//upload image and check for image type
//make sure to change your path to match your images directory
$ImageDir ="img_user/";

$ImageThumb = $ImageDir . "thumbs/";// za syzdavane na miniaturi
$ImageName = $ImageDir . $image_tempname;

if (move_uploaded_file($_FILES['image_filename']['tmp_name'], 
                       $ImageName)) {

  //get info about the image being uploaded
  list($width, $height, $type, $attr) = getimagesize($ImageName);

  switch ($type) {
    case 1:
      $ext = ".gif";
      break;
    case 2:
      $ext = ".jpg";
      break;
    case 3:
      $ext = ".png";
      break;
    default:
      echo "Sorry, but the file you uploaded was not a GIF, JPG, or " .
           "PNG file.<br>";
      echo "Please hit your browser's 'back' button and try again.";
  } 
  if ($type > 3) {
    echo "Sorry, but the file you uploaded was not a GIF, JPG, or " .
         "PNG file.<br>";
    echo "Please hit your browser's 'back' button and try again.";
  } else {


   //insert info into image table

  $insert = "INSERT INTO images
            (image_caption, username, image_date)
            VALUES
            ('$image_caption', '$username', '$today')";
  $insertresults = mysql_query($insert)
    or die(mysql_error());

  $lastpicid = mysql_insert_id();

  $newfilename = $ImageDir . $lastpicid . $ext;

   if ($type == 2) {
    rename($ImageName, $newfilename);
  } else {
    if ($type == 1) {
      $image_old = imagecreatefromgif($ImageName);
    } elseif ($type == 3) {
      $image_old = imagecreatefrompng($ImageName);
    }

    //"convert" the image to jpg
    $image_jpg = imagecreatetruecolor($width, $height);
    imagecopyresampled($image_jpg, $image_old, 0, 0, 0, 0, 
                     $width, $height, $width, $height);
    imagejpeg($image_jpg, $newfilename);
    imagedestroy($image_old);
    imagedestroy($image_jpg);
  }
  
  
  
   $newthumbname = $ImageThumb . $lastpicid . ".jpg";

  //get the dimensions for the thumbnail
  $thumb_width = $width * 0.10;
  $thumb_height = $height * 0.10;

  //create the thumbnail
  $largeimage = imagecreatefromjpeg($newfilename);
  $thumb = imagecreatetruecolor($thumb_width, $thumb_height);
  imagecopyresampled($thumb, $largeimage, 0, 0, 0, 0, 
                    $thumb_width, $thumb_height, $width, $height);
  imagejpeg($thumb, $newthumbname);
  imagedestroy($largeimage);
  imagedestroy($thumb);}

}
}else{
?>
<html>
<head>
<title>Upload your pic to our site!</title>
</head>
<body>

<form name="form1" method="post" action='<?=$_SERVER['PHP_SELF']?>'
    enctype="multipart/form-data">

<table border="0" cellpadding="5">
  <tr>
    <td>Image Title or Caption<br>
      <em>Example: You talkin' to me?</em></td>
    <td><input name="image_caption" type="text" id="image_caption" size="55" 
          maxlength="255"></td>
  </tr>
  <tr>
  
  </tr>
    <td>Upload Image:</td>
    <td><input name="image_filename" type="file" id="image_filename"></td>
  </tr>
</table>
<br>
<em>Acceptable image formats include: GIF, JPG/JPEG, and PNG.</em>
<p align="center"><input type="submit" name="Submit" value="Submit">
  &nbsp;
  <input type="reset" name="Submit2" value="Clear Form">
</p>
</form>
</body>
</html><?php }?><? ob_flush(); ?>
Last edited by Benjamin on Tue Dec 28, 2010 3:50 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: not working code, please help!

Post by social_experiment »

Code: Select all

<input type="hidden" name="MAX_FILE_SIZE" value="102400" />
Your html form needs a hidden field that will help to enforce limits on the files uploaded. There are quite a few other issues with file upload and security that you have to take into account before letting users upload files to your webserver.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
elenait
Forum Newbie
Posts: 21
Joined: Thu Dec 09, 2010 8:23 am

Re: not working code, please help!

Post by elenait »

I foud that if tre user type one name for 2 pics, the names are in the table, but in the folder is only one pic
elenait
Forum Newbie
Posts: 21
Joined: Thu Dec 09, 2010 8:23 am

How to make separate page for each item automaticaly?

Post by elenait »

Hello, everybody!
Now I am working on a web portal.
A lot of users will be registered in it, and they will have their own profile.
I would like to make personal page with their data for each one automatically and when someone want to see them to go to this page. It is like online shop where every product has more description on a separate page. I hope you understand me and you could help me how to make it. If you have any questions please don't hesitate to ask me.
Thank you in advance!
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: not working code, please help!

Post by social_experiment »

elenait wrote:I would like to make personal page with their data for each one automatically and when someone want to see them to go to this page.
You take a value that is unique to each user (UserId, etc) and then pass the value along in a query string perhaps? Like your name clicked here would bring up your profile memberlist.php?mode=viewprofile&u=177042 <-- the part in the query string that is relevant is the value of u which is 177042. Hth.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
elenait
Forum Newbie
Posts: 21
Joined: Thu Dec 09, 2010 8:23 am

Re: not working code, please help!

Post by elenait »

Thank you! I was thinking about something like this, but as I am very new, I don't know how exactly to put the value in the url and the rest...
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: not working code, please help!

Post by social_experiment »

elenait wrote:...I don't know how exactly to put the value in the url and the rest...
Lets assume you will have a list of all the users and one of the entries will have the following url : yoursite/individual.php?u=20
To create the link you have to cycle through all the users and place each of their id's (u) in a <a> tag

Code: Select all

<?php
 // other code above
 while ($userArray = mysql_fetch_array($sql)) {
  echo '<a href="page?u='. $userArray['id'] .'">Link Name</a>';
  // repeats for all users
 }
?>
When the page is clicked, the value of u becomes available in $_GET['u'].

Code: Select all

<?php
 // after checking that $_GET['u'] is set and the value 
 // is safe use it
 $userInfoQuery = mysql_query("SELECT * FROM userTable WHERE u = '". mysql_real_escape_string($_GET['u']) ."' ");
 // from here it's a simple exercise to display the information
 // about each user.
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
elenait
Forum Newbie
Posts: 21
Joined: Thu Dec 09, 2010 8:23 am

Re: not working code, please help!

Post by elenait »

Thank you for the help!
I made it but without "mysql_escape_string".
It works! :D
Post Reply