gpt4 book ai didi

php - HTML表单使用PHP写入MySQL

转载 作者:行者123 更新时间:2023-11-29 23:17:58 30 4
gpt4 key购买 nike

我需要以下表单来访问 MySQL 表“table1”和列“name”、“email”和“comment”。

<form method="post" action="sign.php" class="pure-form pure-form-stacked">
<fieldset>

<label for="name">Your Name</label>
<input id="name" type="text" placeholder="Your Name" name="name">


<label for="email">Your Email</label>
<input id="email" type="email" placeholder="Your Email" name="email">

<label for="comment">Your Comments</label>
<input id="comment" type="text" placeholder="Your Comments" name="comment">

<a href="thanks.html" button type="submit" class="pure-button">SUBMIT</button>
</a>

</fieldset>
</form>

我的 PHP 文件如下所示:

<html>
<body>

<?php
$myUser = "dbuser";
$myPassd = "********";
$myDB = "dbname";
$myserver = "192.168.1.80:3306";
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];

// Create connection
$dbhandle = mysql_connect($myserver,$myUser,$myPassd)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";

//select a database to work with
$selected = mysql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");
//if (mysqli_connect_errno()) {
//echo "Failed to connect to MySQL: " . mysqli_connect_error();
//}
//echo "connected";

$sql = "INSERT INTO signatures (name, comment) VALUES ( $name, $comment)";

if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
echo "record added";

mysql_close($dbhandle);
?>
</body>
</html>

我还需要它在运行后重定向到另一个 html 页面。

最佳答案

这段代码解决了您的发帖问题:

<form method="post" id="form" action="sign.php" class="pure-form pure-form-stacked">

... your HTML ...
<a href="javascript:document.getElementById('form').submit()" class="pure-button">SUBMIT</button>
</a>

使用以下内容对数据库进行 PDO 查询(更安全!)。

function openDBConnection()
{
$myUser = "dbuser";
$myPassd = "********";
$myDB = "dbname";
$myserver = "192.168.1.80";
$port = 3306;

try
{
//this line was just updated!
$dbConn = new PDO("mysql:host=$myserver;port=$port;dbname=$myDB", $myUser, $myPassd, array( PDO::ATTR_PERSISTENT => false));
}
catch( PDOException $Exception )
{
echo ("Unable to connect to database.");
exit;
}
return $dbConn;
}

function doPDOQuery($sql, $type, $var = array(), $dbname, $fetch = false)
{
$db = openDBConnection();
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//Prepare and query are two types, please use prepare.
if ($type == "prepare")
{
$queryArray = array();
if ($var != null)
{
foreach ($var as $key => $queryItem)
{
$queryArray[$key] = $queryItem;
}
}
$sth = $db->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$sth->execute($queryArray);
}
else if ($type == "query")
{
$sth = $db->query($sql);
}
else
{
echo ("Supplied type is not valid.");
exit;
}
if (!$sth)
{
$error = $db->errorInfo();
echo($error[2]);
exit;
}

//$fetch: set to true when you expect results, set to false (default) when you do update/delete/insert and there are no records returned. Else the records are returned through $sth->fetchAll();
if (!$fetch)
{
return $sth->rowCount();
}
return $sth->fetchAll();
}

数据库条目的示例查询

$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];

$SQL = "
INSERT INTO signatures (name, email, comment) VALUES ( :name, :email, :comment);
";
$result = doPDOQuery($SQL, "prepare", array(":name" => $name, ":comment" => $comment, ":email" => $email), $myDB, false);

请阅读 PDO。其工作原理如下:

  1. 数据库连接已建立
  2. SQL 由 SQL 服务器准备
  3. 参数被加载到 SQL 查询中(由数组提供)。
  4. 参数被转义,这使得 SQL 注入(inject)的可能性降低。

关于php - HTML表单使用PHP写入MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27594477/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com