Learning PHP, need some help

If it's yes/no, use a checkbox; if it's some other kind of restricted input, use either a set of radio buttons or a select box, depending on the information you're displaying. For something like a survey, where you have "Strongly Agree/Agree/Disagree/Strongly Disagree" for example, radio buttons are appropriate; most other things, however, will be better suited to a select box, particularly if there are large amounts of data.

Also, why use "yes" and "no"? Use a TINYINT; 1 for yes, 0 for no. Less overhead (and it's just what mySQL's bool type does anyway) and much easier to work with (just do if($row['foo']) instead of if($row['foo'] == 'yes')).
 
Last edited:
Right, thanks for the help guys :) I think i'm confusing myself now though :o I've got the form done which inserts the 'problem' data into the database but am struggling to modify the value of the 'done' column for a specific row in the admin interface (See below) and to make it all worse the damn table won't center in CSS :( This is what it looks like:

Click

I would really appreciate it if any of you could give me an idea on how to initiate the SQL UPDATE command (Understand SQL more now- spent last night reading up on it at w3school :D ) by a button or similar as you can see.

Thanks

Ben
 
Last edited:
I've managed to get the update function working now. It's not very effecient though so I'm looking for a different way of doing things. At the moment, I have put two radio buttons with the name $row[id] and the values of done or delete. I've then passed this information to another form by GET and then had to say:

Code:
if ($_GET['1'] = done) {
mysql_query("
UPDATE help_desk
SET done = 'yes'
WHERE id = 1
");
} else {
};

And I have to do that for each one which is ridiculous :( Can anyone help me with a more effecient way? :)

Thanks

Ben
 
Trigger said:
Code:
if ($_GET['1'] = done)

The assignment operator in PHP is '=' the comparison operator is '=='.

Trigger said:
Code:
 else {
};

You don't need that if it's going to be empty, also you don't need a semicolon there.

If I were you I'd have a file named 'actions.php' or similar, pass it an 'action' and the primary key of the row you want to do something with:

Code:
<?php
switch($_GET['action']) {
	case 'delete':
		// SQL stuff here...
		header('Location: backWhereTheyCameFrom.php');
		break;    // break is unecessary here, but leave it in.
	case 'completed':
		mysql_query('UPDATE help_desk ' .
			    'SET done = "yes" ' .
			    'WHERE id = '.intval($_GET['id']);
		header('Location: backWhereTheyCameFrom.php');
		break;

	// etc, etc...

}
?>

Example URL: actions.php?action=completed&id=3
 
Last edited:
Trigger said:
Right, thanks :) I've thought about this before, but what if the user checks a radio box in more than one row?

Thanks

Ben

The way I was suggesting doing it was to have a link instead of a form. You click the link to the action, it does it's magic and redirects the user back. Okay, it's a little bit expensive performance wise but considering the projects simplicity and small number of users I wouldn't worry about it.
 
So what would I need to put into the Action column?

Code:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

	<title>Ruskin Sports College .::. HelpDesk™</title>
	<link rel="stylesheet" type="text/css" href="css/layout.css" />
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

</head>

<body>




<div id="login">
	
	<p class="logintitle">
	
		Ruskin Sports College .::. HelpDesk™ Administrator Interface
		
	</p>
	
	<p class="viewdesk" align="center">
	
<?php

// Create Database Variables:

$host = "localhost";
$user = "******";
$pass = "********";
$dbase = "helpdesk";

// Connect to Database:

$connection = mysql_connect($host, $user, $pass) or die('Could not connect to Database');

// Select Database:

mysql_select_db($dbase) or die('Could not find selected database');

$query = "SELECT * FROM help_desk";

$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());


// see if any rows were returned

if (mysql_num_rows($result) > 0) {
    // yes
    // print them one after another
    echo "<center";
    echo "<center><font color=green><div id=tables><table cellpadding=10 border=1 align=center>";
    
        echo "<tr>";
        echo "<td><font color=red> ID Number</font></td>";
        echo "<td><font color=red> Submitted by User</font></td>";
        echo "<td><font color=red> Problem Location</font></td>";
        echo "<td><font color=red> Type </font></td>";
        echo "<td><font color=red> Urgent </td>";
        echo "<td><font color=red> Problem Description </font></td>";
        echo "<td><font color=purple> Done </font></td>";
        echo "<td><font color=red> Actions </font></td>";
        echo "</tr>";
        while($row = mysql_fetch_array($result)) {
        $id = $row[id];
        global $id;
        global $row;
        echo "<tr>";
        $urgent = $row[urgent];
        $done = $row[done];  
        echo "<td>" . $row[id]."</td>";
        echo "<td>" . $row[user]."</td>";
        echo "<td>".$row[location]."</td>";
        echo "<td>".$row[type]."</td>";
        if ($urgent == "Yes") {
        echo "<td><font color=red><img src=img/urgent.jpg></img></font></td>";
        }
        elseif ($urgent == "No") {
        echo "<td><font color=blue>". $urgent; "</td>";
        } else {
        }     
        echo "<td>".$row[problem]."</td>";
        if ($done == "yes") {
        echo "<td><font color=red><img src=img/done.jpg></img></font></td>";
        }
        elseif ($done == "no") {
        echo "<td><font color=blue><img src=img/cross.gif></img></font></td>";
        } else {
        }  
        echo "<td><form action='u8w6y58irfhfiwe794.php' method=get><font face=tahoma color=blue>Done:<input type=radio name='$row[id]' value=done><br />Delete:<input type=radio name='$row[id]' value=delete></font></td>";
        echo "</tr>";
    }
    
    echo "</table></div>";
}
else {
    // no
    // print status message
    echo "No New Problems have been submitted to the HelpDesk!";
}

$row = mysql_fetch_array($result);
$id = $row[id];
echo $id;




function updatedone($idacc) { mysql_query("
UPDATE help_desk
SET done = 'yes'
WHERE id = '$idacc'
");
};

updatedone(3);


echo $row;
echo "</p><p>";
echo "<input type=submit value=Update onClick=alert('Are you sure?')</input>";


// free result set memory
mysql_free_result($result);

// close connection
mysql_close($connection);

?>

</p>

<p>

<form>
<input type="button" value="Return to Main Menu" onClick="window.location='d4ttg4689hjyhjjsjyhweqwlkml.php'""></input>

</form>

</p>
<br /><br />

</body>
</html>

That's the code for the table that displays all the items submitted to the help desk- I know it's messy, its still got bits of code in that don't do anything now so I appologise for that :o Really don't know how to do the 'Done' and 'Delete' bits though. Sorry for being a n00b :(

Thanks for the help

Ben
 
Trigger said:

Do you see my point about comments? They're showing you, the reader of the tutorial, what each segment of code is doing. Once you've learned the language (which you will, quickly) such comments become tiresome and unnecessary, so it's better to leave them out :)
 
robmiller said:
Do you see my point about comments? They're showing you, the reader of the tutorial, what each segment of code is doing. Once you've learned the language (which you will, quickly) such comments become tiresome and unnecessary, so it's better to leave them out :)


Ahh right, I thought it was supposed to be done like that :o I'll take them out in future :) Took it into school today and tried to install XAMPP and the MySQL service won't bloddy start! I managed to get MySQL 5 directly installed but if I try and access http://localhost it just comes up 'The page cannot be displayed blah blah... Connection Refused!' Any ideas why it would do this? It's not on the proxy server, it's on an internal machine.

Thanks

Ben
 
Back
Top Bottom