Connecting Tech Pros Worldwide Forums | Help | Site Map

PHP script

Member
 
Join Date: Sep 2009
Posts: 56
#1: 3 Weeks Ago
May someone help me correct this script? there a few ' and " and ; in the wrong places:


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include "./comm.inc"; 
  3. connectdb(); 
  4. $sql = "SELECT imgid,imgtype FROM tblimage ORDER BY imgid";  
  5.  
  6. $result = @mysql_query($sql) or die(mysql_error());  
  7.  
  8. echo '<table border=1>n';  
  9. echo '<tr><th>imgid</th><th>imgtype</th><th>imgdata</th></tr>n';  
  10. while ($rs=mysql_fetch_array($result)) {  
  11.   echo '<tr><td>.$rs[0].</td>';  
  12.   echo '<td>.$rs[1].</td>';  
  13.   echo '<td><img src="post.php?imgid=.$rs[0]."" width=100 height=100></td></tr>n';  
  14. echo '</table>n';  
  15.  
  16. ?>
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  

Member
 
Join Date: Sep 2009
Posts: 56
#2: 3 Weeks Ago

re: PHP script


I'll post here the other two codes:

post.php -->

Expand|Select|Wrap|Line Numbers
  1. <?  
  2. if (!isset($submit)) {  
  3. ?>  
  4. <form method="POST" action="" enctype=multipart/form-data>  
  5. <table>  
  6. <tr><td>Type</td><td><select name="imgtype"><option value="image/gif">GIF</option><option  
  7. value="image/jpeg">JPEG</option></select></td></tr>  
  8. <tr><td>File</td><td><input type="file" name="imgfile"></td></tr>  
  9. <tr><td></td><td><input type="submit" name="submit" value="upload"><input type="reset" value="reset"></td></tr>  
  10. </table>  
  11. </form>  
  12. <?  
  13. } else {  
  14.         include "./comm.inc"; 
  15.         connectdb(); 
  16.         $hndl=fopen($imgfile,"rb");  
  17.         $imgdata=''; 
  18.         while(!feof($hndl)){ 
  19.                 $imgdata.=fread($hndl,2048);  
  20.         } 
  21.  
  22. {
  23.         $imgdata=addslashes($imgdata);  
  24.  
  25.         $sql = 'INSERT INTO tblimage VALUES(NULL,". $imgtype .",". $imgdata .")';  
  26.  
  27.         @mysql_query($sql) or die(mysql_error());  
  28.  
  29.         fclose($hndl);  
  30.  
  31.         echo '<a href="view.php">view image</a>';  
  32. }  
  33. ?> 
  34.  
  35.  


And




upload.php -->





Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if (!isset($submit)) {
  3. ?>
  4. <form method="POST" action="" enctype=multipart/form-data>
  5. <table>
  6. <tr><td>Tipo</td><td><select name="imgtype"><option value="image/gif">GIF</option><option
  7. value="image/jpeg">JPEG</option></select></td></tr>
  8. <tr><td>File</td><td><input type="file" name="imgfile"></td></tr>
  9. <tr><td></td><td><input type="submit" name="submit" value="upload"><input type="reset" value="reset"></td></tr>
  10. </table>
  11. </form>
  12. <?
  13. } else {
  14.         include "./comm.inc";
  15.         connectdb();
  16.         $hndl=fopen($imgfile,"rb");
  17.         $imgdata='';
  18.         while(!feof($hndl)){
  19.                 $imgdata.=fread($hndl,2048);
  20.         }
  21.  
  22.         $imgdata=addslashes($imgdata);
  23.  
  24.         $sql = 'INSERT INTO tblimage VALUES(NULL,", $imgtype .",". $imgdata .")';
  25.  
  26.         @mysql_query($sql) or die(mysql_error());
  27.  
  28.         fclose($hndl);
  29.         echo '<a href="view.php">view image</a>';
  30. }
  31. ?>
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,083
#3: 3 Weeks Ago

re: PHP script


This is a rather tiresome task you have set.
Do you think it is fair to expect people to trail through your code looking for syntax errors.
If you put some debugging in your code you might help yourself to find the problem
Member
 
Join Date: Sep 2009
Posts: 56
#4: 3 Weeks Ago

re: PHP script


sorry, I just though....
Member
 
Join Date: Sep 2009
Posts: 56
#5: 3 Weeks Ago

re: PHP script


whell, I fexed a little things in view.php, and upload.php is correct, maybe the trouble is just in post, and dont get any error using the scripts, that's the problem but the image doesnt appear when I open view.php
Member
 
Join Date: Sep 2009
Posts: 56
#6: 3 Weeks Ago

re: PHP script


I'll post a photo here
Member
 
Join Date: Sep 2009
Posts: 56
#7: 3 Weeks Ago

re: PHP script


This is how it appears on the browser:






And this is how I left view.php:


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include "./comm.inc"; 
  3. connectdb(); 
  4. $sql = "SELECT imgid,imgtype FROM tblimage ORDER BY imgid";  
  5.  
  6. $result = @mysql_query($sql) or die(mysql_error());  
  7. {
  8. echo '<table border=1>n';  
  9. echo '<tr><th>imgid</th><th>imgtype</th><th>imgdata</th></tr>n';  
  10. while ($rs=mysql_fetch_array($result))
  11.  
  12.   echo '<tr><td>".$rs[0]."</td>';  
  13.   echo '<td>".$rs[1]."</td>';  
  14.   echo '<td><img src="post.php?imgid=.$rs[0]." width=100 height=100></td></tr>n';  
  15. echo '</table>n';
  16.  
  17.  
  18. }
  19.  
  20. ?>
Member
 
Join Date: Sep 2009
Posts: 56
#8: 3 Weeks Ago

re: PHP script


True is I'm not getting any bug anymore, but, it just doesn't appears.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,948
#9: 3 Weeks Ago

re: PHP script


Variables are not parsed within single quoted strings. You'll have to concatenate the string or use double quotes:

Expand|Select|Wrap|Line Numbers
  1. $name = "Mark";
  2. // Works
  3. echo "My name is $name";
  4. // Also works
  5. echo 'My name is ' . $name;
  6. // Doesn't work: prints "My name is $name"
  7. echo 'My name is $name';
  8.  
Also, in future please provide all error details. Simply asking us to 'look at' something and deduce what is wrong with it simply isn't the proper way to ask a question to a bunch of volunteers.

Mark.
Member
 
Join Date: Sep 2009
Posts: 56
#10: 3 Weeks Ago

re: PHP script


Quote:

Originally Posted by Markus View Post

Variables are not parsed within single quoted strings. You'll have to concatenate the string or use double quotes:

Expand|Select|Wrap|Line Numbers
  1. $name = "Mark";
  2. // Works
  3. echo "My name is $name";
  4. // Also works
  5. echo 'My name is ' . $name;
  6. // Doesn't work: prints "My name is $name"
  7. echo 'My name is $name';
  8.  
Also, in future please provide all error details. Simply asking us to 'look at' something and deduce what is wrong with it simply isn't the proper way to ask a question to a bunch of volunteers.

Mark.



Do I have to do that to view.php, post.php, upload.php, or both?
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,948
#11: 3 Weeks Ago

re: PHP script


Quote:

Originally Posted by matheussousuke View Post

Do I have to do that to view.php, post.php, upload.php, or both?

Wherever you are outputting strings (and they are not showing correctly).
Member
 
Join Date: Sep 2009
Posts: 56
#12: 3 Weeks Ago

re: PHP script


Well, true is, I found that script on forum, dont remember witch, had to correct a lot of things, it was worst than that.


Let's go to the point, all I wanted was a simple script that could upload a image to mysql and show it on page. And this script "does" all of this.


So if someone could give a script with that simple task, I would be really greatful, I'm not being rude, I'm tellgin that cause I spent more than 4 hours looking on google for a script that could do what I need.

The goal is put a option on my site where the user can be able to manage the index logo diretly from the admin area by using image upload function.
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,083
#13: 2 Weeks Ago

re: PHP script


What Markus is referring to is lines such as this
Expand|Select|Wrap|Line Numbers
  1. echo '<td><img src="post.php?imgid=.$rs[0]." 
  2. width=100 height=100></td></tr>n'; 
You have wrapped the whole line in single quotes so the variable inside will not be parsed.
In fact the variable is concatenated but the quotes are not opened or closed
This is better
Expand|Select|Wrap|Line Numbers
  1. echo '<td><img src="post.php?imgid='.$rs[0].'" 
  2. width=100 height=100></td></tr>n'; 
You need to correct all lines such as this
Member
 
Join Date: Sep 2009
Posts: 56
#14: 2 Weeks Ago

re: PHP script


I'm using a image upload script without mysql, it works perfectly, all I want is to show its image on my site index.

The script works like this:

It simple upload a image with the name logo. with extension in front (eg. logo.png).

What I need is a way to show it on index with a code such as img src


eg.:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?
  3. echo '<img src="admin/fotos/logo.   I need a way to put a extension variable here, so, undepeding on extension format, it will always show image file, rembering that the name is aways "logo.".
  4.  
  5. ">';
  6. ?>
  7.  
  8.  
  9.  
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,083
#15: 2 Weeks Ago

re: PHP script


It has been explained to you what you are doing wrong in your thread PHP script
Familiar Sight
 
Join Date: Mar 2007
Posts: 172
#16: 2 Weeks Ago

re: PHP script


Have u checked that logo extension got coorectly for example logo.png. If you echo any values using single code, for example
Expand|Select|Wrap|Line Numbers
  1. <?
  2.  echo '<img src="admin/fotos/logo.".$ext."">
  3. ?>
  4.  
  5. then you must add ". ." in variables.
  6.  
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,948
#17: 2 Weeks Ago

re: PHP script


Quote:

Originally Posted by maheswaran View Post

Have u checked that logo extension got coorectly for example logo.png. If you echo any values using single code, for example

Expand|Select|Wrap|Line Numbers
  1. <?
  2.  echo '<img src="admin/fotos/logo.".$ext."">
  3. ?>
  4.  
  5. then you must add ". ." in variables.
  6.  

That won't work - and will definitely throw an error.

The correct PHP would look like:
Expand|Select|Wrap|Line Numbers
  1. <?
  2.  echo "<img src='admin/fotos/logo" . $ext . "'>";
  3. ?>
  4.  
Furthermore, please do not double post your questions.
Familiar Sight
 
Join Date: Mar 2007
Posts: 172
#18: 2 Weeks Ago

re: PHP script


i think your code is wrong
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,948
#19: 2 Weeks Ago

re: PHP script


Quote:

Originally Posted by maheswaran View Post

i think your code is wrong

Would you care to clarify?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,658
#20: 2 Weeks Ago

re: PHP script


the dot between file name and file extension is missing. there is also the required alt attribute missing.
Familiar Sight
 
Join Date: Mar 2007
Posts: 172
#21: 2 Weeks Ago

re: PHP script


while using "" there is no need to add ". ." to declearing values
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,658
#22: 2 Weeks Ago

re: PHP script


Quote:

Originally Posted by maheswaran View Post

while using "" there is no need to add ". ." to declearing values

that’s a matter of personal preference and not an error. besides you see the variables way better in highlighted source code. not sure you can do that with function call, too.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,948
#23: 2 Weeks Ago

re: PHP script


Quote:

Originally Posted by maheswaran View Post

while using "" there is no need to add ". ." to declearing values

This does not make my code 'wrong'. Also, you are not declaring variables when you concatenate them into strings.

Mark.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,948
#24: 2 Weeks Ago

re: PHP script


Quote:

Originally Posted by Dormilich View Post

the dot between file name and file extension is missing. there is also the required alt attribute missing.

Come on...
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,658
#25: 2 Weeks Ago

re: PHP script


Quote:

Originally Posted by Markus View Post

Come on...

guess how many people don’t know that?
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,948
#26: 2 Weeks Ago

re: PHP script


Quote:

Originally Posted by Dormilich View Post

guess how many people don’t know that?

That doesn't make my PHP wrong, which is what I was questioning ;)
Member
 
Join Date: Sep 2009
Posts: 56
#27: 2 Weeks Ago

re: PHP script


Thanks a lot, guys, but I caught another one. The one one who made this one (of this post) must had make it just for making people angry. I now declare this post closed. Thank you very much.
Reply

Tags
image upload