I want to make a website about famous quotes and i want to add an "ADD Quote Section". I made the <form>
like below, but i want when if someone forgets to select an author or an subject or to write his quote to display an error like in the error section bellow, but if he write the quote and forgot to select the author or the subject i want to display the error but keep the writed quote so the user will not be angry or something like this that he have to write again his quote. This code i want to apply to the select author and select subject too. The form will look like this: [Form add quote]: http://imgur.com/PN72ZcH.jpg
And another problem will be that on refresh that added quote is added again in mysql database and i want to be checked if that quote is added in database and if it is added to display an error 'This quote is already in the database'.
require("connection.php");
Here you select the name of the author:
echo '<select name="selauthor"><option>-Author-</option>';
$author=mysql_query('SELECT * FROM author ORDER BY `name` ASC ');
while($linea=mysql_fetch_array($author)){
echo '<option value="'.$linea[0].'">'.$linea[1].'</option>';}
echo '</select>';
Here you select the quote subject(like love, friendship, peace)
echo '<select name="selsubject"><option>-Subject-</option>';
$subject=mysql_query('SELECT * FROM subject ORDER BY `name` ASC ' );
while($lines=mysql_fetch_array($subject)){
echo '<option value="'.$lines[0].'">'.$lines[1].'</option>';}
echo '</select>';
And here you write your quote:
echo 'Write your quote:<input type="text" name="quote" value="">
<input type="submit" name="qtebut" value="Add Quote"><br /><br /><br />';
if(isset($_POST['qtebut'])){
$author=$_POST['selauthor'];
$subject=$_POST['selsubject'];
$quote=$_POST['quote'];
Errors:
if($author=='-Author-'){echo 'Choose Author.<br />';}
if($subject=='-Subject-'){echo 'Choose Subject.<br />';}
if($quote==''){echo 'Don`t forget to write your quote.<br />';}
if($quote!='' && $author!='-Author-' && $subject!='-Subject-'){
$insert=mysql_query('INSERT INTO `quotes` (`id_subject`, `id_author`, `quote`)
VALUES ("'.$author.'", "'.$subject.'", "'.$quote.'")');
echo 'Quote added in the database';}
Try This This is HTML FORM:
<form>
<select name="selauthor" required="required">
<option value="">-Select Author-</option>
</select>
<select name="selsubject" required="required">
<option value="">-Select Subject-</option>
</select>
<input type="text" name="qtebut" value="" required="required">
<input type="submit" value="ADD quote" />
</form>
PHP CODE:
$author=$_POST['selauthor'];
$subject= $_POST['selsubject'];
$quote=$_POST['qtebut'];
if($author==''){echo 'Choose Author.<br />';}
if($subject==''){echo 'Choose Subject.<br />';}
if($quote==''){echo 'Don`t forget to write your quote.<br />';}
if($quote !='' && $author !='' && $subject!=''){
$insert=mysql_query('INSERT INTO `quotes` (`id_subject`, `id_author`, `quote`)
VALUES ("'.$author.'", "'.$subject.'", "'.$quote.'")');
if($insert)
{
echo 'Quote added in the database';
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments