PDA

View Full Version : Error connecting to the host server


nindoja
01-25-2003, 04:02 PM
Whenever I try to upload data to a database using forms, it doesn't do anything. It just reloads the page. When I check to see if it uploaded to the database, it doesn't. Here is the code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<?php
$db = mysql_connect("localhost","fryshane","******") or die("Cannot connect to the database.");
mysql_select_db("fryshane_uk_db",$db);
?>
</head>
<body>

<b>Add Information to your plan file</b>
<form name="plan" method="post" action="<?php echo $PHP_SELF; ?>">
Date:
<input type="text" name="form_date">
<br>
Title: <input type="text" name="form_titre">
<br>
Plan update:<br>
<textarea name="form_corps" cols="100" rows="10"></textarea><br>
<input type="submit" name="Submit" value="Submit">
<?php
$sql = "INSERT INTO 'Jonathan' SET auteur='Jonathan', date='$date', titre='$titre' corps='$corps'";
$query = mysql_query($sql) or die("Cannot query the database.");
echo "Database Updated.";
?>
</form>

</body>
</html>:verymad: :grrrr: :confused: :mad

Dynasty
01-25-2003, 05:35 PM
Whenever using forms with well any web scripting language really, whatever you put in the name attribute for each element of the form (eg. <input type="text" name="text"> is also the name of the variable to use in the script.

The reason your script doesn't work is because you are trying to insert values to the database that don't exist.


<?php
$sql = "INSERT INTO 'Jonathan' SET auteur='Jonathan', date='$date', titre='$titre' corps='$corps'";
$query = mysql_query($sql) or die("Cannot query the database.");
echo "Database Updated.";
?>


Should really be done as follows...


<?php

if (isset($form_date) && isset($form_titre) && isset($form_corps))
{
$sql = "INSERT INTO 'Jonathan' SET auteur='Jonathan', date='$form_date', titre='$form_titre' corps='$form_corps'";
$query = mysql_query($sql) or die("Cannot query the database." .mysql_error());
echo "Database Updated.";
}

?>


Now as you see I changed it slightly, always check that the variables have been set before trying to insert into a database. That way you don't get the problem you did ;)

Also when using the die statement with a query, add the .mysql_error it will tell you why it couldn't insert into the database, rather than just "oh I can't do that".

Hope that helps!

nindoja
01-25-2003, 07:47 PM
Thanks, dynasty, but it still doesn't work. Here is the code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<?php
$db = mysql_connect("localhost","fryshane","*******") or die("Cannot connect to the database.");
mysql_select_db("fryshane_uk_db",$db);
?>
</head>
<body>

<b>Add Information to your plan file</b>
<form name="plan" method="post" action="<?php echo $PHP_SELF; ?>">
Date:
<input type="text" name="date">
<br>
Title: <input type="text" name="titre">
<br>
Plan update:<br>
<textarea name="corps" cols="100" rows="10"></textarea><br>
<input type="submit" name="Submit" value="Submit">
<?php

if (isset($date) && isset($titre) && isset($corps))
{
$sql = "INSERT INTO 'Jonathan' SET date='$date', titre='$titre' corps='$corps'";
$query = mysql_query($sql) or die("Cannot query the database." .mysql_error());
echo "Database Updated.";
}

?>
</form>

</body>
</html>
I get the following error after filling out the form and pressing submit:
Cannot query the database.You have an error in your SQL syntax near ''Jonathan' SET date='01/25/03', titre='test' corps='testsetsetsetstest'' at line 1

Dynasty
01-25-2003, 08:32 PM
I should really look at things closer next time :P


$sql = "INSERT INTO 'Jonathan' SET date='$date', titre='$titre' corps='$corps'";


That is where your problem is. The code should be...


$sql = "INSERT INTO Jonathan ( date, titre, corps ) VALUES ( $date, $titre, $corps )";


Give that a try, see if it works :P

nindoja
01-26-2003, 08:52 AM
Thanks dynasty, it is getting closer and closer everytime you help me. Here is the code now

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<?php
$db = mysql_connect("localhost","fryshane","********") or die("Cannot connect to the database.");
mysql_select_db("fryshane_uk_db",$db);
?>
</head>
<body>

<b>Add Information to your plan file</b>
<form name="plan" method="post" action="<?php echo $PHP_SELF; ?>">
Date:
<input type="text" name="date">
<br>
Title: <input type="text" name="titre">
<br>
Plan update:<br>
<textarea name="corps" cols="100" rows="10"></textarea><br>
<input type="submit" name="Submit" value="Submit">
<?php

if (isset($date) && isset($titre) && isset($corps))
{
$sql = "INSERT INTO Jonathan ( date, titre, corps ) VALUES ( $date, $titre, $corps )";
$query = mysql_query($sql) or die("Cannot query the database." .mysql_error());
echo "Database Updated.";
}

?>
</form>

</body>
</html>

When I enter data, it says:
Cannot query the database.Unknown column 'test' in 'field list'
What is wrong???

Dynasty
01-26-2003, 10:14 AM
Hmm, it's parsing the values as columns...Oh wait silly me :P


$sql = "INSERT INTO Jonathan ( date, titre, corps ) VALUES ( '$date', '$titre', '$corps' )";



Try that.

jigal
03-08-2003, 12:52 AM
Originally posted by nindoja
INSERT INTO 'Jonathan' SET auteur='Jonathan', date='$date', titre='$titre' corps='$corps'
And nobody really saw that the query just missed one tiny little comma???
INSERT INTO 'Jonathan' SET auteur='Jonathan', date='$date', titre='$titre', corps='$corps'

I personally prefer the SET column=value syntax over the (columns) VALUES (values) syntax.
Just see if you still like it if there are 20 or so columns and values to be set and you need to change one...

Jigal.

Dynasty
03-08-2003, 04:11 PM
That's the thing I do still like the columns, values way better. For one thing it looks a hell of a lot neater, and for another I don't think things like Oracle like the set way of doing things. Or at least I've never tried to see if it does :P