Forum Moderators: coopster

Message Too Old, No Replies

PHP/MySQL question

         

Saikou

3:13 am on May 7, 2006 (gmt 0)

10+ Year Member



I've begun to develop a script by myself for once, and I was wondering the best way to do it.

I'm going to be using PHP/JavaScript/MySQL for it. The basic layout is this:

On the website, a user can click a link, JavaScript takes over and furnishes a pop-up window. In this window I would need information to be displayed according to what link they clicked.

So let's start like this...On "index.php" there are three links:

Link A
Link B
Link C

Link A - Contains information about cars, listed in MySQL database "info" as line 1.

Link B - Information on houses, MySQL database "info" as line 2.

Link C - Information on pets, MySQL database "info" as line 3.

I suppose it might help if I show you the coding I'm using..


<script>
var newwindow;
function box(url)
{
newwindow=window.open(url,'name','height=400,width=200');
if (window.focus) {newwindow.focus()}
}
</script>

That would be the javascript segment.

The links are:


<a href="javascript:box('popup.php');">Link</a>

"popup.php" would contain the code for connecting to the MySQL database to retrieve the relevant information.

My only question is...How do I tell it what row in the table to display?

Let's say my table is setup like this:

Column 1 - ID
Column 2 - Name
Column 3 - Description

My only guess would be to use the ID as a reference, but I'm not sure how to do that..So..I think that's my question.

I know this post is probably really confusing, so..If anyone needs clarification, just ask.

Also, after I get some help on that, I was wondering if anyone could lend their knowledge on setting up a so-called "Admin Interface".

This would generally be a form to input data into the MySQL table for the links..I'm guessing it would be something like this:

<?php

$dbhost='localhost';
$dbusername='';
$dbuserpass='';
$dbname='';

mysql_connect ($dbhost, $dbusername, $dbuserpass);

if (!mysql_select_db($dbname)) die(mysql_error());

$id = "";
$name = "";
$desc = "";

$query1 = "INSERT INTO info (id,name,description) VALUES ('".$id."','".$name."','".$desc."')";
mysql_query($query1)or die(mysql_error());
?>

But I'm not sure how to make a web-interface to handle entering the variables. That script would just be run from a browser, but you have to fill in the variables by hand in the script.

Thanks in advance!

Regards,
Ken

dmorison

5:06 am on May 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Ken,

It should just be a case of passing the id of each link to popup.php on the query string - just as form variables are submitted using a form with the "GET" method. Your links would look something like this:

<a href="javascript:box('popup.php?id=1');">Link A</a>
<a href="javascript:box('popup.php?id=2');">Link B</a>
<a href="javascript:box('popup.php?id=3');">Link C</a>

Then, in popup.php, you simply query the database using an SQL statement with a WHERE clause something like:

$sql = "SELECT * FROM info WHERE id=".mysql_escape_string($_GET["id"]);

Strictly speaking the mysql_escape_string function is not required but you should use it because the value of $id is untrusted.

Saikou

5:28 am on May 7, 2006 (gmt 0)

10+ Year Member



I'll try your code out in the morning, does anyone have any input on the form?

P.S. - Thanks, I'll let you know how it goes.

Regards,
Ken

Saikou

7:53 pm on May 7, 2006 (gmt 0)

10+ Year Member



Well I started coding popup.php early this morning but I think I've made a mistake, maybe someone can lend another set of eyes..

<?php

$dbhost='localhost';
$dbusername='#*$!';
$dbuserpass='#*$!';
$dbname='info';

mysql_connect ($dbhost, $dbusername, $dbuserpass);

if (!mysql_select_db($dbname)) die(mysql_error());

$sql = "SELECT * FROM info WHERE id=".mysql_escape_string($_GET["id"])";
$result = mysql_query($query) or die('Error, query failed');
while ($row = mysql_fetch_array($result)) {
echo "<table border='5'>
<tr>
<td>
'.$row['name'].'
</td>
</tr>
<tr>
<td>
'.$row['description'].'
</td>
</tr>
</table>";
}
?>

Just not seeing why it's not working...

Oh yeah..It's not giving errors, it's just...Nothing displays. :)

I guess it would help to let you know I'm running PHP 4.4.0, MySQL 4.1.14, Apache 2.0.54 On Win Server 2003

Slade

8:09 pm on May 7, 2006 (gmt 0)

10+ Year Member



Does the popup appear?

If so, view source on the resulting window and see if there's anything in there.

Saikou

9:12 pm on May 7, 2006 (gmt 0)

10+ Year Member



Popup appears...

No source code.

Saikou

12:44 am on May 8, 2006 (gmt 0)

10+ Year Member



Fixed.

<?php


$dbhost='localhost';
$dbusername='root';
$dbuserpass='password';
$dbname='database';

mysql_connect ($dbhost, $dbusername, $dbuserpass);

if (!mysql_select_db($dbname)) die(mysql_error());

$query = "SELECT * FROM info WHERE id=".mysql_escape_string($_GET["id"]);
$result = mysql_query($query) or die('Error, query failed');
while ($row = mysql_fetch_assoc($result)) {
echo $row['id'];
echo $row['name'];
echo $row['description'];
}



?>

[edited by: coopster at 4:35 pm (utc) on May 8, 2006]
[edit reason] generalized information [/edit]

eelixduppy

12:53 am on May 8, 2006 (gmt 0)



If only i was a moderator, i would change THAT!

Saikou

1:39 am on May 8, 2006 (gmt 0)

10+ Year Member



True, I was in a rush.....time to go change the p/w. :)

wheelie34

10:44 am on May 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$dbusername='root';

Looks like a server login to me, be careful