如何在php中的表中插入多个值 [英] How to insert multiple values into a table in php

查看:27
本文介绍了如何在php中的表中插入多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经学习并被教导如何将单个值插入到表中的列中,但我无法将多个值插入到同一个表中的多列中

I have learnt and been taught how to insert a single value into a column in a table but i cant get my head around inserting multiple values into multiple columns in the same table

我希望能够添加 ID、名字、第二名和电子邮件,有人可以帮忙吗?请.

I want to be able to add an id, first name, second name and email, can anyone help? please.

我有一个 php 文件来连接数据库以及插入数据库的函数.

I have a php file to connect to the database along with the function to insert into the database.

db.php

<?php

function getSQLConnection() {
$mysqlConnection = new PDO('mysql:host=localhost;dbname=isad235_100000', "root", "");


return $mysqlConnection;
}

function insertSingleValue($tableName, $columnName, $value) {
$sql = 'INSERT into ' . $tableName . '(' . $columnName . ') VALUES(:Value)';

$mySqlConnection = getSQLConnection();
$sqlStatement = $mySqlConnection->prepare($sql);
$sqlStatement->bindValue(":Value", $value, PDO::PARAM_STR);

$bReturn = false;
try {
    $sqlStatement->execute();
    //if we get to here it's all worked.
    $bReturn = true;
} catch (PDOException $e) {
    echo $e->getMessage();
}
return $bReturn;
}

function getResults($tablename) {
$sql = "SELECT * FROM " . $tablename;

$mysqlConnection = getSQLConnection();

$ResultSet = $mysqlConnection->query($sql);
return $ResultSet;
}

  ?>

以及获取数据并调用插入函数的网页文件.

and a webpage file which gets the data and calls the function to insert.

index.php

<?php
include_once 'db.php';

if (isset($_POST['first_name'])) {
$success = insertSingleValue("members", "first_name", $_POST['first_name']);
if (!$success)
    echo 'Sorry, the insert failed';
}
$Results = getResults("members");
?>

 <!DOCTYPE html>
<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title></title>
</head>
<body>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

        <label id="lblName" for="first_name">First Name</label>

        <input type="text" name="first_name" />

        <input type="submit" name="submit" value="Submit" />
    </form>
    <table border="1">
        <tr><th>Name</th>
        </tr>

<?php
if (isset($Results)) {
foreach ($Results as $row) {
    echo '<tr><td>';
    echo $row['first_name'];
    echo '</td></tr>';
    }
}
?>

    </tr>
</table>
</body>
</html>

推荐答案

语法是

INSERT INTO table( col1, col2, col3, ..., coln ) VALUES( val1, val2, ..., valn)

插入单条记录.例如,使用

to insert a single record. For example, use

$sql = 'INSERT INTO ' . $tableName . '( firstname, lastname, salary ) VALUES( :FirstName, :LastName, :Salary )';

作为 SQL 然后 bindValue 三个参数的值:

as the SQL and then bindValue the values to the three parameters:

$mySqlConnection = getSQLConnection();
$sqlStatement = $mySqlConnection->prepare($sql);
$sqlStatement->bindValue(":FirstName", $firstname, PDO::PARAM_STR);
$sqlStatement->bindValue(":LastName", $lastname, PDO::PARAM_STR);
$sqlStatement->bindValue(":Salary", $salary, PDO::PARAM_STR);

$bReturn = false;

我将让您在函数中获取正确的数据(例如,您可以将数组传递为数组而不是单个 $column$value单个参数,例如 array( "firstName" => "Compu", "lastName" => "Chip", "salary" => 65000),并使用它来构建动态SQL 语句.

I will leave it up to you to get the right data in the function (e.g. instead of a single $column and $value you could pass in an array as a single parameter, like array( "firstName" => "Compu", "lastName" => "Chip", "salary" => 65000), and use that to construct a dynamic SQL statement.

这篇关于如何在php中的表中插入多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆