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>