如何将信息从html表单发送到MySql数据库(XAMPP)

时间:2012-01-13 07:09:53

标签: php javascript mysql html xampp

我非常接近这个迷你项目。这基本上是一项调查,我想知道如何将信息发送到数据库?现在我在Windows Server 2008 R2上安装了XAMPP。我怎么得到:

种族/民族群体(选1):

<ul id="race">
<li><input type="radio" name="race" value="latino" /> Hispanic or Latino</li> <li><input type="radio" name="race" value="white" /> White</li> <li><input type="radio" name="race" value="black" /> Black or African American</li>
<li><input type="radio" name="race" value="asian" /> Asian</li> <li><input type="radio" name="race" value="mraces" /> Two or more Races</li> <li><input type="radio" name="race" value="native"/> Native American Indian/Alaskan Native</li>
<li><input type="radio" name="race" value="pac" /> Native Hawaiian or Other Pacific Islander</li> <li><input type="radio" name="race" value="other" /> Other</li> <br/>
</ul>

进入MySql数据库?我非常接近,但我不知道任何PHP和非常非常有限的Javascript知识。我必须将它包围在表单标签中,然后创建一个提交按钮?最后一步可以帮助任何人吗?非常感谢你!

2 个答案:

答案 0 :(得分:0)

首先,使用以下命令访问MySql数据库服务器:

mysql_connect('localhost', 'userid', 'password');
mysql_select_db(database_name);

然后,您可以使用insert命令来使用MySql服务器的insert语句,如:

$sql=mysql_query("insert into table_name(column_name, column_name2, column_name3) values('value1', 'value2', 'value3')");

答案 1 :(得分:0)

<?php
$username = "your_name";
$password = "your_password";
$hostname = "localhost"; 
$database = "exampleDb";

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) or 
            die("Unable to connect to MySQL");

//select a database to work with
$selected = mysql_select_db($database,$dbhandle) or 
            die("Could not select examples");

 CREATE DATABASE `exampleDb`;
 USE `exampleDb`;
 CREATE TABLE `cars` (
      `id` int UNIQUE NOT NULL,
      `name` varchar(40),
      `year` varchar(50),
      PRIMARY KEY(id)
 );

 INSERT INTO cars VALUES(1,'Mercedes','2000');
 INSERT INTO cars VALUES(2,'BMW','2004');
 INSERT INTO cars VALUES(3,'Audi','2001');    
 ?>