将表添加到现有的php / html表单中

时间:2015-05-06 10:43:33

标签: php html mysql

我想将这些表添加到原始数据库中,我应该对HTML / PHP代码进行哪些更改?

the new tables SQL codes:

CREATE TABLE IF NOT EXISTS `stores` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `description` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

INSERT INTO `stores` (`id`, `name`, `description`) VALUES
(1, 'Test Store', 'Some description'),
(2, 'Some Other Store', 'Some other description');


CREATE TABLE IF NOT EXISTS `store_hours` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `store_id` int(11) NOT NULL,
  `day` int(11) NOT NULL,
  `time_open` time NOT NULL,
  `time_close` time NOT NULL,
  PRIMARY KEY (`id`),
  KEY `store_id` (`store_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;


ALTER TABLE `store_hours`
  ADD CONSTRAINT `store_hours_ibfk_1` FOREIGN KEY (`store_id`) REFERENCES `stores` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

第二部分..

SELECT
  stores.name AS store_name,
  stores.description AS store_description,
  store_hours.day AS store_hours_day,
  TIME(store_hours.time_open) AS store_open,
  TIME(store_hours.time_close) AS store_close
FROM
  stores
JOIN
  store_hours
ON
  store_hours.store_id = stores.id

我的原始表PHP代码:(它与html代码和它存储的数据库配合良好)

 <?php


include ("../Connections/Connection.php");



if (isset($_POST["submit"]))
{
    $name = $_POST["name"];   
    $type = $_POST["type"];
    $address = $_POST["address"];
    $email = $_POST["email"];
    $phone = $_POST["phone"];
    $lng = $_POST["lng"];
    $lat = $_POST["lat"];
    $opening = $_POST["opening"];
    $closing = $_POST["closing"];

$sql = "INSERT INTO markers (name, type, address, email, phone, lng, lat, opening, closing)
        VALUES('$name', '$type', '$address', '$email', '$phone', '$lng', '$lat', '$opening', '$closing')";

$query = mysql_query($sql);

if (!$query)
{
    die ("Error : " .  mysql_error());
}
if(empty($name) || empty($type) || empty($address) || empty($email) || empty($phone) || empty($lng) || empty($lat) || empty($opening) || empty($closing))
    {
    echo "You did not fill out the required fields.";
    die();  // Note this
    }
echo "<center></center>";
}

?>

如何将新的两个表添加到我的HTML表单(客户端) 和我的PHP代码(后端)(服务器端)??

1 个答案:

答案 0 :(得分:0)

这是PHP代码,它将创建stores表:

$sql = "CREATE TABLE IF NOT EXISTS `stores` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(255) NOT NULL,
    `description` text NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;";

$query = mysql_query($sql);

这里没有什么神奇的事情发生。您可以像上面一样简单地传递SQL代码来创建新表。