Reputation: 1
I'm new to PHP so this is new to me. Anyway I'm creating a site so I can access my receipes online.
This is the form:
<form action="Form.php" method="post" class="basic-grey">
<h1><a href="index.html">Receita</a>
<span>Aqui podes adicionar uma nova receita</span>
</h1>
<label>
<span>Titulo :</span>
<input id="titulo" type="text" size="20" maxlength="100" name="titulo" placeholder="Introduza o Titulo" />
</label>
<label>
<span>Categoria :</span><select maxlength="10" name="categoria">
<option value="categoria">--- Seleccione aqui a Categoria ---</option>
<option name=" " value="sopa">Sopa</option>
<option name="entrada" value="entrada">Entrada</option>
<option name="carne" value="carne">Carne</option>
<option name="peixe" value="peixe">Peixe</option>
<option name="salada" value="salada">Salada</option>
<option name="sobremesa" value="sobremesa">Sobremesa</option>
</select>
</label>
<label>
<span>Ingredientes :</span>
<textarea id="ingredientes" size="20" maxlength="1000" name="ingredientes" placeholder="Introduza os ingredientes"></textarea>
</label>
<label>
<span>Preparação :</span>
<textarea id="preparacao" size="20" maxlength="1000" name="preparacao" placeholder="Introduza o modo de preparação"></textarea>
</label>
<label>
<span>Notas :</span>
<textarea id="notas" size="20" maxlength="1000" name="notas" placeholder="Aqui pode adicionar uma nota"></textarea>
</label>
<label>
<span> </span>
<input type="submit" class="button" value="Enviar" />
</label>
</form>
This is the code to handle the form:
<?php
// processing form values
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$titulo = $_POST['titulo'];
$categoria = $_POST['categoria'];
$ingredientes = $_POST['ingredientes'];
$preparacao = $_POST['preparacao'];
$notas = $_POST['notas'];
if(!empty($titulo) && !empty($categoria) && !empty($ingredientes) && !empty($preparacao) && !empty($notas)){
include('connection.php');
mysqli_query($dbc, "INSERT INTO receita(Titulo,Categoria,Ingredientes,Preparacao,Notas) VALUES ('$titulo','$categoria','$ingredientes','$preparacao','$notas')");
$registered = mysqli_affected_rows($dbc);
echo $registered." row is affected, everything worked fine!";
}else{
echo "Please fill all values on the form";
}
}else{
echo "No form has been submitted";
}
?>
And what happens is that if I input something like this it doesn't work:
Titulo:Açorda de camarao
Categoria: Peixe
Ingredientes: 800 g de camarao; 4 dentes de alho; 1 ramo de salsa ou coentros; 3 ovos inteiros; 1.5 dl de Azeite; 1.5 pão por pessoa; sal; piri-piri
Preparação: Coze-se o camarão com sal e piri-piri e reserva-se a agua. De seguida demolha-se o pão na agua do camarão. Aquece-se o azeite com os alhos e os coentros e de seguida junta-se o camarão e por ultimo o pão. Mexe-se tudo para cozer o pão e ganhar consistencia. Por ultimo junta-se os ovos e envolve-se tudo.
Nota: Receita para 4 pessoas
But if I input like this it works:
Titulo:gfdsfdsa
Categoria: Peixe
Ingredientes: hudsbfbdsf fdsfidsfidsfsd, fdsjifjdsifdis 0palpdsandnsaud jkdosakodsakodmnsa jidsjaidsa
Preparação: nfjdbshfbhdbjfdjs dsajijdisandiabuu fjndoisjfojidsanfds
Nota: fbhdubsufbndsnfs
My database table:
Nome Tipo Agrupamento (Collation) Atributos Nulo Omissao Extra
1 ID bigint(50) Não None AUTO_INCREMENT Muda Muda Elimina
2 Titulo varchar(100) utf8_general_ci Não None Muda Muda Elimina
3 Categoria varchar(10) utf8_general_ci Não None Muda Muda
4 Ingredientes varchar(1000) utf8_general_ci Não None Muda
5 Preparacao varchar(1000) utf8_general_ci Não None Muda Muda 6 Notas varchar(1000) utf8_general_ci Não None Muda Muda
Sorry if this post its to long. Any ideas how to fix it?
Upvotes: 0
Views: 105
Reputation: 782508
There's probably a special character in the input that's causing a syntax error in the query.
You need to either escape your input before substituting it into the query. Add this after include ('connection.php');
$titulo = mysqli_real_escape_string($dbc, $titulo);
$categoria = mysqli_real_escape_string($dbc, $categoria);
// and so on for all the other variables
or (better) use a prepared statement. Use this in place of your call to mysqli_query
:
$stmt = mysqli_prepare($dbc, "INSERT INTO receita(Titulo,Categoria,Ingredientes,Preparacao,Notas) VALUES (?, ?, ?, ?, ?)");
mysqli_stmt_bind_param($stmt, "sssss", $titulo, $categoria, $ingredientes, $preparacao, $notas);
mysqli_stmt_execute($stmt);
$registered = mysqli_stmt_affected_rows($stmt);
Upvotes: 5