$host = "localhost" ;
$user = "duck" ;
$pass = "lalala" ;
$dbas = "quack" ;
// Connect to the mysql server and select the database.
// Note: Let the interpreter give you error messages.
$link = mysql_connect( $host , $user , $pass ) ;
mysql_select_db( $dbas , $link ) ;
// Decide what table you're going to work with on the basis of $table whose value came from the html form.
// Decide whether you are goind go add to the table or read from it on the basis of $action whose valued came from the html form.
// The values that get put into the table are $first, $second, $third which also came from the html form.
switch ( $table ) {
case "animal" :
if ( $action == "add" ) {
echo "doing add animal: $first $second -- " ;
$query = "INSERT INTO Animal_Sounds ( Animal, Sound ) VALUES ( '$first' , '$second' ) ;" ;
} else {
$query = "SELECT * FROM Animal_Sounds ;" ;
}
break ;
case "vegetable" :
if ( $action == "add" ) {
echo "doing add vegetable: $first $second -- " ;
$query = "INSERT INTO Vegetable_Colors ( Vegetable, Color ) VALUES ( '$first' , '$second' ) ;" ;
} else {
$query = "SELECT * FROM Vegetable_Colors ;" ;
}
break ;
case "mineral" :
if ( $action == "add" ) {
echo "doing add minearal: $first $second $third -- " ;
$query = "INSERT INTO Mineral_Values ( Mineral, Price, Value ) VALUES ( '$first' , '$second' , '$third' ) ;" ;
} else {
$query = "SELECT * FROM Mineral_Values ;" ;
}
break ;
default :
echo "wtf?
\n" ;
break ;
}
//The above only got the $query string ready --- the following makes the query and checks for errors.
$result = mysql_query( $query, $link ) ;
if ( $result ) {
if ( $action == "add" ) {
echo "success
\n" ;
}
} else {
echo mysql_error($link) . "
\n" ;
}
//Make the result pretty and print it out, if we are looking at the results.
if ( $action == "see" ) {
$num_rows = mysql_num_rows( $result ) ;
echo "\n" ;
while ( $a_row = mysql_fetch_row( $result ) ) {
echo "\n" ;
foreach ( $a_row as $field ) {
echo " | $field | \n" ;
}
echo "
\n" ;
}
echo "
\n" ;
}
//Close the link
mysql_close( $link ) ;
?>
Back