...

View Full Version : A database from which users can make choices and save them



okeddy
11-07-2010, 05:30 PM
Hello folks I am new to php and I have been trying to put together a database that a user can search and choose from the results. I have managed to make this script by copying code from google searches and trial and error.
The script so far has been tested and works.
The hard part is the code for choosing from the results, I have tried some things but I have been far from the mark, the thing is I can't get my head around the problem, if the first field is a number which is unique to each row, how can I pick that up in a php argument.
I have tried making the first field an href link to send that number to a different table which would collect the results of the users choices, but I'm just not sure what to put in the code.
Could someone throw me a lifeline here I've searched for hours on google to find any code that looks like it would work with no luck. :(




// Get the search variable from URL

$var = @$_GET['a'] ;
$trimmed1 = trim($var); //trim whitespace from the stored variable

$var = @$_GET['b'] ;
$trimmed2 = trim($var);

$var = @$_GET['c'] ;
$trimmed3 = trim($var);

$var = @$_GET['d'] ;
$trimmed4 = trim($var);

$var = @$_GET['e'] ;
$trimmed5 = trim($var);

$var = @$_GET['f'] ;
$trimmed6 = trim($var);


//connect to your database
mysql_connect("localhost","root",""); //(host, username, password)

//specify database
mysql_select_db("a2149809_MV") or die("Unable to select database"); //select which database we're using

// Build SQL Query
$query = "SELECT * FROM `table` WHERE `field1` LIKE \"%$trimmed1%\" AND `field2` LIKE \"%$trimmed2%\" AND `field3` LIKE \"%$trimmed3%\"
AND `field4` LIKE \"%$trimmed4%\" AND `field5` LIKE \"%$trimmed5%\" AND `field6` LIKE \"%$trimmed6%\" order by `field1`";

$result=mysql_query($query);
$num=mysql_num_rows($result);

mysql_close();



<table width="100%" border=2 cellspacing=2 cellpadding=2>



<tr><form name="form" action="" method="get">



<td colspan="6"><input type="submit" name="Submit" value="Search" />



</td>



</tr>



<tr>



<td><input type="text" name="a" value="" size="4" /></td>



<td><input type="text" name="b" value="" size="40" /></td>
<td><input type="text" name="c" value="" size="3" /></td>



<td><input type="text" name="d" value="" size="10" /></td>



<td><input type="text" name="e" value="" size="10" /></td>



<td><input type="text" name="f" value="" size="10" /></td>



</form></tr>











<?php
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"Field1");
$f2=mysql_result($result,$i,"Field2");
$f3=mysql_result($result,$i,"Field3");
$f4=mysql_result($result,$i,"Field4");
$f5=mysql_result($result,$i,"Field5");
$f6=mysql_result($result,$i,"Field6");

?>

<tr>
<td><?php echo $f1; ?></td>
<td><?php echo $f2; ?></td>



<td><?php echo $f3; ?></td>



<td><?php echo $f4; ?></td>



<td><?php echo $f5; ?></td>



<td><?php echo $f6; ?></td>





</tr>







<?php
$i++;
}
?>
</table>

Keleth
11-07-2010, 06:19 PM
I'm not sure what you mean by "pick that up in a PHP argument". I assume you mean variable, as argument is what you pass a function, and I don't see any function you're referring to here. With the indiscript field names, its difficult to figure out what you're trying to do, thus hard to say what you want. I strongly suspect your DB is setup poorly, but you mentioned you're new, so its excusable, but you'd be surprised how an efficient DB setup makes a lot of problems easier. There are a few things you can fix in the code immediately, such as


<?php
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"Field1");
$f2=mysql_result($result,$i,"Field2");
$f3=mysql_result($result,$i,"Field3");
$f4=mysql_result($result,$i,"Field4");
$f5=mysql_result($result,$i,"Field5");
$f6=mysql_result($result,$i,"Field6");

?>

<tr>
<td><?php echo $f1; ?></td>
<td><?php echo $f2; ?></td>



<td><?php echo $f3; ?></td>



<td><?php echo $f4; ?></td>



<td><?php echo $f5; ?></td>



<td><?php echo $f6; ?></td>





</tr>







<?php
$i++;
}
?>

Ignoring the overly numerous white spaces, this is an inefficient way to pull data. You're doing 6 function calls on the result, when you only need one:


<?php while ($row = mysql_fetch_array($result)) { ?>
<tr>
<td><?php echo $row['Field1']; ?></td>
<td><?php echo $row['Field2']; ?></td>
<td><?php echo $row['Field3']; ?></td>
<td><?php echo $row['Field4']; ?></td>
<td><?php echo $row['Field5']; ?></td>
<td><?php echo $row['Field6']; ?></td>
</tr>
<?php } ?>

So anyway, again, whats the exact issue?

MattF
11-07-2010, 06:24 PM
I really would suggest you read this before doing any more DB query code:

http://uk3.php.net/manual/en/function.mysql-real-escape-string.php

okeddy
11-07-2010, 07:53 PM
Thanx Keleth and MattF for your replies, I have swapped the code you suggested Keleth and it works the same as before with tidier code, great.:thumbsup:

What I wanted to know was how to allow a user to choose any row of the search results, each row is a single item but I have separated each field into separate tds when I suppose I could have had them in one td.
It's actually Artist, Title, Disc, Era, Date Added, Aspect Ratio.
The search is set this way so you could search for all "Beatles" tracks with "love" in the title. The search was like that so I just copied that format to the results.
If the user could choose a row and have this row added to another table or just print it in another page, like an 'add to cart' button.

Thanks for any suggestions I hope I have made my problem clear.

Keleth
11-07-2010, 08:01 PM
That's exactly what your primary key column should be about. A unique identifier for any row. In this case, I don't see you having a column as an identifier. People do often use incremental integer, so its always different. If you had that, all you'd have to do is pass that integer and you'd know what row they're referring to.

okeddy
11-07-2010, 08:30 PM
So Keleth if I had a primary key column what would the code be to pass that to another page or table?, If I could see an example of this i might be able to change it for my own needs but I'm unsure of what to search for..

MattF
11-07-2010, 08:46 PM
You'd have a table something like the following:

id (auto_increment)
field1 (varchar)
field2 (varchar)
field3 (varchar)
primary_key(id)

That way, the id will automatically increment for each entry you add and the id is the primary key, so you can only ever have one row with that id. You'd then just select the id in your search query and pass that to the next query when you want the specific results for that entry alone.

Keleth
11-07-2010, 09:58 PM
Well, there's no "code to pass it on"... once you have that number, you can do with it as you like. You can pass it on as a GET variable, have some other scheme which works on it.

okeddy
11-07-2010, 10:27 PM
I have created a new field called id with auto increment, so I may be able to figure out how to send the information to a page or table but I'm unclear as to how to get that info from the database, and use it.

I've put 'id' into the $query


$query = "SELECT * FROM `music vidz` `id`
WHERE `Artist` LIKE \"%$trimmed1%\"
AND `Title` LIKE \"%$trimmed2%\"
AND `Disc` LIKE \"%$trimmed3%\"
AND `Era` LIKE \"%$trimmed4%\"
AND `Date Added` LIKE \"%$trimmed5%\"
AND `AR` LIKE \"%$trimmed6%\" order by `Artist`";




And then tried $id = $newArray['id']; here:


$var = @$_GET['a'] ;
$trimmed1 = trim($var);

$var = @$_GET['b'] ;
$trimmed2 = trim($var);

$var = @$_GET['c'] ;
$trimmed3 = trim($var);

$var = @$_GET['d'] ;
$trimmed4 = trim($var);

$var = @$_GET['e'] ;
$trimmed5 = trim($var);

$var = @$_GET['f'] ;
$trimmed6 = trim($var);

$id = $newArray['id'];


I was then going to make a href to print the variable $id but as you will know it doesn't work, can you help me there?

Keleth
11-07-2010, 11:43 PM
Where are you getting $newArray from?

okeddy
11-07-2010, 11:52 PM
Yes I know it's rubbish, I am trying anything and everything
I now have

$var = @$_GET['x'] ; $id = ($var) ;

$query2 = "SELECT * FROM `music vidz` `id`";
and
td><?php echo $row['id']; ?></td>
<td><?php echo $row['Artist']; ?></td>
<td><?php echo $row['Title']; ?></td>
<td><?php echo $row['Disc']; ?></td>
<td><?php echo $row['Era']; ?></td>
<td><?php echo $row['Date Added']; ?></td>
<td><?php echo $row['AR']; ?></td>

which shows no errors
just not sure what to do with 'x'

Keleth
11-07-2010, 11:56 PM
Heh, yah, I'll be honest, I have no idea what's going on anymore. First, the query you put up there makes no sense :P

What are you trying to do now, show us the code, and tell us the bug.

Keleth
11-07-2010, 11:56 PM
Heh, yah, I'll be honest, I have no idea what's going on anymore. First, the query you put up there makes no sense :P

What are you trying to do now, show us the code, and tell us the bug.

okeddy
11-08-2010, 12:11 AM
here is all the code without the header and host etc






$var = @$_GET['a'] ;
$trimmed1 = trim($var);

$var = @$_GET['b'] ;
$trimmed2 = trim($var);

$var = @$_GET['c'] ;
$trimmed3 = trim($var);

$var = @$_GET['d'] ;
$trimmed4 = trim($var);

$var = @$_GET['e'] ;
$trimmed5 = trim($var);

$var = @$_GET['f'] ;
$trimmed6 = trim($var);

$var = @$_GET['x'] ;
$id = ($var) ;



// Build SQL Query
$query = "SELECT * FROM `music vidz`
WHERE `Artist` LIKE \"%$trimmed1%\"
AND `Title` LIKE \"%$trimmed2%\"
AND `Disc` LIKE \"%$trimmed3%\"
AND `Era` LIKE \"%$trimmed4%\"
AND `Date Added` LIKE \"%$trimmed5%\"
AND `AR` LIKE \"%$trimmed6%\" order by `Artist`";

$query2 = "SELECT * FROM `music vidz` `id`";

$result=mysql_query($query);
$num=mysql_num_rows($result);



mysql_close();
?>



<table width="100%" border=2 cellspacing=2 cellpadding=2>
<tr><form name="form" action="" method="get">
<td colspan="7"><input type="submit" name="Submit" value="Search" />
<input type="reset" value="Reset" /></td>
</tr>
<tr>
<td>id</td>
<td>Artist
<input type="text" name="a" value="" size="40" /></td>
<td>Title
<input type="text" name="b" value="" size="40" /></td>
<td>Disc
<input type="text" name="c" value="" size="3" maxlength=3 /></td>
<td>Era
<select name="d">
<option value=""></option>
<option value="50s">50s</option>
<option value="60s">60s</option>
<option value="70s">70s</option>
<option value="80s">80s</option>
<option value="90s">90s</option>
<option value="00s">00s</option>
<option value="10s">10s</option>
</select></td>
<td>Date
<input type="text" name="e" value="" size="10" maxlength="10" /></td>
<td>A/R
<select name="f">
<option value=""></option>
<option value="4:3">4:3</option>
<option value="16:9">16:9</option></select></td>
</form></tr>



<?php while ($row = mysql_fetch_array($result)) { ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['Artist']; ?></td>
<td><?php echo $row['Title']; ?></td>
<td><?php echo $row['Disc']; ?></td>
<td><?php echo $row['Era']; ?></td>
<td><?php echo $row['Date Added']; ?></td>
<td><?php echo $row['AR']; ?></td>

</tr>
<?php } ?>
</table>

Keleth
11-08-2010, 12:41 AM
Ok, I see your code now, though I don't understand what some of it does (and a bunch of it is semantically wrong... a form inside a tr for example).

SELECT * FROM `music vidz` `id` is supposed to do what?

In fact, the only thing I'm not getting is what you want to do with the ID at the top?

okeddy
11-08-2010, 01:02 AM
Well that is supposed to bring the id field into the database results
here is what the db looks like I want to send any row to another file or table
but I can't get my head around the code for it

https://docs.google.com/leaf?id=0B5c-qZPscnixZmUzN2RjNmYtYmY0Yy00MGJhLWIyNDctZjBiOTk3MjFkMDhl&hl=en_GB

okeddy
11-08-2010, 01:02 AM
sorry


https://docs.google.com/leaf?id=0B5c-qZPscnixZmUzN2RjNmYtYmY0Yy00MGJhLWIyNDctZjBiOTk3MjFkMDhl&hl=en_GB

MattF
11-08-2010, 01:11 AM
When you make your new query with the id supplied, (either through GET or POST), your query for that specific row should look something like:



$query = 'SELECT * FROM `music vidz` WHERE id='.intval($_GET['id']);


assuming you're using GET in that example above. Btw, it's bad form to use *. You should be naming the specific columns you want to extract from the DB.

Keleth
11-08-2010, 01:11 AM
As a small note, at least here, its usually more useful to give the create and insert statements directly so we can test it out ourselves :p

Regardless, if you have an auto_increment field, you don't need to (and won't be able to) add a id value when you insert, and when you're selecting, if you're selecting, you need to include `id` in the select columns. Having it randomly after the table does nothing.

Again, your first query grabs the id fine. Now just pass it along as a GET variable for example (in your url) and select on the other side, so something like:

file1, link to different page

<a href="file2.php?id=3">New table</a>

file2, select data

$query = 'SELECT * FROM `table` WHERE id = '.intval($_GET['id']);

As MattF said, avoid *, specially once your queries start to get more complex, and also, you shouldn't have spaces in table names/column names/db names... it'll make things a mess... also notice how both of us used intval... its called sanitizing, and its very important.

okeddy
11-08-2010, 01:31 AM
Thanks to you both for the help I realise that the code
$query2 = "SELECT * FROM `music vidz` `id`";
wasn't doing anything as the data was brought in with the first query.

It's going to take me a while to take in what you have suggested, I'll post again later, thx again.

Keleth
11-08-2010, 01:39 AM
Well.... it wasn't doing anything because you didn't do anything with it. You stored a string into a variable then never did anything with it. Just like you put $query into mysql_result, you have to do something with $query2.

okeddy
11-08-2010, 02:01 AM
Keleth what does id=3 mean?, is it supposed to be the id of the row that the user has chosen?, if so how will it get there?
<a href="file2.php?id=3">New table</a>

Keleth
11-08-2010, 02:43 AM
Yes, and for how it'll get there, echo it there.

okeddy
11-08-2010, 02:53 PM
I may be getting close but this still doesn't produce anything on the second page.

<?php

$var = @$_GET['a'] ;
$search1 = ($var);

$var = @$_GET['b'] ;
$search2 = ($var);

$var = @$_GET['c'] ;
$search3 = ($var);

$var = @$_GET['d'] ;
$search4 = ($var);

$var = @$_GET['e'] ;
$search5 = ($var);

$var = @$_GET['f'] ;
$search6 = ($var);

$var = @$_GET['x'] ;
$id = ($var);


mysql_connect("localhost","root","");

mysql_select_db("a2149809_MV") or die("Unable to select database");

$query = "SELECT * FROM `music vidz` WHERE `Artist` LIKE \"%$search1%\"
AND `Title` LIKE \"%$search2%\" AND `Disc` LIKE \"%$search3%\"
AND `Era` LIKE \"%$search4%\" AND `Date Added` LIKE \"%$search5%\"
AND `AR` LIKE \"%$search6%\" order by `Artist`";

$result=mysql_query($query);
$num=mysql_num_rows($result);

mysql_close();
?>
<table width="100%" border=2 cellspacing=2 cellpadding=2>
<tr><form name="form" action="" method="get">
<td colspan="8"><input type="submit" name="Submit" value="Search" />
<input type="reset" value="Reset" /></td>
</tr>
<tr>
<td>id<input type="hidden" name="x" value="" size="4" /></td>
<td>Artist
<input type="text" name="a" value="" size="40" /></td>
<td>Title
<input type="text" name="b" value="" size="40" /></td>
<td>Disc
<input type="text" name="c" value="" size="3" maxlength=3 /></td>
<td>Era
<select name="d">
<option value=""></option>
<option value="50s">50s</option>
<option value="60s">60s</option>
<option value="70s">70s</option>
<option value="80s">80s</option>
<option value="90s">90s</option>
<option value="00s">00s</option>
<option value="10s">10s</option>
</select></td>
<td>Date
<input type="text" name="e" value="" size="10" maxlength="10" /></td>
<td>A/R
<select name="f">
<option value=""></option>
<option value="4:3">4:3</option>
<option value="16:9">16:9</option></select></td>
</form></tr>



<?php while ($row = mysql_fetch_array($result)) { ?>
<tr>
<td><a href="test.php?id=$id"><?php echo $row['id']; ?></a></td>
<td><?php echo $row['Artist']; ?></td>
<td><?php echo $row['Title']; ?></td>
<td><?php echo $row['Disc']; ?></td>
<td><?php echo $row['Era']; ?></td>
<td><?php echo $row['Date Added']; ?></td>
<td><?php echo $row['AR']; ?></td>

</tr>
<?php } ?>
</table>

Second page

<?php

$id = $_GET['id'] ;

mysql_connect("localhost","root","");

mysql_select_db("a2149809_MV") or die("Unable to select database");

$query = "SELECT * FROM `music vidz` WHERE `id` =".intval($_GET['id']);

$result=mysql_query($query);

mysql_close();

?>

<?php while ($row = mysql_fetch_array($result)) { ?>

<?php echo $row['id']; ?>

<?php } ?>


Ther are no errors shown

Keleth
11-08-2010, 03:12 PM
First things first: you're pulling the GET twice... first at the top into $id, then again in $query. Just do it once, and I'd say do it at the top, $id = intval($_GET['id']);. Now that being said, its because you're passing gibberish, and its what you get for simply copy/pasting without looking at/understanding the code people give you.


<a href="test.php?id=$id"><?php echo $row['id']; ?>

The output where $row['id'] = 4 would link to:


test.php?id=$id4

So when you pull the GET, you're extracting '$id4', and the intval of that is 0.

okeddy
11-08-2010, 03:28 PM
Thanks Keleth I'll have a look at what you have said and you are right I don't understand some of the code you have given me but it wasn't for want of trying, :D

okeddy
11-08-2010, 04:00 PM
When I mouse over this it is not giving me a number just the variable


<a href="test.php?id=$id"><?php echo $row['id']; ?></a>


mouse over = test.php?id=$id


And the test page with this code is printing "id"


<?php

$id = intval($_GET['id']);

mysql_connect("localhost","root","");

mysql_select_db("a2149809_MV") or die("Unable to select database");

mysql_close();

?>

<?php echo 'id'; ?>



Am I sending anything to test.php??

MattF
11-08-2010, 04:12 PM
You're supposed to check example code before using it verbatim. There's always a probability that it's either a generic reference or just contains a typo.



<a href="test.php?id=<?php echo $row['id'].'">'.$row['id']; ?></a>

Keleth
11-08-2010, 04:36 PM
Good point Matt... my epic fail :p

Follow Matt's code, and on your second page, its echoing 'id' because you're echoing a string 'id'.

okeddy
11-08-2010, 05:02 PM
Thanks lads Matts code with mouse over is test.php?id=
is that what it should say?

but I still can't get anything to print in test.php after trying different perms other than id or whatever I put after echo

this is the code at the mo..

<?php

$id = intval($_GET['id']);

mysql_connect("localhost","root","");

mysql_select_db("a2149809_MV") or die("Unable to select database");

mysql_close();

?>

<?php echo 'id' ; ?>

can you throw me another bone here??

Keleth
11-08-2010, 05:06 PM
Is it showing the id on the page itself? Not in the link I mean, does it show the number in the table? And as for the second page, the reason I'm not blatently writing the code is its important you were where you went wrong.

Doing echo 'id' is echoing a string. You're just telling the system to put out the letter i followed by the letter d. You want it to print the the content you put into $id, which is a variable.

okeddy
11-08-2010, 07:25 PM
This code returns 0 on the test.php page when clicking any line


<?php

$id = intval($_GET['id']);
mysql_connect("localhost","root","");

mysql_select_db("a2149809_MV") or die("Unable to select database");

mysql_close();

?>


<?php echo $id; ?>

Keleth
11-08-2010, 08:02 PM
Perfect, your test page's code is correct now. Now going back to the previous page, the questions I had: does the id show in the table? And show us the table generation code as you have it now.

okeddy
11-08-2010, 08:30 PM
Thanks Keleth!


<html>
<head>
<title>Music Vidz Data Base</title>
<link rel="stylesheet" href="usa4.css" type="text/css" />
</head>
<body>

<?php

$var = @$_GET['a'] ;
$search1 = ($var);

$var = @$_GET['b'] ;
$search2 = ($var);

$var = @$_GET['c'] ;
$search3 = ($var);

$var = @$_GET['d'] ;
$search4 = ($var);

$var = @$_GET['e'] ;
$search5 = ($var);

$var = @$_GET['f'] ;
$search6 = ($var);

$var = @$_GET['x'] ;
$id = ($var);


mysql_connect("localhost","root","");

mysql_select_db("a2149809_MV") or die("Unable to select database");

$query = "SELECT * FROM `music vidz` WHERE `Artist` LIKE \"%$search1%\"
AND `Title` LIKE \"%$search2%\" AND `Disc` LIKE \"%$search3%\"
AND `Era` LIKE \"%$search4%\" AND `Date Added` LIKE \"%$search5%\"
AND `AR` LIKE \"%$search6%\" order by `Artist`";

$result=mysql_query($query);
$num=mysql_num_rows($result);

mysql_close();
?>
<table width="100%" border=2 cellspacing=2 cellpadding=2>
<tr><form name="form" action="" method="get">
<td colspan="8"><input type="submit" name="Submit" value="Search" />
<input type="reset" value="Reset" /></td>
</tr>
<tr>
<td>id
<input type="hidden" name="x" value="" size="4" /></td>
<td>Artist
<input type="text" name="a" value="" size="40" /></td>
<td>Title
<input type="text" name="b" value="" size="40" /></td>
<td>Disc
<input type="text" name="c" value="" size="3" maxlength=3 /></td>
<td>Era
<select name="d">
<option value=""></option>
<option value="50s">50s</option>
<option value="60s">60s</option>
<option value="70s">70s</option>
<option value="80s">80s</option>
<option value="90s">90s</option>
<option value="00s">00s</option>
<option value="10s">10s</option>
</select></td>
<td>Date
<input type="text" name="e" value="" size="10" maxlength="10" /></td>
<td>A/R
<select name="f">
<option value=""></option>
<option value="4:3">4:3</option>
<option value="16:9">16:9</option></select></td>
</form></tr>



<?php while ($row = mysql_fetch_array($result)) { ?>
<tr>
<td><a href="test.php?id=<?php echo $id.'">'.$row['id']; ?></a></td>
<td><?php echo $row['Artist']; ?></td>
<td><?php echo $row['Title']; ?></td>
<td><?php echo $row['Disc']; ?></td>
<td><?php echo $row['Era']; ?></td>
<td><?php echo $row['Date Added']; ?></td>
<td><?php echo $row['AR']; ?></td>

</tr>
<?php } ?>
</table>
</body>
</html>

Keleth
11-08-2010, 09:10 PM
So, so still, the question you're not answering :p Does the id show in the table or not?

okeddy
11-08-2010, 09:14 PM
Sorry yes the id does show in the table
relevant code is



$var = @$_GET['x'] ;
$id = ($var);

td>id<input type="hidden" name="x" value="" size="4" /></td>

<td><a href="test.php?id=<?php echo $id.'">'.$row['id']; ?></a></td>

okeddy
11-08-2010, 09:26 PM
db pic here:

https://doc-0g-5k-docs.googleusercontent.com/docs/secure/ha0ro937gcuc7l7deffksulhg5h7mbp1/g5rdujab1llkocro1u28o9b9p5lfqcgo/1289239200000/12870340946081238890/*/0B5c-qZPscnixMWI0Zjc5ZmMtYTE4Ni00M2U1LTlmNzgtNDgyMDY2MWRhNDk0

Keleth
11-08-2010, 09:43 PM
DB is irrelevant. This is again you copying code without checking to see what it does :p


<a href="test.php?id=<?php echo $id.'">'.$row['id']; ?></a>

You never define $id, so of course it comes up blank. It should be $row['id']. NEVER just copy code :)

okeddy
11-08-2010, 09:57 PM
Changed it to this
<a href="test.php?id=<?php echo $row['id'].'">'.$row['id']; ?></a>

and it works delivers the id to test.php
now I have to figure out how to print the entire row in test.php

Fantastic Keleth can't thank you enough for your patience..:thumbsup:

Keleth
11-08-2010, 10:00 PM
You had the right query code for it earlier


SELECT * FROM table WHERE id = $id

And I trust you won't simply copy that and will instead apply it :)

okeddy
11-08-2010, 10:05 PM
Yes you're right but I really need to be putting the choices in a new table, so I'll be working on that..

okeddy
11-09-2010, 10:27 PM
I have a page now that prints the selections, the first choice from the table moves you to the selection page and there is nothing there, go back to the table and the second choice shows the first choice on the selection page and so on, the select.txt has all the selections though and I realise that it is something to do with refreshing/reloading because if you refresh the selection page you catch up but you also re-enter the previous choice.
I could have the selections on a separate page altogether that wasn't viewed by the user till the end of their selections but I have the problem of them then going to a blank page every time they click a selection, OK I could put 'selection added' or something there with a back button to the table but I think this way looks better.
Or is there any way that I could stay on the table page after a user clicking a selection ??



<html>
<head>
<title>Music Vidz Selections</title>
</head>
<body>


<?php include("select.txt"); ?>

<?php

$file = fopen("select.txt","a");
$table = "<table><tr\r\n>";
$table2 = "</tr></table>";
$td = "<td>";
$td2 = "</td>\r\n";
$break = "\r\n";


$id = intval($_GET['id']);

mysql_connect("localhost","root","");

mysql_select_db("a2149809_MV") or die("Unable to select database");

$query = "SELECT * from `music vidz` where `id` ='$id'";

$result=mysql_query($query);
$num=mysql_num_rows($result);

mysql_close();

?>

<?php while ($row = mysql_fetch_array($result)) { ?>
<?php fwrite($file, $break); ?>
<?php fwrite($file, $table); ?>
<?php fwrite($file, $td); ?>
<?php fwrite($file, $row ['Artist']); ?>
<?php fwrite($file, $td2); ?>
<?php fwrite($file, $td); ?>
<?php fwrite($file, $row ['Title']); ?>
<?php fwrite($file, $td2); ?>
<?php fwrite($file, $td); ?>
<?php fwrite($file, $row ['Disc']); ?>
<?php fwrite($file, $td2); ?>
<?php fwrite($file, $td); ?>
<?php fwrite($file, $row ['Era']); ?>
<?php fwrite($file, $td2); ?>
<?php fwrite($file, $td); ?>
<?php fwrite($file, $row ['Date Added']); ?>
<?php fwrite($file, $td2); ?>
<?php fwrite($file, $td); ?>
<?php fwrite($file, $row ['AR']); ?>
<?php fwrite($file, $td2); ?>
<?php fwrite($file, $table2); ?>
<?php fclose($file); ?>



<?php } ?>
</body>
</html>

MattF
11-09-2010, 10:49 PM
Get rid of the single quotes around $id:



$query = "SELECT * from `music vidz` where `id` ='$id'";


should be:



$query = "SELECT * from `music vidz` where id=$id";


It's a verified integer, not a string.

Keleth
11-09-2010, 10:51 PM
If you want new content without a reload, you can't do that with PHP alone... need AJAX. As for the blank page, put a link going back to the previous page, or use the PHP header function to redirect it if there's no content on it. Also, there is NO reason for so many PHP code blocks. If two consecutive lines have PHP in them, but them in one block, not 2, let alone 30ish.

okeddy
11-09-2010, 11:47 PM
Thanks for the tips, great idea on the header location redirect back to the database, when I click on a selection it just looks like the page is reloading and you stay where you are. and i will have another page for the printed selections :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum