A database for my data

From: ANT_THOMAS30 Nov 2008 14:50
To: Peter (BOUGHTONP) 38 of 158
I've stuck these lines in...

PHP code:
<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<script type="text/javascript">$j = jQuery.noConflict();</script>

(between the head tags)

then

PHP code:
<a href='./nmr/{$row['machine']}/{$row['year']}/{$row['month']}/data/AB/nmr/{$row['servercode']}/{$row['nmrn1']}/pdata/1/email_{$row['servercode']}_{$row['nmrn1']}_1.png' onclick='$j('img#spectra').attr('src','./nmr/{$row['machine']}/{$row['year']}/{$row['month']}/data/AB/nmr/{$row['servercode']}/{$row['nmrn1']}/pdata/1/email_{$row['servercode']}_{$row['nmrn1']}_1.png').show(); return false'>{$row['nmre1']} #2</a>
<br />
<img id='spectra' src='' alt='' style='display:none;'/>


But it just opens the image directly instead of within the page.
EDITED: 30 Nov 2008 14:51 by ANT_THOMAS
From: Ally30 Nov 2008 17:20
To: ANT_THOMAS 39 of 158
Two possibilities:

  • The js is erroring out before it gets to return false- therefore it'll just go ahead and follow the link. It might then also wipe the JS error console and you won't get to see what the problem is. Try just doing a href to # to test it.
  • return false isn't actually a terribly reliable way of stopping things from happening. I forget the details now but in MooTools you'd call e.stop() to stop all event bubbling. But Peter made you use jQuery, so he can bloody well sort out his own mess, can't he?
From: Peter (BOUGHTONP)30 Nov 2008 18:02
To: ANT_THOMAS 40 of 158
Firstly, make sure you're testing in Firefox, and have Firebug installed, as it makes debugging much easier.

Then, you can either:
1) switch the href to # like Ally says
2) rename it to xhref which is a little simpler

And then look in Firebug's console to see what it says about the error.
From: ANT_THOMAS30 Nov 2008 20:17
To: Peter (BOUGHTONP) 41 of 158
Changed it to <x href and firebug says

syntax error
http://wakeupbomb.no-ip.org/full.php?id=5
Line 1
(

When it is an 'a' the code outputted by the page is:

PHP code:
<a false="" return="" ).show();="" email_2008-10-09-ab-49_10_1.png="" 1="" pdata="" 10="" 2008-10-09-ab-49="" nmr="" ab="" data="" oct="" 2008="" bruk400data="" .="" ,="" src="" ).attr(="" img#spectra="" onclick="(" href="./nmr/bruk400data/2008/Oct/data/AB/nmr/2008-10-09-AB-49/10/pdata/1/email_2008-10-09-AB-49_10_1.png">F #2</a>
<br/>
<img id="spectra" style="display: none;" alt="" src=""/>


I see that somehow there is a cheeky extra RHS bracket there:

<a false="" return="" ).show();=""

The code I on the page is:

PHP code:
<a href='./nmr/{$row['machine']}/{$row['year']}/{$row['month']}/data/AB/nmr/{$row['servercode']}/{$row['nmrn1']}/pdata/1/email_{$row['servercode']}_{$row['nmrn1']}_1.png' onclick='$j('img#spectra').attr('src','./nmr/{$row['machine']}/{$row['year']}/{$row['month']}/data/AB/nmr/{$row['servercode']}/{$row['nmrn1']}/pdata/1/email_{$row['servercode']}_{$row['nmrn1']}_1.png').show(); return false'>{$row['nmre1']} #2</a>
<br />
<img id='spectra' src='' alt='' style='display:none;'/>


I should really start using your $prefix idea because the long directory structures are doing my head in now.
From: Peter (BOUGHTONP)30 Nov 2008 20:30
To: ANT_THOMAS 42 of 158
Eh? I didn't mean <x href=""... - I meant <a xhref=""...

Anyway, no matter - the problem is that you've got single quotes for the HTML attributes, which is conflicting with the JavaScript single quotes, and causing all that funniness to occur.

Use double quotes for the onclick and it should be fine - but that means escaping them with a backslash so PHP echo doesn't get all confused:

code:
onclick=\"...\"


Once you've got that all working, you can replace the URL within onclick section with this.href so you don't have to repeat yourself but also keep your code accessible.
From: ANT_THOMAS30 Nov 2008 21:34
To: Peter (BOUGHTONP) 43 of 158
Done, no worky :((

It now, as you would image imagine, looks like this with the backslashes, two needed I assume?

PHP code:
<a href='$prefix{$row['nmrn1']}/pdata/1/email_{$row['servercode']}_{$row['nmrn1']}_1.png' onclick=\"$j('img#spectra').attr('src','$prefix{$row['nmrn1']}/pdata/1/email_{$row['servercode']}_{$row['nmrn1']}_1.png').show(); return false\">{$row['nmre1']} #2</a>
<br />
<img id='spectra' src='' alt='' style='display:none;'/>
 
</span


Still opens the image directly and not within the page.
EDITED: 30 Nov 2008 21:46 by ANT_THOMAS
Message 35356.44 was deleted
From: Peter (BOUGHTONP) 1 Dec 2008 00:13
To: ANT_THOMAS 45 of 158
Ah, bloody PHP - you also need to escape the $ of $j with \

So, your first line now becomes:
PHP code:
<a href='$prefix{$row['nmrn1']}/pdata/1/email_{$row['servercode']}_{$row['nmrn1']}_1.png' onclick=\"\$j('img#spectra').attr('src', this.href ).show(); return false\">{$row['nmre1']} #2</a>

(That also has the second url replaced with this.href)

Once you've done that it'll work.
EDITED: 1 Dec 2008 00:14 by BOUGHTONP
From: ANT_THOMAS 1 Dec 2008 00:46
To: Peter (BOUGHTONP) 46 of 158
Yay, thank you for your persistence Peter!
From: ANT_THOMAS 4 Dec 2008 00:31
To: ALL47 of 158
Right. It is all working very nicely now. I had missed the option in IrfanView for PDF to PNG batch conversions so that's all good except for the image quality being a bit poor, but nevermind.

I have a question tho. I want to pull, for example, 5 consecutive rows from my database. Sounds simple enough to me, but I can't get it working how I want.

Current code is...

PHP code:
$tableid = $_GET['id'];
$query  = "SELECT * FROM nmr LIMIT 5 OFFSET $tableid";
 


If id=5 then it gives me entry 5-9, as you would expect. Problem being, the first id in my database isn't 1 due to deleting a few, and if I was to delete entries at a later date even if the first one was 1 it would mess things up. So clearly this code is wrong and I realise this.

I can already get id from the address bar but I then want it to pull the record that has that id and the next 4 records. So the limit is 5 including the record with the id in the address bar.

I've searched and searched but had no luck :(
From: andy 4 Dec 2008 00:33
To: ANT_THOMAS 48 of 158

select * from nmr where id >= offset limit 5

 

edit: oh and you prob want a "order by id asc" just to make sure.

EDITED: 4 Dec 2008 00:34 by ANDY
From: Matt 4 Dec 2008 00:42
To: ANT_THOMAS 49 of 158
php code:
$tableid = $_GET['id'];
$query  = "SELECT * FROM nmr WHERE id >= $tableid LIMIT 5";
 


Of course change the name of the id column, if it's different.

Also, if you're not already you should really be using mysql_escape_string / mysql_real_escape_string on the variables you take from $_GET / $_POST / $_COOKIE etc. to use in your queries, or you risk SQL injection.

I could for instance do:

http://www.yourdomain.com/script.php?id=;DELETE%20*%20FROM%20nmr

And empty your database for you.
From: ANT_THOMAS 4 Dec 2008 01:00
To: Matt 50 of 158
Thank you both of you.

Hmm, I think I should too because someone emptying my database would make me cry.

Would this be sufficient?

PHP code:
 
$getid = $_GET['id'];
$tableid = mysql_escape_string($getid);
$query  = "SELECT * FROM nmr WHERE id >= $tableid LIMIT 5";
 
From: Peter (BOUGHTONP) 4 Dec 2008 01:08
To: ANT_THOMAS 51 of 158
Why use two steps?

Simpler to just do this:
code:
$tableid = mysql_escape_string($_GET['id']);

?
From: Matt 4 Dec 2008 01:18
To: ANT_THOMAS 52 of 158
That will work fine.

Personally, if I'm expecting $_GET['id'] to be a number I would use PHP's is_numeric function to make sure that's what it is. If I'm expecting a string or an unknown data type then I'd use mysql_real_escape_string.

php code:
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
 
    // No need to escape via mysql_escape_string as we
    // know it's numeric from the is_numeric test in the
    // if statement.
    $table_id = $_GET['id'];
 
    // sprintf can also be used to sanitise input from a user,
    // but it can make code harder to read. In this example %d
    // requires the variable to be an integer. The other types
    // are listed on the manual page.
    $query  = sprintf("SELECT * FROM nmr WHERE id >= %d LIMIT 5", $table_id);
 
    // Rest of your code goes here
 
}else {
 
    // ID is not set in the URL query or it is not a number, show an error here?
 
}


If you want to write better PHP code, turn display_errors on and set error_reporting to E_ALL (both in php.ini) and you can see what PHP really thinks of your script.
From: Dan (HERMAND) 4 Dec 2008 10:20
To: ANT_THOMAS 53 of 158
I'm still in bed so can't quite comprehend the issue, but do you just need to order them now? The query isn't interested in the missing numbers.
From: ANT_THOMAS 4 Dec 2008 17:40
To: ALL54 of 158

Problem number something:

 

I have a load of the data I want in my database in the form of an Excel spreadsheet. I've now saved it as a CSV and removed all the lines already present. I thought I could import this using PHPMyAdmin but it turns out I can't. I've read that it's possible (from the PHPMA wiki of all places) but for me it only gives me the option to import SQL based files.

 

I've upgraded PHPMA versions from 2.11.6 to 3.1.0 with no luck.

 

How and where does one enable CSV imports/uploads?

From: Peter (BOUGHTONP) 4 Dec 2008 17:49
To: ANT_THOMAS 55 of 158
Browse into the actual table you want before selecting import, then you get the CSV option appearing.
From: ANT_THOMAS 4 Dec 2008 18:57
To: Peter (BOUGHTONP) 56 of 158
Sure I tried it that way but I'll check when I get home.
From: Peter (BOUGHTONP) 4 Dec 2008 19:00
To: ANT_THOMAS 57 of 158
You have to actually browse the table - not simply view it.

It is more convoluted than it needs to be - should have the CSV option available at all levels, and just allow you to select a database/table from a list if it's not obvious where you're importing to.