A database for my data

From: Peter (BOUGHTONP) 3 Aug 2010 18:43
To: ANT_THOMAS 113 of 158
Here's the cleverer solution that doesn't repeat code. :)

php code:
$sql="SELECT * FROM weight WHERE year='$year'";
 
if ($month != "") $sql .= "AND month='$month'";


Some people will probably complain that if statement should be using braces, but meh. Works either way.
EDITED: 3 Aug 2010 18:45 by BOUGHTONP
From: Drew (X3N0PH0N) 3 Aug 2010 18:48
To: Peter (BOUGHTONP) 114 of 158
Clever lad.
From: Matt 3 Aug 2010 18:56
To: ANT_THOMAS 115 of 158
You're falling foul of operator precedence. The OR in your SQL statement is matching everything from 2010, which undoes the other WHERE clauses.

SQL code:
SELECT * FROM weight WHERE month='August' AND year='2010' OR year='2010'
 


gives you the same result set as:

SQL code:
SELECT * FROM weight WHERE year='2010'


If you ever need to combine OR and AND in your WHERE clause you'll need to bracket them correctly. For example, to get matches for August 2010 and everything in 2010 you would do:

SQL code:
SELECT * FROM weight WHERE (month='August' AND year='2010') OR year='2011'


As for combining your query strings. You'll have to perform tests in PHP and construct the SQL you need based on the presence of the URL query variables

PHP code:
if (isset($_GET['month'], $_GET['year'])) {
 
    $month = mysql_escape_string($month);
    $year = mysql_escape_string($year);
 
    $sql = "SELECT * FROM weight WHERE month = '$month' AND year = '$year'";
 
} else if (isset($_GET['month'])) {
 
    $month = mysql_escape_string($month);
    $sql = "SELECT * FROM weight WHERE month = '$month'";
 
} else if (isset($_GET['year'])) {
 
    $year = mysql_escape_string($year);
    $sql = "SELECT * FROM weight WHERE year = '$year'";
}
 
$result = mysql_query($sql);


Note the use of mysql_escape_string too. It's your best friend.
From: ANT_THOMAS 3 Aug 2010 19:02
To: Peter (BOUGHTONP) 116 of 158
What Drew said, it works great.

Next question

I'm wanting to use some input boxes to enter the month and/or year to generate the URLs.

The one for just year works fine
HTML code:
<input type="text" id="year" value="Enter Year"/>
<button type="button" onclick="location.href='./date.php?year='+document.getElementById('year').value;">Go To</button></div>


But I want it to grab two variables for the year and month one
HTML code:
<input type="text" id="month" value="Enter Month"/>
<input type="text" id="year1" value="Enter Year"/>
<button type="button" onclick="location.href='./date.php?month='+document.getElementById('month').value'&year='+document.getElementById('year1').value;">Go To</button

Unsurprisingly this messy thing doesn't work.
From: ANT_THOMAS 3 Aug 2010 19:07
To: Matt 117 of 158

What's this mysql_escape_string chap and what does it do for me?

 

(As I'm sure you're aware you code works perfectly well also :D )

From: Peter (BOUGHTONP) 3 Aug 2010 19:14
To: Drew (X3N0PH0N) 118 of 158
And yet still flawed. :(

I did think it should be doing isset instead of != "", but decided to trust you instead of looking it up.

And in either case I should have mentioned escaping. :'(

Bah.
From: Peter (BOUGHTONP) 3 Aug 2010 19:18
To: ANT_THOMAS 119 of 158
Hmmm, unless I'm being crazy, can you just do this:

html code:
<form action="./date.php" method="get">
	<input type="text" id="year" name="year" value="Enter Year" />
	<button type="submit">Go To</button>
</form>
 
<form action="./date.php" method="get">
	<input type="text" id="month" name="month" value="Enter Month/>
	<input type="text" id="year1" name="year" value="Enter Year" />
	<button type="submit">Go To</button>
</form>


?

(you may also need to restyle the form tags to remove margins/etc this way, but it's a better way than doing onclick=location.href stuff)
EDITED: 3 Aug 2010 19:20 by BOUGHTONP
From: ANT_THOMAS 3 Aug 2010 19:22
To: Peter (BOUGHTONP) 120 of 158
Indeed you can, much much nicer. Thanks!
From: Matt 3 Aug 2010 19:23
To: ANT_THOMAS 121 of 158
It escapes data for use in a MySQL query. Without it you open yourself up to SQL inject attacks. This explains some of the possibilities quite well: http://unixwiz.net/techtips/sql-injection.html

Your form, just use:

HTML code:
<form method="get" action="date.php">
  <input name="month" type="text" value="Enter month" />
  <input name="year" type="text" value="Enter year" />
  <button type="submit">Go To</button>
</form>
EDITED: 3 Aug 2010 19:24 by MATT
From: Drew (X3N0PH0N) 3 Aug 2010 19:26
To: Peter (BOUGHTONP) 122 of 158
isseting would be pointless because it's ... been set already.
From: Peter (BOUGHTONP) 3 Aug 2010 19:26
To: ANT_THOMAS 123 of 158
Woohoo. :)

Worth pointing out the id attributes are only necessary if you're also referencing these fields elsewhere (in html/js/css) - if not, just drop them, since the form submit and php stuff uses the name attribute.
From: ANT_THOMAS 3 Aug 2010 19:27
To: Matt 124 of 158

I see, yeah I guess I should be using escapes then.

 

Fantastic stuff, I can now be a sad bastard and track my weight via a pretty(ish) graph (using PHPGraphLib).

From: Drew (X3N0PH0N) 3 Aug 2010 19:28
To: Peter (BOUGHTONP) 125 of 158
isseting would be pointless because it's ... been set already.
From: Peter (BOUGHTONP) 3 Aug 2010 19:32
To: Drew (X3N0PH0N) 126 of 158
Hmm, so does $this = $_GET['that'] return empty string if that's not defined?

... computer says yes. Bah.
EDITED: 3 Aug 2010 19:37 by BOUGHTONP
From: Drew (X3N0PH0N) 3 Aug 2010 19:48
To: Peter (BOUGHTONP) 127 of 158
Aye. It's a pain in the arse.
From: Matt 3 Aug 2010 20:04
To: Peter (BOUGHTONP) 128 of 158
It should be NULL, not an empty string. If you var_dump the variable it'll show you what it contains.

If you turn on PHP strict error checking you'll get an undefined index error if you don't test with isset / array_key_exists. I prefer to code with strict errors switched on, it's safer and makes me feel smarter
From: Peter (BOUGHTONP) 3 Aug 2010 21:01
To: Matt 129 of 158
Ah, fair enough then. And yeah, it is null when var_dumped.
From: ANT_THOMAS 6 Jun 2012 18:51
To: ALL130 of 158
I need some more database coding help.

I have a row with a list of comma separated image filenames.

eg
code:
image1.jpg,image2.jpg,image3.jpg,image4.jpg


I generally PHP echo out the contents of a row using a nice and simple
PHP code:
{$row[images]}


That would obviously just chuck out the text with the commas, useless. I want each imagen.jpg wrapped in some HTML without the commas.
eg
HTML code:
<img src="image1.jpg" /><img src="image2.jpg" /><img src="image3.jpg" /><img src="image4.jpg" />


HOW?!
From: Drew (X3N0PH0N) 6 Jun 2012 18:58
To: ANT_THOMAS 131 of 158
php code:
$blah = split(",", $row[images]);
foreach ($blah as $beep) {
echo "<img src=\"" . $beep . "\" />";
}


Probably a neater way which PB or Matt will come along and embarrass me with.
From: ANT_THOMAS 6 Jun 2012 19:29
To: Drew (X3N0PH0N) 132 of 158

And how do I pop that within a current big echo?

 

I'm trying (honest :$ )