Log in

View Full Version : Error at school project



elwinh
09-05-2007, 02:32 PM
I have a student that came to me with a error he had at a school php project. I searched for the problem, but i could'nt find it.

This is the code:

<?php
// Db gegevens:
$db_user="root";
$db_password="";
$db_host="localhost";
$db_database="school";

// Database connectie:
function database_connect(){
global $db_host, $db_database, $db_user, $db_password;

$db_link = @mysql_connect("$db_host","$db_user","$db_password");
$sql_error = mysql_error();

if (!$db_link) {
echo "Er heeft zich een fout opgetreden met het verbinden met de database.<br>";
echo "$sql_error";
exit;
}

if (!@mysql_select_db("$db_database")) {;
echo "De database kan niet worden geselecteerd.";
exit;
}
return $link;
}

$artist_naam = $_POST['artist_naam'];

if(empty($artist_naam)){
echo("vul eerst de gegevens van het <a href=\"muziek.php\">nummer</a> in");
exit();
}else{

// Start de database connectie:
database_connect();

//kijken of de artiest al in de database aanwezig is
$query = mysql_query("SELECT * FROM tabel_artist WHERE artist_naam = '".$artist_naam."';") or die ("FOUT1:".mysql_error());

// controleer eerst of er records werden gevonden
$rij = mysql_fetch_array($query) or die ("FOUT2:" . mysql_error());
$teller = count($rij);

// Als de teller wat heeft gevonden, laat gegevens zien:
if($teller>=1){

//
for($tel=1; $tel<=$teller; $tel++){
$artist_id_tmp = $rij['artist_id'];
}
} else {

database_connect();

$query ="insert tabel_artist (artist_id, artist_naam, artist_nat)";
$query .="values('";
$query .= $_POST["artist_id"]."','";
$query .= $_POST["artist_naam"]."','";
$query .= $_POST["artist_nat"]."');";
$result = mysql_query($query)or die("FOUT:" . mysql_error());

$artist_naam_tmp = $_POST["artist_naam"];

$query = "SELECT * FROM tabel_artist WHERE artist_naam = '".$artist_naam_tmp."';";
$query = mysql_query($query, $db)or die("FOUT:" . mysql_error());
$query = mysql_fetch_object($query)or die("FOUT:" . mysql_error());
}

database_connect();

$query ="insert muziek (media_id, titel, beoordeling, gerne, medium, artiest)" ;
$query .="values('";
$query .= $_POST["media_id"]."','";
$query .= $_POST["titel"]."','";
$query .= $_POST["beoordeling"]."','";
$query .= $_POST["gerne"]."','";
$query .= $_POST["medium"]."','";
$query .= $artist_id_tmp."');";
$result=mysql_query($query)or die("FOUT:" . mysql_error());


}
?>
<html>
<head>
<title>nummer invoegen</title>
</head>
<body bgcolor="#d3d3d3">
<?php
echo("U heeft de volgende gegevens succesvol ingevoegd:<BR>\n");


echo("Artistnaam:<b>".$_POST["artist_naam"]."</b><br>");
echo("land:<b>".$_POST["artist_nat"]."</b><br>");
echo("Titel:<b>".$_POST["titel"]."</b><br>");
echo("Beoordeling:<b>".$_POST["beoordeling"]."</b><br>");
echo("Gerne:<b>".$_POST["gerne"]."</b><br>");
echo("medium:<b>".$_POST["medium"]."</b><br>");
echo("<hr>|<a href=\"muziek.php\">Nog een nummer toevoegen</a>|<br>|<a href=\"overzicht.php\">overzicht alle nummers in database</a>|");
?>
</body>

We get this error:

FOUT2:

No errors or anything that could be usefull for solving the problem.

(I know this php has some dangerous holes, and i repeat that its not writed by me, nor will it ever come online.)

Can some1 help finding the errors?

thetestingsite
09-05-2007, 02:44 PM
The error lies on this line:



$query ="insert muziek (media_id, titel, beoordeling, gerne, medium, artiest)" ;


It should be:



$query ="insert INTO muziek (media_id, titel, beoordeling, gerne, medium, artiest)" ;


Hope this helps.

elwinh
09-05-2007, 02:47 PM
Ah..that's one error. But not the error wich crashes the script at this point. The error is almost at the top of the page. (remember me saying: "Fout2:")

But ty for the reply...

boogyman
09-05-2007, 02:56 PM
the problem is that it cannot complete the query, which is caused from not connecting properly.



return $link;


no where in your connect function do you declare = assign a value to this variable

elwinh
09-05-2007, 03:16 PM
eddited....

still got the mssg.

It will connect, even if i have that error in it. had it ad a other page and that 1 still connects.

If the connection whas teh problem, i would have had a msg in mysql_error(). By the fact that i did'nt, it could'nt be the mysql connection.

But also ty for your mssg.

thetestingsite
09-05-2007, 03:30 PM
Here you go:



$query = mysql_query("SELECT * FROM tabel_artist WHERE artist_naam = '".$artist_naam."';") or die ("FOUT1:".mysql_error());


That semicolon should not be there.

elwinh
09-05-2007, 04:19 PM
Ty, removed the semicolon, still got the error mssg.

I thank you for your mssg, but it still won't work :(