a newbie in PHP here. I am currently creating a blog from scratch. I have a problem with INSERT INTO. Here is my code. The function for the query INSERT INTO does not seem to populate the table. SQL injection security will be worked on later.
dbconnection.php
<?php
/* Connexion au serveur de la base de donnees */
$conn=mysqli_connect( DB_HOST, DB_USER, DB_MDP );
if (!$conn){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else{
/* Connexion a la base de donnees */
$dbselect=mysqli_select_db( $conn, DB_SELECT );
if (!$dbselect){
echo "Cannot select database" ;
}
}
?>
This is the page which contains the function for the query. article.sql.php
// INSERT
function insertArticle( $c ){
$qryInsertArt = 'INSERT INTO articles
VALUES (
\''.$TitreArticle.'\',
\''.$AuteurArticle.'\',
\''.$ContenuArticle.'\'
)';
$result = mysqli_query ( $qryInsertArt );
return ($result);
}
And this is my form: articleform.php
<h2>Articles</h2>
<form action="index.php?page=articles&action=" method="post">
<table class="admin_form" cellpadding="0" cellspacing="0" width="100%">
<?php $insertA = selectArticles($conn); ?>
<tr>
<td width="30%"><label for="TitreArticle">Titre</label></td>
<td><input id="TitreArticle" type="text" name="TitreArticle" value="<?php if(isset($_POST['TitreArticle'])){echo $_POST['TitreArticle'];} ?>" /></td>
</tr>
<tr>
<td width="30%"><label for="AuteurArticle">Auteur</label></td>
<td><input id="AuteurArticle" type="text" name="AuteurArticle" value="<?php if(isset($_POST['AuteurArticle'])){echo $_POST['AuteurArticle'];} ?>" /></td>
</tr>
<tr>
<td valign="top"><label for="ContenuArticle">Contenu de l'article</label></td>
<td><textarea id="ContenuArticle" name="ContenuArticle"><?php if(isset($_POST['ContenuArticle'])){echo $_POST['ContenuArticle'];} ?></textarea></td>
</tr>
<tr>
<td></td>
<td>
<a class="btn" href="index.php">Annuler</a>
<input type="submit" value="Envoyer" />
</td>
</tr>
</table>
Thanks in advance!
The right syntax of the mysqli_connect is this:
mysqli_connect("localhost","my_user","my_password","my_db");
You need to pass the db name also in this function.
mysqli_select_db
is used the change the db after connection created.
And in the INSERT query from where you are getting these variables:
$TitreArticle
$AuteurArticle
$ContenuArticle
Check properly are these have values or not.