...

View Full Version : Problem w/ HTML form



Smartmonkey
06-05-2005, 10:35 PM
I am trying to insert data from this html form into a mysql database.. however whenever i hit submit I only receive a blank page.... and when I check phpmyadmin there are no rows added....

heres my html doc:




<html>
<body>

<form method="post" action="test.php">
<fieldset>
<legend> Reserve a Copy </legend>

<label for="name">Name:<input type="text" name="name" id="name" /></label><br />
<label for="address">Address:<input type="text" name="address" id="address" /></label><br />
<label for="street">Street:<input type="text" name="street" id="street" /></label><br />
<label for="state">State:<input type="text" name="state" id="state" /></label><br />
<label for="zip">Zip:<input type="text" name="zip" id="zip" /></label><br />
<label for="email">Email:<input type="text" name="email" id="email" /></label><br />
<label for="tel">Telephone:<input type="text" name="tel" id="tel" /></label><p><br /></p><p>
<label for="comments">Comments:<textarea name="comments" id="comments" /></textarea></label><br />
</p><p>&nbsp;</p><p>&nbsp;</p><p><input type="submit" value="Submit" class="submit" />

</TD>
</TR>
<TR>
</fieldset>



</table>
</form>
</html>
</body>


and here is the php doc.. 'test.php'




<?php
//Change the user and pw to mySql name and password
$hostname = 'localhost'
$user = 'root'
$password = 'xxxx
$database = 'derbyworld'
$table = 'info'

//connect to db, and select db
mysql_connect($hostname,$user,$password) or die(Could not connect to $database);
mysql_select_db($database) or or die(mysql_error());

//convert all the posts to variables
$name = $_POST['name'];
$address = $_POST['addres'];
street = $_POST['street'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$comment = $_POST['comments'];

//table = derbyworld
//table columns = name,address,street,state,zip,email,tel,comments,id
$sqlquery = mysql_query("INSERT INTO $table
values'$_POST[name]', '$_POST[address]', '$_POST[street]', '$_POST[state]', '$_POST[street]', '$_POST[zip]', '$_POST[email]',
'$_POST[tel]', '$_POST[comments]')";

$sqlquery = mysql_query($'sqlquery');
mysql_close();



//confirm
echo "Query Finished";
}
?>



anyone know what im doing wrong?? I tried to get $php_self to work but it gave me a bad request...

I am a php newb and I would appreciate any insight as to what im doing wrong.

Thanks a bunch

Brandoe85
06-05-2005, 11:53 PM
Couple things, you forgot a $ sign when declaring your 'street' variable, also, you were running mysql_query() twice? Also, what is the field id? is it an auto increment field? but assuming that it is, you can try this out:


<?php
//Change the user and pw to mySql name and password
$hostname = 'localhost'
$user = 'root'
$password = 'xxxx
$database = 'derbyworld'
$table = 'info'

//connect to db, and select db
mysql_connect($hostname,$user,$password) or die(Could not connect to $database);
mysql_select_db($database) or or die(mysql_error());

//convert all the posts to variables
$name = $_POST['name'];
$address = $_POST['addres'];
$street = $_POST['street'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$comment = $_POST['comments'];

//table = derbyworld
//table columns = name,address,street,state,zip,email,tel,comments,id

$sqlquery = mysql_query("INSERT INTO $table (name, address, street, state, zip, email, tel, comments) values '$name', '$address', '$street', '$state', '$zip', '$email', '$tel', '$comment'") or die(mysql_error());

if(!$sqlquery)
{
echo 'Error';
}
else
{
echo 'Query was good';
}

?>

Smartmonkey
06-06-2005, 06:06 AM
oops forgot to take those out.. but i tried what you had posted and I still just get a blank page.. is the html ok? should I be using get rather then post? in the sql query do i need to do it like:



$sqlquery = mysql_query("INSERT INTO $table (name, address, street, state, zip, email, tel, comments) values '$_POST['name];


the id column is autoincrement with int(11)..

Thanks for your quick reply

Yakisoba
06-06-2005, 06:34 AM
Not sure if this is the error or just a typo but in your code you have...



//table = derbyworld
//table columns = name,address,street,state,zip,email,tel,comments,id

$sqlquery = mysql_query("INSERT INTO $table (name, address, street, state, zip, email, tel, comments) values '$name', '$address', '$street', '$state', '$zip', '$email', '$tel', '$comment'") or die(mysql_error());


so I assume that your table name is 'derbyworld' (based on the first comment)

but above you have $table = 'info';... (and you need to close the quote for the value in $password)


$hostname = 'localhost'
$user = 'root'
$password = 'xxxx
$database = 'derbyworld'
$table = 'info'


shouldn't it be $table = 'derbyworld'; ?
It also appears that you're missing ";" at the end of your lines.

try this:


$hostname = 'localhost';
$user = 'root';
$password = 'xxxx';
$database = 'derbyworld';
$table = 'derbyworld';



Yak

Smartmonkey
06-06-2005, 07:54 AM
oops i meant that to say database name, but yeah the db is derbyworld, table is info.

I added the ';' to the end and I still get the same thing.... is there a reason that I don't get the mysql_error()) ?? I just get a blank white page.. doesn't appear to do anything. I believe all the typo's should be fixed heh.. but heres what I have now:



<?php
//Username / PW
$hostname = 'localhost';
$user = 'root';
$password = 'password';
$database = 'derbyworld';
$table = 'info';

//connect to db, and select db
mysql_connect($hostname,$user,$password) or die(Could not connect to $database);
mysql_select_db($database) or or die(mysql_error());

//convert all the posts to variables
$name = $_POST['name'];
$address = $_POST['address'];
$street = $_POST['street'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$comments = $_POST['comments'];

//database = derbyworld
//table = info
//table columns = name,address,street,state,zip,email,tel,comments,id

$sqlquery = mysql_query("INSERT INTO $table (name, address, street, state, zip, email, tel, comments) values '$name', '$address', '$street', '$state', '$zip', '$email', '$tel', '$comment'") or die(mysql_error());

if(!$sqlquery)
{
echo 'Error';
}
else
{
echo 'Query was good';
}

?>


and this was the sql I used to create the 'info' table:



SQL-query:
CREATE TABLE `info` (
`address` VARCHAR( 100 ) NOT NULL ,
`comments` VARCHAR( 500 ) NOT NULL ,
`email` VARCHAR( 50 ) NOT NULL ,
`name` VARCHAR( 50 ) NOT NULL ,
`state` VARCHAR( 10 ) NOT NULL ,
`street` VARCHAR( 100 ) NOT NULL ,
`tel` VARCHAR( 12 ) NOT NULL ,
`zip` VARCHAR( 7 ) NOT NULL ,
`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
UNIQUE (
`id`
)
);

Yakisoba
06-06-2005, 08:16 AM
this may be another typo...

you have "or" written twice, and you need to put quotes around "Could not connect to $database"



//connect to db, and select db
mysql_connect($hostname,$user,$password) or die(Could not connect to $database);
mysql_select_db($database) or or die(mysql_error());


did you mean:


//connect to db, and select db
mysql_connect($hostname,$user,$password) or die('Could not connect to $database');
mysql_select_db($database) or die(mysql_error());


I order to get mysql_error on a webpage you have to "echo" it (i think).

i.e.


mysql_select_db($database) or die(echo mysql_error());


give that a shot

Yak

Smartmonkey
06-06-2005, 08:24 AM
well that at least got me:



You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''asd', 'asda', 'asd', 'asd', 'asd', 'asd', 'asd', ''' at line 1


I was just putting in garbage for the info.. but is it my query that is messed up do I need to do


$sqlquery = mysql_query("INSERT INTO $table (name, address, street, state, zip, email, tel, comments) values '$POST_['name'],



?? I appreciate your help :)

Yakisoba
06-06-2005, 08:34 AM
try this...



$sqlquery = mysql_query("INSERT INTO $table (name, address, street, state, zip, email, tel, comments) VALUES ('$name', '$address', '$street', '$state', '$zip', '$email', '$tel', '$comment')";


make sure all the col names and variable names I typed are correct.

Yak

Kurashu
06-06-2005, 04:57 PM
Also, tacking error_reporting(E_ALL) at the top of your script is helpful for tracking down errors.

Smartmonkey
06-06-2005, 06:47 PM
Hrm, tried your sqlquery and I don't get anything anymore.. just a blankpage

I also added




// error reporting
error_reporting(E_ALL);


at the top of my script and still a blank page..

Brandoe85
06-07-2005, 12:25 AM
I had some typo's, but try this out:
html form:


<html>
<body>

<form method="post" action="test.php">
<fieldset>
<legend> Reserve a Copy </legend>

<label for="name">Name:<input type="text" name="name" id="name" /></label><br />
<label for="address">Address:<input type="text" name="address" id="address" /></label><br />
<label for="street">Street:<input type="text" name="street" id="street" /></label><br />
<label for="state">State:<input type="text" name="state" id="state" /></label><br />
<label for="zip">Zip:<input type="text" name="zip" id="zip" /></label><br />
<label for="email">Email:<input type="text" name="email" id="email" /></label><br />
<label for="tel">Telephone:<input type="text" name="tel" id="tel" /></label><p><br /></p><p>
<label for="comments">Comments:<textarea name="comments" id="comments" /></textarea></label><br />
</p><p>&nbsp;</p><p>&nbsp;</p><p><input type="submit" value="Submit" class="submit" />

</TD>
</TR>
<TR>
</fieldset>



</table>
</form>
</html>
</body>

php page:


<?php
//Change the user and pw to mySql name and password
$hostname = 'localhost';
$user = 'root';
$password = 'your password';
$database = 'derbyworld';
$table = 'info';

//connect to db, and select db
mysql_connect($hostname,$user,$password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());

//convert all the posts to variables
$name = $_POST['name'];
$address = $_POST['address'];
$street = $_POST['street'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$comment = $_POST['comments'];

//table = derbyworld
//table columns = name,address,street,state,zip,email,tel,comments,id


$query = "INSERT INTO $table (name, address, street, state, zip, email, tel, comments) values('$name', '$address', '$street', '$state', '$zip', '$email', '$tel', '$comment')";
$sqlquery = mysql_query($query) or die(mysql_error());

if(!$sqlquery)
{
echo 'Error';
}
else
{
echo 'Query was good';
}

?>

Smartmonkey
06-07-2005, 02:46 AM
right on that worked!, thanks for your help.. so what was the problem??

I found the typos in my html form (oops) and the main difference I saw with the php was


query = "INSERT INTO $table (name, address, street, state, zip, email, tel, comments) values('$name', '$address', '$street', '$state', '$zip', '$email', '$tel', '$comment')";
$sqlquery = mysql_query($query) or die(mysql_error());


is that all that you did to fix the problem??

I really appreciate your help with this one :)

also, how could I have gotten this to work with $PHP_SELF?? would I need to change the $query to $PHP_SELF['value'] ??

again thanks alot for your help :)

Brandoe85
06-07-2005, 02:58 AM
Your welcome. Basically, I ended the double quotes too early on the first piece of code I posted, and I just separated it into the $query variable, helps me read it better. You could use PHP_SELF, I take it you want it to submit to itself?

somepage.php:


<html>
<body>

<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
<fieldset>
<legend> Reserve a Copy </legend>

<label for="name">Name:<input type="text" name="name" id="name" /></label><br />
<label for="address">Address:<input type="text" name="address" id="address" /></label><br />
<label for="street">Street:<input type="text" name="street" id="street" /></label><br />
<label for="state">State:<input type="text" name="state" id="state" /></label><br />
<label for="zip">Zip:<input type="text" name="zip" id="zip" /></label><br />
<label for="email">Email:<input type="text" name="email" id="email" /></label><br />
<label for="tel">Telephone:<input type="text" name="tel" id="tel" /></label><p><br /></p><p>
<label for="comments">Comments:<textarea name="comments" id="comments" /></textarea></label><br />
</p><p>&nbsp;</p><p>&nbsp;</p><p><input type="submit" value="Submit" name="sub" class="submit" />

</TD>
</TR>
<TR>
</fieldset>

</table>
</form>
<?php
if(isset($_POST['sub']))
{
//Change the user and pw to mySql name and password
$hostname = 'localhost';
$user = 'root';
$password = 'yourpass';
$database = 'derbyworld';
$table = 'info';

//connect to db, and select db
mysql_connect($hostname,$user,$password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());

//convert all the posts to variables
$name = $_POST['name'];
$address = $_POST['address'];
$street = $_POST['street'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$comment = $_POST['comments'];

//table = derbyworld
//table columns = name,address,street,state,zip,email,tel,comments,id


$query = "INSERT INTO $table (name, address, street, state, zip, email, tel, comments) values('$name', '$address', '$street', '$state', '$zip', '$email', '$tel', '$comment')";
$sqlquery = mysql_query($query) or die(mysql_error());

if(!$sqlquery)
{
echo 'Error';
}
else
{
echo 'Query was good';
}
}
?>
</body>
</html>

Smartmonkey
06-07-2005, 03:05 AM
oh sweet so all I really have to add is:



action="<?php $_SERVER['PHP_SELF'] ?>">


and



if(isset($_POST['sub']))
{

?

oh side note.. what do you use for your php editor? do u know of a good one?

Brandoe85
06-07-2005, 03:09 AM
Yep thats it, and make sure you name your submit button 'sub', I use textpad for my coding, but heres a thread on editors (http://www.codingforums.com/showthread.php?t=57951&page=1)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum