[英]How to insert values into a mysql database using php
I need help understanding how to insert values into a mysql database.我需要帮助了解如何将值插入到 mysql 数据库中。 I understand I will need to write a INSERT statement of the data that I get from the user.
我知道我需要为我从用户那里获得的数据编写一个 INSERT 语句。 But I dont really understand where to put this insert statement and how to get it to run.
但我真的不明白把这个插入语句放在哪里以及如何让它运行。 Do I use pg_prepare and pg_execute?
我是否使用 pg_prepare 和 pg_execute? If someone could just help me set my code up to where I would run the insert statement I would greatly appreciate it!
如果有人可以帮助我将代码设置为运行插入语句的位置,我将不胜感激! Thanks for the help in advance.
我在这里先向您的帮助表示感谢。
HTML code HTML代码
<!DOCTYPE html>
<html>
<body>
<form method="POST", action="Blast.php">
<select id="database" name="database" value='Select a Database'>
<option value="UniprotKB">UniProtKB</option>
<option value="GenBank">GenBank</option>
<option value="RelSeq">RelSeq</option>
</select>
<select id="evalue" name="evalue" value='Select evalue'>
<option value="0.0001">0.0001</option>
<option value="0.001">0.001</option>
<option value="0.01">0.01</option>
<option value="0.1">0.1</option>
<option value="1">1</option>
<option value="10">10</option>
<option value="100">100</option>
<option value="1000">1000</option>
</select>
<input id="BlastSearch" type="text" name="BlastSearch" value='' />
<input type='submit' name='submit' value='Run BLAST' />
<button type="reset" value="Clear">Clear</button>
</form>
So there are basically 3 values that are inserted by the user, and I want to insert them all into the database when the submit button is pressed!所以基本上有3个值是由用户插入的,我想在按下提交按钮时将它们全部插入数据库!
PHP Code PHP代码
<?php
require_once '../secure/database.php';
$mysqli = new mysqli($dbhost,$dbuser,$dbpass,$dbname);
if($mysqli->connect_error){
exit('CON Error: ' . $mysqli->connect_errno . ' ' . $mysqli->connect_error);
}
$db = $_POST['database'];
$evalue = $_POST['evalue'];
$sequence = $_POST['BlastSearch'];
print "Connected! Host info: " . $mysqli->host_info . "<br>\n";
$mysqli->close();
?>
$dsn = 'mysql:host=localhost;dbname=your_db_name';
$username = 'username';
$password = 'password';
$pdo = new PDO($dsn, $username, $password, $options);
$stm = $pdo->prepare('INSERT INTO table (col1, col2, col3) VALUES (?,?,?)');
$stm->execute(array($col1_value, $col2_value, $col3_value));
It is as easy as that.就是这么简单。
Basically you have to do two steps:基本上你必须做两个步骤:
(1) Setup you connection, (2) Select your database and run your INSERT. (1) 设置您的连接,(2) 选择您的数据库并运行您的 INSERT。
$mysqli = new mysqli("localhost", "my_user", "my_password", "database");
$mysqli->query("INSERT INTO ...");
/*
* SQL
CREATE TABLE `NewTable` (
`id` int NOT NULL AUTO_INCREMENT ,
`col1` varchar(255) NOT NULL ,
`col2` varchar(255) NOT NULL ,
`col3` varchar(255) NOT NULL ,
PRIMARY KEY (`id`)
)
;
*
* /SQL
*/
$mysqli = new mysqli("localhost", "DB_USERNAME", "DB_PASSWORD", "DB_NAME");
if($mysqli->connect_error)
{
die('Connect Error' . $mysqli->connect_error);
}
$mysqli->query("SET NAMES 'utf8'");
$mysqli->query("SET CHARACTER SET 'utf8'");
$mysqli->query("SET COLLATION_CONNECTION='utf8_general_ci'");
$mysqli->query("SET character_set_results = 'utf8'");
$mysqli->query("SET character_set_server = 'utf8'");
$mysqli->query("SET character_set_client = 'utf8'");
//connection END
if(isset($_POST['database']) and isset($_POST['evalue']) and isset($_POST['BlastSearch'])){
$db = htmlspecialchars(strip_tags($_POST['database']));
$evalue = htmlspecialchars(strip_tags($_POST['evalue']));
$sequence = htmlspecialchars(strip_tags($_POST['BlastSearch']));
$mysqli->query("INSERT INTO TABLE_NAME (col1,col2,col3) VALUES('$db','$evalue','$sequence')");
}
You can try this:你可以试试这个:
<form action="send.php" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>Value1:</td>
<td><input type="text" name="value1" required><br></td>
</tr>
<tr>
<td>value2:</td>
<td><input type="text" name="value2" required><br></td>
</tr>
<tr>
<td>value3:</td>
<td><input type="text" name="value3" required><br></td>
</tr>
</table>
</form>
Now on the send.php you have this:现在在 send.php 你有这个:
<?php
require 'database.php';
//information is send to the database//
$add_info= $database->information();
?>
The class is in ur database.php wich looks like this:该类在您的 database.php 中,如下所示:
<?php
class database {
private $pdo;
public function __construct() {
// Connection information
$host = 'urhostname';
$dbname = 'dbname';
$user = 'username';
$pass = 'password';
// Attempt DB connection
try
{
$this->pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//echo 'Successfully connected to the database!';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
function information() {
$sql = "INSERT INTO tablename"
. "(value1, value2, value3)"
. "VALUES (:value1, :value2, :value3) ";
$sth = $this->pdo->prepare($sql);
$sth->bindParam(':value1', $_POST['value1'], PDO::PARAM_STR);
$sth->bindParam(':value2', $_POST['value2'], PDO::PARAM_STR);
$sth->bindParam(':value3', $_POST['value3'], PDO::PARAM_STR);
$sth->execute();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.