CodingA database for my data

 

Press Ctrl+Enter to quickly submit your post
Quick Reply  
 
 
  
 From:  Matt  
 To:  ANT_THOMAS      
35356.115 In reply to 35356.111 
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.

doohicky

0/0
 Reply   Quote More 

 From:  ANT_THOMAS   
 To:  Peter (BOUGHTONP)     
35356.116 In reply to 35356.113 
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.
0/0
 Reply   Quote More 

 From:  ANT_THOMAS   
 To:  Matt     
35356.117 In reply to 35356.115 

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 )

0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  Drew (X3N0PH0N)     
35356.118 In reply to 35356.114 
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.
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  ANT_THOMAS      
35356.119 In reply to 35356.116 
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)
0/0
 Reply   Quote More 

 From:  ANT_THOMAS   
 To:  Peter (BOUGHTONP)     
35356.120 In reply to 35356.119 
Indeed you can, much much nicer. Thanks!
0/0
 Reply   Quote More 

 From:  Matt  
 To:  ANT_THOMAS      
35356.121 In reply to 35356.117 
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>

doohicky

0/0
 Reply   Quote More 

 From:  Drew (X3N0PH0N)  
 To:  Peter (BOUGHTONP)     
35356.122 In reply to 35356.118 
isseting would be pointless because it's ... been set already.

0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  ANT_THOMAS      
35356.123 In reply to 35356.120 
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.
0/0
 Reply   Quote More 

 From:  ANT_THOMAS   
 To:  Matt     
35356.124 In reply to 35356.121 

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).

0/0
 Reply   Quote More 

 From:  Drew (X3N0PH0N)  
 To:  Peter (BOUGHTONP)     
35356.125 In reply to 35356.118 
isseting would be pointless because it's ... been set already.

0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  Drew (X3N0PH0N)     
35356.126 In reply to 35356.122 
Hmm, so does $this = $_GET['that'] return empty string if that's not defined?

... computer says yes. Bah.
0/0
 Reply   Quote More 

 From:  Drew (X3N0PH0N)  
 To:  Peter (BOUGHTONP)     
35356.127 In reply to 35356.126 
Aye. It's a pain in the arse.

0/0
 Reply   Quote More 

 From:  Matt  
 To:  Peter (BOUGHTONP)     
35356.128 In reply to 35356.126 
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

doohicky

0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  Matt     
35356.129 In reply to 35356.128 
Ah, fair enough then. And yeah, it is null when var_dumped.
0/0
 Reply   Quote More 

 From:  ANT_THOMAS   
 To:  ALL
35356.130 
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?!
0/0
 Reply   Quote More 

 From:  Drew (X3N0PH0N)  
 To:  ANT_THOMAS      
35356.131 In reply to 35356.130 
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.
0/0
 Reply   Quote More 

 From:  ANT_THOMAS   
 To:  Drew (X3N0PH0N)     
35356.132 In reply to 35356.131 

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

 

I'm trying (honest :$ )

0/0
 Reply   Quote More 

 From:  ANT_THOMAS   
 To:  Drew (X3N0PH0N)     
35356.133 In reply to 35356.131 
To help more, I'd like the images somewhere within this lot....

PHP code:
$car = $_GET['car'];

$query  = "SELECT * FROM cars WHERE carcode = '$car' ";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))

echo "
<table>
    <tr>
    <th rowspan='5'><a href='#'><img src='./photos/{$row[thumb]}' /></a>
    <br />
    <td colspan='2' width='300px'><a href='car.php?car={$row[carcode]}'>{$row[title]}</a></td>
    </tr>
    <tr>
    <td width='250px'>{$row[year]}<br />
                      {$row[mileage]}<br />
                      {$row[engine]}<br />
                      {$row[fuel]}<br />
                      {$row[gearbox]}<br />     </td>
    <td width='250px'>{$row[colour]}<br />
                      {$row[mot]}<br />
                      {$row[tax]}<br /></td>
    </tr>
    <tr>
    <td colspan='2'>{$row[extra]}</td>
    </tr>
    <tr>
    <td colspan='2' align='right' valign='bottom'><b>&pound;{$row[price]}</b></td>
    </tr>
    <tr>
    <td colspan='2'>FULL WIDTH</td>
    </tr>
</table>

         ";
0/0
 Reply   Quote More 

 From:  ANT_THOMAS   
 To:  ALL
35356.134 

I think I'm doing this totally wrong, because I actually want to do some stuff with the output from the other rows depending on what it is rather than just showing their content.

 

Back to the drawing board possibly.

0/0
 Reply   Quote More 

Reply to All  
 

1–20  …  61–80  81–100  101–120  121–140  141–158

Rate my interest:

Adjust text size : Smaller 10 Larger

Beehive Forum 1.5.2 |  FAQ |  Docs |  Support |  Donate! ©2002 - 2024 Project Beehive Forum

Forum Stats