Click to See Complete Forum and Search --> : SQL Querrie 3


scottyrob
12-22-2005, 10:49 AM
<?php
require_once("check_login.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Our List of Jokes</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
<link href="main.css" rel="stylehseet" type="text/css" />

<style type="text/css">
body {
font-family:Verdana, Arial, Helvetica, sans-serif
}
.details {
display: none;
}
.alldetails p a {
margin-left: 5em;
}
</style>

<script type="text/javascript">
function showDetails(personnumber,button){
if(button.value == '+'){
document.getElementById(personnumber).style.display = 'block';
button.value = '-';
}else{
document.getElementById(personnumber).style.display = 'none';
button.value = '+';
}
}

</script>
<noscript>
<style type="text/css">
.details {
display: block;
}
</style>
</noscript>
</head>

<body>

<?php if (isset($_GET['adddetails'])): // User wants to add a joke
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>
<label>Add Details<br />
<input name="ID" type="text" id="ID" value="" size="40" />
ID</label>
<br /> <label> <br />
<input name="Type" type="text" value="" size="40" />
Type</label>
</p>
<p>
<label>
<input name="Forename" type="text" value="" size="40" />
</label>
Forename
</p>
<p>
<label>
<input name="Surname" type="text" value="" size="40" />
</label>
Surname</p>
<p>
<label>
<input name="Address" type="text" value="" size="40" />
Address
</label>
</p>
<p>
<label>
<input name="Town" type="text" value="" size="40" />
</label>
Town</p>
<p>
<label>
<input name="Postcode" type="text" value="" size="40" />
</label>
Postcode</p>
<p>
<label>
<input name="Home" type="text" value="" size="40" />
Home
</label>
</p>
<p>
<label>
<input name="Mobile" type="text" value="" size="40" />
Mobile</label>
</p>
<p>
<label>
<input name="Email" type="text" value="" size="40" />
</label>
Email</p>
<p>ID : 1-10 Commisoner, 11-30 Leaders, 31-40 Admin
<label> </label>
</p>
<p><br />
<input type="submit" name="SUBMIT" value="SUBMIT" />
</p>
</form>

<?php else: // Default page display

// Connect to the database server
$dbcnx = @mysql_connect('localhost', ' ', ' ');
if (!$dbcnx) {
exit('<p>Unable to connect to the ' .
'database server at this time.</p>');
}

// Select the jokes database
if (!@mysql_select_db(' )) {
exit('<p>Unable to locate the joke ' .
'database at this time.</p>');
}

// If a joke has been submitted,
// add it to the database.
if (isset($_POST['SUBMIT'])) {
$sql = "INSERT INTO `Contacts` (`ID`,`Type`,`Forename`,`Surname`,`Address`,`Town`,`Postcode`,`Home`,`Mobile`,`Email`)
VALUES ('{$_POST['ID']}','{$_POST['Type']}','{$_POST['Forename']}','{$_POST['Surname']}','{$_POST['Address']}','{$_POST['Town']}','{$_POST['Postcode']}','{$_POST['Home']}','{$_POST['Mobile']}','{$_POST['Email']}')";
/* I only specified these values as they are the only inputs I can see in the form you supplied, if in fact you also wanted to add the other fields this would need changing */
echo $sql; // test to see if the sql is correct
if (@mysql_query($sql)) {
echo '<p>Your name has been added.</p>';
} else {
echo '<p>Error adding submitted name: ' .
mysql_error() . '</p>';
}
}
echo '<p>Here are all the jokes in our database:</p>';

// Request the text of all the jokes
$result = @mysql_query('SELECT * FROM `Contacts` GROUP BY `ID`'); /* YOU MAY NEED TO EDIT THIS ID SO THAT
IT IS THE SAME AS THE FIELD NAME
IN YOUR DATABASE */
if (!$result) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
}
$num = mysql_num_rows($result);
echo "<div id=\"alldetails\">";
for ($i=0; $i<$num; $i++)
{
$row = mysql_fetch_row($result);
echo "<p><input type=\"button\" name=\"person$i\" value=\"+\" onclick=\"showDetails('person$i',this);return true;\"> $row[2] $row[3] <a href=\"mailto:$row[9]\">$row[9]</a></p>\n";
echo "<p id=\"person$i\" class=\"details\">
Position: $row[1]<br>\n
Name: $row[2] $row[3]<br>\n
Address: $row[4]<br>\n
Town: $row[5]<br>\n
Postcode: $row[6]<br>\n
Home Phone: $row[7]<br>\n
Mobile: $row[8]<br>\n
Email: $row[9]</p>";
}
echo "</div>";

// When clicked, this link will load this page
// with the joke submission form displayed.
echo '<p><a href="' . $_SERVER['PHP_SELF'] .
'?adddetails=1">Add Details</a></p>';

endif;
?>
</body>
</html

How do i create a new querrie that will show the ID, First Name and second name?

chazzy
12-22-2005, 11:32 AM
$querrie = "SELECT `id`,`first_name`,`second_name` FROM `your_table`";
$querrie_result = mysql_query($querrie);
printf("<table>\n");
while($result_row = mysql_fetch_array($querrie_result,MYSQL_NUM)){
printf("<tr>");
foreach($result_row as $result_column){
printf("<td>".$result_column."</td>");
}//foreach
printf("</tr>\n");
}//while
printf("</table>");


that will give you a nice tabular report for your querrie.