将此html链接到数据库

时间:2017-10-15 10:30:30

标签: php html sql

我希望这就是你的意思



-- phpMyAdmin SQL Dump
-- version 4.6.6deb5
-- https://www.phpmyadmin.net/
--
-- Host: localhost:3306
-- Generation Time: Oct 15, 2017 at 12:30 PM
-- Server version: 10.1.26-MariaDB-1
-- PHP Version: 7.0.22-3

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `Taheal`
--

-- --------------------------------------------------------

--
-- Table structure for table `test`
--

CREATE TABLE `test` (
  `ID` int(11) NOT NULL,
  `first_name` varchar(255) NOT NULL,
  `Price` int(11) NOT NULL,
  `last_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `item_num` int(11) NOT NULL,
  `Total` int(11) AS (Price*item_num) PERSISTENT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `test`
--
ALTER TABLE `test`
  ADD PRIMARY KEY (`ID`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `test`
--
ALTER TABLE `test`
  MODIFY `ID` int(11) NOT NULL AUTO_INCREMENT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;




这里是给出了正确凭证的connect1.php 名为Taheal的数据库由名为test columns(ID,first_name,Price,last_name,item_num,Total)的表组成,但是当我在html表单上按提交时,它仍然无效



<?php
$servername = "localhost";
$dbusername = "root";
$dbpassword = "Youssef123";
$dbname = "test";

$fname = $_POST['fname']
$lname = $_POST['lname'];
$it_num = $_POST['it_num'];

/** Create connection **/
$conn = new mysqli($servername, $dbusername, $dbpassword, $dbname);

/** 
* Use !empty($var) instead of $var, because is fast and return TRUE only if $var not empty 
* Use urlencode() to generate correct $_GET string
**/
if (!empty($conn->connect_error)) {
    header('location: /form.php?error='.urlencode($conn->connect_error));
    exit; /** Prevent the script from running in background **/
}

if( empty($fname) ) {
    header('location: /form.php?error='.urlencode('fname is empty'));
    exit; /** Prevent the script from running in background **/
}
if( empty($lname) ) {
    header('location: /form.php?error='.urlencode('lname is empty'));
    exit; /** Prevent the script from running in background **/
}

if( empty($it_num) ) {
    header('location: /form.php?error='.urlencode('it_num is empty'));
    exit; /** Prevent the script from running in background **/
} else if( !is_numeric($it_num) ) {
    header('location: /form.php?error='.urlencode('it_num must be a number'));
    exit; /** Prevent the script from running in background **/
}

/**
* Example of db_table_field    :    first_name
* SQL                          :    INSERT INTO test ('first_name') ...
* Use mysql_escape_string() to prevent Injection of JS code, etc, into DB
**/
$SQL = "INSERT INTO test ('first_name', 'last_name', 'item_num') VALUES ('".mysql_escape_string($fname)."', '".mysql_escape_string($lname)."', '".mysql_escape_string($it_num)."')";

/** Use === instead of ==, because It's more secure **/
if ($conn->query($SQL) === TRUE ) {
    header('location: /form.php?success='.urlencode('Thank you for inserting info in the database') );
} else {
    header('location: /form.php?error='.urlencode($conn->error));
}

exit; /** Prevent the script from running in background **/

?>
&#13;
&#13;
&#13;

这是我创建的新form.php

&#13;
&#13;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtm111/DTD/xhtm111.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang=en
<html>
<head> <title>Taheal</title>
</head>
<body bgcolor ="lightyellow">
    <?php if(!empty($_GET['success'])) { ?>
    <div class="SUCCESS_MESSAGE">
        <?php echo $_GET['success']; ?>
    </div>
    <a href="/form.php">New insert</a>
    <?php } else if(!empty($_GET['error'])) { ?>
    <div class="SUCCESS_MESSAGE">
        <?php echo $_GET['success']; ?>
    </div>
    <a href="/form.php">Retry</a>
    <?php } else { ?>
	<form name="consumables" method ="post" action="connect1.php"/>
	<table border = "2" align = "center" bgcolor = "lightblue">
	<tr>
	<td colspan= "2" align = "center">Form</td>
	</tr>

	<tr>
	<td><center><font color = "red" >consumables:</font><center>
	<select type = "text" name = "fname" value =""></center>
		<option value="1">1</option>
		<option value="a">a</option>
		<option value="b">b</option>
		<option value="c">c</option>
	</select>
	</td>
	</tr>

	<tr>
	<td><center><font color="red" >RoomNum:</font><center>
	<select type="text" name="lname" value=""/></center>
		<option value="1">1</option>
		<option value="2">2</option>
		<option value="3">3</option>
		<option value="4">4</option>
		<option value="5">5</option>
		<option value="6">6</option>
		<option value="7">7</option>
		<option value="8">8</option>
		<option value="9">9</option>
		<option value="10">10</option>
		<option value="11">11</option>
		<option value="12">12</option>
		<option value="13">13</option>
		<option value="14">14</option>
		<option value="15">15</option>
		<option value="16">16</option>

	</select>
	</td>
	</tr>
	
	<tr>
        <td><center><font color="red" >ItemNum:</font><center>
        <select type="text" name="it_num" value=""></center>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
		<option value="5">5</option>
		<option value="6">6</option>
		<option value="7">7</option>
		<option value="8">8</option>
		<option value="9">9</option>
		<option value="10">10</option>
		<option value="11">11</option>
		<option value="12">12</option>


        </select>
        </td>
	</tr>
		<tr>
		<td colspan="2" align="center"><input type="submit" name="submit" value='submit'> </td>
		</tr>	
	</table>

	
	</form>
    <?php } ?>
</body>
</html>
&#13;
&#13;
&#13;

我需要有关此代码的帮助。它应该连接到&#39; connect.php&#39;然后应用代码将数据插入名为&#39; test&#39;的数据库中并向用户反馈他的数据已被插入

&#13;
&#13;
<?php include('connect.php'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtm111/DTD/xhtm111.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang=en
<html>
<head> <title>Taheal</title>
</head>
<body bgcolor ="lightyellow">
	<form name="consumables" method ="post" action="connect.php"/>
	<table border = "2" align = "center" bgcolor = "lightblue">
	<tr>
	<td colspan= "2" align = "center">Form</td>
	</tr>

	<tr>
	<td><center><font color = "red" >consumables:</font><center>
	<select type = "text" name = "fname" value =""></center>
		<option value="1">1</option>
		<option value="a">a</option>
		<option value="b">b</option>
		<option value="c">c</option>
	</select>
	</td>
	</tr>

	<tr>
	<td><center><font color = "red" >RoomNum:</font><center>
	<select type="text" name ="lname" value=""/></center>
		<option value="1">1</option>
		<option value="2">2</option>
		<option value="3">3</option>
		<option value="4">4</option>
		<option value="5">5</option>
		<option value="6">6</option>
		<option value="7">7</option>
		<option value="8">8</option>
		<option value="9">9</option>
		<option value="10">10</option>
		<option value="11">11</option>
		<option value="12">12</option>
		<option value="13">13</option>
		<option value="14">14</option>
		<option value="15">15</option>
		<option value="16">16</option>

	</select>
	</td>
	</tr>
	
	<tr>
        <td><center><font color = "red" >ItemNum:</font><center>
        <select type = "text" name = "it_num" value =""></center>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
		<option value="5">5</option>
		<option value="6">6</option>
		<option value="7">7</option>
		<option value="8">8</option>
		<option value="9">9</option>
		<option value="10">10</option>
		<option value="11">11</option>
		<option value="12">12</option>


        </select>
        </td>
	</tr>
		<tr>
		<td colspan = "2" align = "center"><input type="submit" name= "submit" value = 'submit'> </td>
		</tr>	
	</table>

	
	</form>

</body>
</html>
&#13;
&#13;
&#13;

这是&#39; connect.php&#39;文件

&#13;
&#13;
<?php
$servername = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "test";

$fname = $_POST['fname']
$lname = $_POST['lname'];
$it_num = $_POST['it_num'];

//   create connection
$conn = new mysqli($servername, $dbusername, $dbpassword, $dbname);

//   check connection 
if ($conn->connect_error) {
	die("connection failed: " . $conn->connect_error);
}

if (empty($fname)){
	echo "sometxt"
	die();
}

if (empty($lname)){
        echo "sometxt"
        die();
}

if (empty($it_num)){
        echo "sometxt"
        die();
}

$sql ="INSERT INTO test ('$first_name', '$last_name', '$item_num')
VALUES {('$fname')}, {('$lname')}, {('$it_num')}";

if ($conn->query($sql) == TRUE) {
	echo  "thank you for inserting info in the database"
} else {
	echo "ERROR: " $sql . "<br>" .$conn->error;

}
$conn->close()
?>
&#13;
&#13;
&#13;  

我只需要知道问题是否在语法中,因为在我提交项目后它没有做任何事情。只需挂在“localhost / db / connect.php&#39;”。 并且不会给出错误。

1 个答案:

答案 0 :(得分:0)

首先:您不需要在“form.php”文件中包含“connect.php”,因为您在提交表单时调用它

  

删除:(leftArrow)? php include('connect.php'); ?&GT;

修复没有jQuery库

  

注意:HTML文件必须具有PHP扩展名(而不是form.html使用form.php)

/** PHP File connect.php **/

<?php
$servername = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "test";

$fname = $_POST['fname']
$lname = $_POST['lname'];
$it_num = $_POST['it_num'];

/** Create connection **/
$conn = new mysqli($servername, $dbusername, $dbpassword, $dbname);

/** 
* Use !empty($var) instead of $var, because is fast and return TRUE only if $var not empty 
* Use urlencode() to generate correct $_GET string
**/
if (!empty($conn->connect_error)) {
    header('location: /form.php?error='.urlencode($conn->connect_error));
    exit; /** Prevent the script from running in background **/
}

if( empty($fname) ) {
    header('location: /form.php?error='.urlencode('fname is empty'));
    exit; /** Prevent the script from running in background **/
}
if( empty($lname) ) {
    header('location: /form.php?error='.urlencode('lname is empty'));
    exit; /** Prevent the script from running in background **/
}

if( empty($it_num) ) {
    header('location: /form.php?error='.urlencode('it_num is empty'));
    exit; /** Prevent the script from running in background **/
} else if( !is_numeric($it_num) ) {
    header('location: /form.php?error='.urlencode('it_num must be a number'));
    exit; /** Prevent the script from running in background **/
}

/**
* Example of db_table_field    :    first_name
* SQL                          :    INSERT INTO test ('first_name') ...
* Use mysql_escape_string() to prevent Injection of JS code, etc, into DB
**/
$SQL = "INSERT INTO test ('db_table_field_1', 'db_table_field_2', 'db_table_field_3') VALUES ('".mysql_escape_string($fname)."', '".mysql_escape_string($lname)."', '".mysql_escape_string($it_num)."')";

/** Use === instead of ==, because It's more secure **/
if ($conn->query($SQL) === TRUE ) {
    header('location: /form.php?success='.urlencode('Thank you for inserting info in the database') );
} else {
    header('location: /form.php?error='.urlencode($conn->error));
}

exit; /** Prevent the script from running in background **/

?>
  如果我们退出PHP脚本=)

,则不需要 $ conn-&gt; close()
/** form.php file **/

<DOCTYPE ...>
...
<body bgcolor ="lightyellow">
    <?php if(!empty($_GET['success'])) { ?>
    <div class="SUCCESS_MESSAGE">
        <?php echo $_GET['success']; ?>
    </div>
    <a href="/form.php">New insert</a>
    <?php } else if(!empty($_GET['error'])) { ?>
    <div class="SUCCESS_MESSAGE">
        <?php echo $_GET['success']; ?>
    </div>
    <a href="/form.php">Retry</a>
    <?php } else { ?>
    <form>
        ... SHOW FORM HTML HERE ...
    </form>
    <?php } ?>
</body>

使用jQuery库修复

如果您需要更动态的技术,则必须使用jQuery方法(JavaScript)

如果您想尝试一下,我可以编辑这篇文章=)没问题

相关问题