Web Hosting Talk







View Full Version : PHP Help (again lol)


Danny159
08-25-2007, 03:59 PM
Hey

I have this script i have just made and i want it to put all the items in the database into a table so i can view then as the admin. However i can't seem to get it to put the data into the table...:confused:

Here is the script is there any way someone can help me :S it brings up the titles to the table but not the data in the table :confused:

<?php
require('../connect.php');
$result = mysql_query("SELECT * FROM voucher WHERE id = '$id' ORDER BY $param",$db);
if (!$param) {
$result = mysql_query("SELECT * FROM voucher WHERE id = '$id' ORDER BY id",$db);
}
echo "<p><table border=1 cellspacing=0 cellpadding=2 bordercolor=#eeeeee width=400>";
echo "<tr align=top><td><b>
Coupon id#</b></td><td><b>
Name</b></td><td><b>
Coupon</b></td><td><b>
Amount</b></td></tr>";
while ($row = mysql_fetch_array($result))
{
$id = $row["id"];
$name = $row["name"];
$code = $row["code"];
$amount = $row["amount"];

echo "<tr><td>
$id</td><td>
$name</td><td>
$code</td><td>
$amount</td></tr>";
}
echo "</table>";
?>

Steve_Arm
08-25-2007, 04:30 PM
Why don't you gather all your errors and put them in a thread, after one hour :D ?

Because you are selecting only one item that doesn't exist in the table? ($_GET['id'])

Danny159
08-25-2007, 04:38 PM
ok so i have an id in my database but thats a auto_increment so if i make another id called id2 then that will then work?

(sorry im still lerning php a mate was teaching me php lol and im still abit slow on errors)

Jamie Edwards
08-25-2007, 04:41 PM
$result = mysql_query("SELECT * FROM voucher WHERE id = '$id' ORDER BY $param",$db);
Where is $id being set? What does it contain?

Danny159
08-25-2007, 04:46 PM
I have now changed this to:

$result = mysql_query("SELECT * FROM voucher WHERE name = '$name' ORDER BY $param",$db);
if (!$param) {
$result = mysql_query("SELECT * FROM voucher WHERE id = '$id' ORDER BY id",$db);

but it is an auto_increment so i just counts up as new data is added...

Danny159
08-25-2007, 05:05 PM
All i rearly want is to be able to view the data in my database in a table on a webpage

Here is a list of what is is my 'voucher' table

id - auto_increment id number
id2 - hand imput id
name - name of voucher
code - voucher code
amount - amount you get off

is that php script right for what i want it to do? :confused:

Steve_Arm
08-25-2007, 05:09 PM
Basically if you want to retrieve ALL the data from the table, you don't set a select criteria.
It should be "SELECT * FROM voucher ORDER BY name ASC"

And:

$param = 'id';
if (isset($_GET['param'])) $param = $_GET['param'];
$result = mysql_query("SELECT * FROM voucher WHERE name = '$name' ORDER BY ".$param, $db);

Steve_Arm
08-25-2007, 05:15 PM
And to avoid getting hacked again.

$param = '';
switch ($_GET['param'])
{
case 'id': $param = 'id';
break;
case 'name': $param = 'name';
break;
default: $param = 'id';
break;
}
$result = mysql_query("SELECT * FROM voucher WHERE name = '$name' ORDER BY ".$param, $db);

Danny159
08-26-2007, 04:39 AM
Yes thanks :) its working fine now and i have added your anti hack :agree: top man
thanls guys :agree: