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
I want to be able to add an id, first name, second name and email, can anyone help? please.
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>
The syntax is
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 )';
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;
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.
Do you mean multiple row? If so, this question is already answered in:
insert multiple rows via a php array into mysql
Inserting multiple rows in a table using PHP
I hope it helps