Re: PHP, MySQL problem
| From: | Jan Grafstr�m | Date: | Wed, 14 Jan 2004 19:35:41 +0000 |
| Subject: | Re: PHP, MySQL problem | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-174577@lists.php.net to get a copy of this message | ||
Hi
Add records with this code.
<?php
$name=isset($_POST['name']) ? $_POST['name'] :'';
$address=isset($_POST['address']) ? $_POST['address'] :'';
if (!empty($name)) {
mysql_connect($server,$user,$pass) or die ("Error conecting");
mysql_select_db($dbnamn,$conection) or die ("no db ".$dbnamn);
$query = "insert into mytable (name, address) values ('$name',
'$address')";
$result = mysql_query ($query) or die("bad query");
}
?>
<html>
<head></head>
<body>
<form action="" method="post">
name <input name="name">
address <input name="address">
<input type="submit" value="add record">
</form>
</body></html>
Hope this helps.
Jan
"Nicolai Elmqvist" <nicolai@elmqvist.com> skrev i meddelandet
news:20040114001110.3805.qmail@pb1.pair.com...
> Hi
>
> I have just started working with PHP and MySQL and have gone through 3
> tutorials on how to add and delete records from a database. Nearly
> everything is working, as it should except for the communication between
> HTML <form> and PHP. If I try to add a record to my database by pushing a
> "submit" the text in the textboxes are deleted but no record is inserted
in
> the database. I have copied the code directly form the tutorials so
spelling
> mistakes should not be the problem.
>
>
>
> It is possible to add records manually in the code so the connection to
and
> from the database is ok.
>
>
>
> So, does anybody know what I the problem might be?
>
>
>
> I'm using PHP 4.3.4, MySQL 4.0.17 and an Apache server.
>
>
>
> On before hand, thank you.
>
> Nicolai Elmqvist