Jump to content

PHP & Mysql Help - Newbie!


jfs0479

Recommended Posts

Hi,

Im new to php and mysql and finding it quite intresting so far but i have managed to add a record to a database table like details but i was wondering how i put in am image in by an upload field. Ive heard something about using a blob but im not sure how i do that!

Thanks

James
Link to comment
Share on other sites

Hi,

A BLOB stands for a binary large object, blob columns are binary strings (byte strings).
If you want to put picture in db do following>

1.in form use input type="file" - browse for it, submit it (POST upload to server), than use $_FILES[] superglobal to restore information about uploaded picture,
2. use fopen,fread to read contents of file (picture) to some variable, than strip slashes on this variable (prior to saving it to db)
3. insert it to DB to field which has type BLOB or LONGBLOB ( it depends on size of binary file ).

and if you want to view it, read it from db(field) to variable, apply add slashes() function and display it as an output from db ( I mean - eg. <img src="display_image.php?id=11" /> - it calls display_image.php script which has only one purpose to read image no. 11 from DB table and than you have to display it with print for e.g. - look at phpfreaks.com, I am sure that there is at least one tutorial on creating simple image gallery :-)

note. Saving images (especially large ones e.g. photos) to db is not considered best practice of using DB&Images. Usually DB serves for only saving reference (server path) to real image files uploaded on server, and images are loaded directly from disk.

Hope this helps you a bit, at least like basic points around a way:-)
jd.
Link to comment
Share on other sites

Great yea thanks. I think im with you on the last bit!. I think an alternative option that i could do was to say to the people where is the file located on the web like www.example.com/image.jpg or whteva and then that gets sent to the database by like a varchar and then for like a profile image i can do <img src="$imageurl" /> and the $imageurl is the variable taken from the database.

Also i have this system going at www.hindustanva.com/routes/display.php and it all php mysql made i was wondering how i could just view one record in the database by using the url? ive tried things like http://hindustan.yourourdesign.com/routes/display.php?HS001 but that chucks up all of the records again which i dont really want. Do you know how to change this?

Thanks

James
Link to comment
Share on other sites

Hi,

as for your first part>

[size=8pt]Great yea thanks. I think im with you on the last bit!. I think an alternative option that i could do was to say to the people where is the file located on the web like www.example.com/image.jpg or whteva and then that gets sent to the database by like a varchar and then for like a profile image i can do <img src="$imageurl" /> and the $imageurl is the variable taken from the database.
[/size]

That's correct. It should work perfectly.

as for second part>

[size=8pt]Also i have this system going at www.hindustanva.com/routes/display.php and it all php mysql made i was wondering how i could just view one record in the database by using the url? ive tried things like http://hindustan.yourourdesign.com/routes/display.php?HS001 but that chucks up all of the records again which i dont really want. Do you know how to change this?[/size]

It is done using simple select statement. As I saw HS001 is Route_ID in your table so if you want to make url that select just one route you may do>

1.your url has to have following format http://hindustan.yourourdesign.com/routes/display.php?[b]id=HS001[/b] to GET to work correctly ([b]not just[/b] display.php?something, because you have to tell server in what variable to store value "HS001" in). So for e.g. in your display.php make all Route_ID links active as follows>

(select all route_ids from your table to resultset )

foreach (... as $routeID ){
---suppose you have single Route_ID in $routeID php variable>
[b]---make href: echo '<a href="http:/hindustan.yourourdesign.com/routes/display.php?id='.$routeID.'">';[/b]
---echo '...display all other info from DB table as you do in your list page (display.php)';
}

2. Than all Route_IDs will be active. And add to display.php(beggining) code where you read $_GET['id'] check it if isset($_GET['id']) and if so (user clicked on link with concrete $routeID ) run query like>

SELECT * FROM yourtable WHERE Route_ID='.$_GET['id'].';

the resultset should now be just one selected ROW in your table. So now go ahead and display this single row info on page.

note. It is advised not to use $_GET,$_POST etc. directly in script, because of security issues. More safe is assign $_GET values to local variable ($local_variable = $_GET['id']) and than use to script logic.

note. It has not to be id="value" all the time, you may use anything else instead of "id", so if you want to ...display.php?use anything_you_want=HS001 you can :-) the only things which are mandatory for $_GET(variables passed by url)  to work is to have script.php + ? sign + identifier + = + value

note. The script logic I mention above - I mean something like this>

*display.php*

<? if (isset($_GET['id'])) {
        ...User clicked on active link, and wants to view single record
          identified by id. So read id variable and perform select where Record_ID=$_GET['id']
          and do things in step 2.
  }else{
        ...Otherwise display list of the records from table, part wchich you have just done :-)
  }

Hope this helps :-)
Link to comment
Share on other sites

Hi, Thanks for your reply. I kind of understand it but im confused really. Please could you help me with it. The code is posted below

[quote]<?php
mysql_connect("localhost", "username", "password");
$result = mysql_query(
"select * from databasename.routes ORDER BY RID asc");?><html><title>Our Routes</title>
<style type="text/css">
<!--
.style6 {font-family: "century Gothic"; font-size: 12px; }
.style8 {color: #C70391}
-->
</style>
<body>

<table width="100%" border="0" cellpadding="0" cellspacing="0" class="style6">
  <tr>
    <td width="10%" bgcolor="#F3F3F3"><u>Route ID </u></td>
    <td width="20%" bgcolor="#F3F3F3"><u>Departing Airport </u></td>
    <td width="23%" bgcolor="#F3F3F3"><u>Arriving Airport </u></td>
    <td width="16%" bgcolor="#F3F3F3"><u>Departing Time </u></td>
    <td width="13%" bgcolor="#F3F3F3"><u>Distance (km) </u></td>
    <td width="8%" bgcolor="#F3F3F3"><u>Altitude</u></td>
    <td width="10%" bgcolor="#F3F3F3"><u>Aircraft</u></td>
  </tr>
  <?php while ($row = mysql_fetch_assoc($result)) {?><tr>
    <td><span class="style8"><?php echo $row['RID'];?>&nbsp;</span></td>
    <td><?php echo $row['Departingairport'];?>&nbsp;(<?php echo $row['DepartingICAO'];?>)</td>
    <td><?php echo $row['Arrivingairport'];?>&nbsp;(<?php echo $row['ArrivingICAO'];?>)</td>
    <td><?php echo $row['DepartingTime'];?>&nbsp;</td>
    <td><?php echo $row['Distance'];?>&nbsp;</td>
    <td><?php echo $row['Alt'];?>&nbsp;</td>
    <td><?php echo $row['Aircraft'];?>&nbsp;</td>
  </tr><?php } ?>
</table>
<p class="style6">We have a total of <?

include 'exteriorfile.php';

$query_count = "SELECT * FROM routes";

$result_count = mysql_query($query_count);

$totalrows = mysql_num_rows($result_count);

echo $totalrows

?> routes in our database </p>
</body></html>[/quote]

Thanks

James
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.