如何使用php

时间:2015-04-22 14:20:44

标签: php mysql sql database insert

我需要帮助了解如何将值插入到mysql数据库中。我知道我需要编写一个INSERT语句来说明我从用户那里得到的数据。但我真的不明白这个插入语句的位置以及如何运行它。我使用pg_prepare和pg_execute吗?如果有人可以帮我将代码设置到我将运行insert语句的位置,我将非常感激!感谢您的帮助。

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>

所以用户基本上插入了3个值,我想在按下提交按钮时将它们全部插入到数据库中!

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();

?>

3 个答案:

答案 0 :(得分:1)

$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));

就这么简单。

答案 1 :(得分:0)

基本上你必须做两个步骤:

(1)设置连接,(2)选择数据库并运行INSERT。

$mysqli = new mysqli("localhost", "my_user", "my_password", "database");
$mysqli->query("INSERT INTO ...");

答案 2 :(得分:0)

    /*
 * 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')");
}