Posts are not saved through the checkbox - php

I have tried in many ways to fix it but without success.
I'm trying to sort categories through the checkbox but it does not work. The post is made public during editing but the checkbox is not active and after the post update is checkbox removes the id and the post loses from the category.
<form name="addpost" method="post" enctype="multipart/form-data">
<div class="form-group m-b-20">
<label for="exampleInputEmail1">Category</label>
<br>
<?php
$ret=mysqli_query($con,"select id,CategoryName from tblcategory where Is_Active=1");
while($result=mysqli_fetch_array($ret))
{
?>
<input type="checkbox" name="category" id="category" value="<?php echo htmlentities($result['id']);?>"><?php echo htmlentities($result['CategoryName']);?><br>
<?php } ?>
<button type="submit" name="update" class="waves-effect waves-light pull-right butoni">Update </button>
</div>
</form>

This:
<input type="checkbox" name="category" id="category" value="<?php echo htmlentities($result['id']);?>"><?php echo htmlentities($result['CategoryName']);?><br>
is placed in a loop... so there are more than one of these yes?!
We can't use the same id, and name for multiple inputs of a form or in html ... instead :
<input type="checkbox" name="category[]" id="category<?php echo $result['id'];?>" value="<?php echo htmlentities($result['id']);?>"><?php echo htmlentities($result['CategoryName']);?><br>
this will make things appear in one after the other...
if you want the box to be checked add an if in the loop
if($result['something'] should be checked){ ?>
<input type="checkbox" name="category[]" id="category<?php echo $result['id'];?>" value="<?php echo htmlentities($result['id']);?> checked "><?php echo htmlentities($result['CategoryName']);?><br><?php
} else {
<input type="checkbox" name="category[]" id="category<?php echo $result['id'];?>" value="<?php echo htmlentities($result['id']);?>"><?php echo htmlentities($result['CategoryName']);?><br><?php
}
just fix the:
if($result['something'] should be checked){rest of the code}
to be correct according the when checkbox should be checked and when not.
For checkbox to be checked we add a checked in the input line.

Related

PHP Form Drop Down menu stills blank after I select an item

I am executing an form based on PHP on Windows, however after I select an item in the drop down menu, the text does not show up (even though it was selected properly, as I can verify in the database after clicking the insert button).
The form code:
<div id="janela_formulario">
<form action="inserir_transportadoras.php" method="post">
<input type="text" name="nometransportadora" placeholder="Nome da Transportadora">
<input type="text" name="endereco" placeholder="Endereço">
<input type="text" name="telefone" placeholder="Telefone">
<input type="text" name="cidade" placeholder="Cidade">
<select name="estados">
<?php
while($linha = mysqli_fetch_assoc($lista_estados)) {
?>
<option value="<?php echo $linha["estadoID"]; ?>">
<?php echo utf8_encode($linha["nome"]); ?>
</option>
<?php
}
?>
</select>
<input type="text" name="cep" placeholder="CEP">
<input type="text" name="cnpj" placeholder="CNPJ">
<input type="submit" value="inserir">
</form>
</div>
The dropdown menu PHP code:
<input type="text" name="cidade" placeholder="Cidade">
<select name="estados">
<?php
while($linha = mysqli_fetch_assoc($lista_estados)) {
?>
<option value="<?php echo $linha["estadoID"]; ?>">
<?php echo utf8_encode($linha["nome"]); ?>
</option>
<?php
}
?>
</select>
How can I solve it?
while($linha = mysqli_fetch_assoc($lista_estados)) {
echo '<option value="'. $linha["estadoID"] .'"> '.utf8_encode($linha["nome"]).' </option>';
}
Use above code instead of while loop
test by inspect HTML code. is this estadoID are printing properly if yes then test you are catching (estados) data properly

Says Updated Successfully But When I look At The Database It's Not

Can you guys double check my codes. When I clicked the Update button it told me that the update was successful but when I look at my database nothing changed. I know I'm doing something wrong. Hope you guys help me to figure it out. Thanks.
I am still learning and it's an honor to learn from you guys. Please be nice and use basic terms hehe
<html>
<body>
<div>
<form action="edit.php?id=<?php echo $id;?>" method="post">
<div>
<label for="prov">Provinces</label><br>
<select id="prov"type="text" name="provi1">
<option><?php echo $provi; ?></option>
<option value="Isabela">Isabela</option>
<option value="La Union">La Union</option>
<option value="Pangasinan">Pangasinan</option>
<option value="Ilocos Sur">Ilocos Sur</option>
<option value="Ilocos Norte">Ilocos Norte</option>
</select>
</div>
<div>
<label>Date</label><br>
<input type="date" name="date" value="">
</div>
<div>
<label>Typhoon Name</label><br>
<input type="text" name="typhoon" value="<?php echo $typhoon; ?>">
</div>
<div>
<label>Warning #</label><br>
<input type="text" name="warning" value="<?php echo $warning; ?>">
</div>
<div>
<label for="prov">SS Height</label><br>
<label>2-3 meters
<input type="radio" name="meter" value="2-3 Meters"
<?php if ($ssh == '2-3 Meters') echo 'checked="checked"'; ?>>
</label>
<label>1-2 meters
<input type="radio" name="meter" value="1-2 Meters"
<?php if ($ssh=='1-2 Meters') echo 'checked="checked"'; ?>>
</label>
<label>< 1 meter
<input type="radio" name="meter" value="< 1 Meter"
<?php if ($ssh=='< 1 Meter') echo 'checked="checked"'; ?>>
</label><br>
<label for="imp">Impacts</label><br>
<textarea id="imp" type="text" name="impact1"><?php echo $impact; ?></textarea>
<label for="adv" >Advice</label><br>
<textarea id="adv" type="text" name="advice1"><?php echo $advice; ?></textarea>
</div>
<div class="form-group">
<input type="submit" value="Update" name="update">
<input type="reset" value="Cancel">
</div>
</form>
</div>
</body>
</html>
Fix the MySQL injection, or I WILL come and erase your database. (See comments under question).
Then (and only AFTER you fixed that):
You are using id from the $_GET, but for the update your ID comes from $_POST. However, you forgot to put that <input name='id' value='<?=$edit?>' type='hidden'> within your form. So there is no $_POST['id'], so it only updates all records with an empty id. And says: "Yay, I succesfully updated 0 records!". Hence no error: it was an overwhelming success. ;)
You seem to be confused about the id. There is no need for an $edit, an $id and an $u_id variable. It is all the same id. Your<FORM ACTION=... actually puts ?id= (empty string) behind the URL, since you emptied $id.

How to check multiple radio buttons in array loop in php

I have fetched data and show in the radio buttons while the radio buttons are in loop, and insert the data in loop too.
The problem is that I can check only one radio button from the form, for each question one radio button must be check, which I can't.
<form method="POST" class="form-horizontal">
<?php
$count=1;
$que="SELECT * FROM addques WHERE quz_id='$var'";
$dbd=mysqli_query($conn,$que);
while ($cmd=mysqli_fetch_array($dbd)) {
$quest=$cmd['qusname'];
$ans_id=$cmd['ans_id'];
$opt1=$cmd['qpta'];
$opt2=$cmd['optb'];
$opt3=$cmd['optc'];
$opt4=$cmd['optd'];
$answ=$cmd['answer'];?>
<b>Question <?php echo $count++;?> :<br><?php echo $quest;?></b><br><br>
<fieldset>
<input type="hidden" name="ansid[]" value="<?php echo $ans_id; ?>">
<input type="radio" name="ans[]" value="1"><?php echo $opt1;?><br><br>
<input type="radio" name="ans[]" value="2"><?php echo $opt2;?><br><br>
<input type="radio" name="ans[]" value="3"><?php echo $opt3;?><br><br>
<input type="radio" name="ans[]" value="4"><?php echo $opt4?><br><br><br>
</fieldset>
<?php } ?>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
<?php
$i=0;
if (isset($_POST['submit'])) {
while ( $i<$noquestions ) {
$query="INSERT INTO `result`( `quz_id`, `ans_id`, `answer`) VALUES('";
$query.=$var . "', '";
$query.=$_POST['ansid'][$i] . "', '";
$query.=$_POST['ans'][$i] . "' )";
$db=mysqli_query($conn,$query);
$i++;
}
}
?>
The reason why you're getting that bug is you have given name="ans[]" so each time the index value incrementing.
You can fix it by giving the following code
<input type="radio" name="ans" value="1">
<input type="radio" name="ans" value="2">
And so on.
You have been using radio button inside a loop so you can use it by setting a loop variable like name="ans[$i]"
echo '<input type="radio" name="ans['.$i.']" value="1">
<input type="radio" name="ans['.$i.']" value="2">';

Checking the correct answer from the database by a checked radio button in PHP

I am creating an Online Assessment. I display all the questions randomly in a one page only. I have difficulty on how to check the correct answer in the database for checked radio button. I don't know what to do and the logic on how to do it.
This is my php codes for displaying the questions randomly,
$view_questions=mysql_query("SELECT * FROM questions ORDER BY RAND()");
This is my html codes with php codes,
<form name="" method="POST">
</br><h4># of Questions</h4>
<?php
$i=1;
while($row=mysql_fetch_array($view_questions))
{
?>
<div class="view_question fsize">
<p align="justify"><?php echo $i;?>) <?php echo $row['QUESTION'];?></p>
<div class="indent-question">
<input type="radio" value="1" id="" name="radio[<?php echo $row['QUESTION_NO'];?>]"> <?php echo $row['ANSWER_1'];?>
</br>
<input type="radio" value="2" id="" name="radio[<?php echo $row['QUESTION_NO'];?>]"> <?php echo $row['ANSWER_2'];?>
</br>
<input type="radio" value="3" id="" name="radio[<?php echo $row['QUESTION_NO'];?>]"> <?php echo $row['ANSWER_3'];?>
</br>
<input type="radio" value="4" id="" name="radio[<?php echo $row['QUESTION_NO'];?>]"> <?php echo $row['ANSWER_4'];?>
</div>
</div>
<?php
$i++;
}
?>
<center><button id='next<?php echo $i;?>' class='next btn btn-success' name="finish" type='submit'>Finish</button></center>
</form>
Table name: questions
Table fields: QUESTION_NO, QUESTION, ANSWER_1, ANSWER_2, ANSWER_3, ANSWER_4, ANSWER
It is very easy
store Id of you questions in the value in the radio button and checkbox only those value are submitted which are checked , compare your code after submitting the form in next page , I hope you will understand, What i am trying to say
You should have database structure like this..
Table 1: Questions
Fields: que_id, question
Table 2: Answers
Fields: ans_id, que_id, answer, correct_ans, points
Table 3: Results
Fields: que_id, ans_id
When you add question, question and que_id will be stored n database.. Then you'll add multiple possible answers which are stored in Answers table with reference to que_id..
You need to change query for GUI to fetch question and answer from different table using join.
So GUI would be like this..
<form name="" method="POST">
</br><h4># of Questions</h4>
<?php
$i=1;
while($row=mysql_fetch_array($view_questions))
{
?>
<div class="view_question fsize">
<p align="justify"><?php echo $i;?>) <?php echo $row['QUESTION'];?></p>
<div class="indent-question">
<input type="radio" value="<?php echo $row['ans_id']; ?>" id="" name="radio[<?php echo $row['QUESTION_NO'];?>]"> <?php echo $row['ANSWER_1'];?>
</br>
<input type="radio" value="<?php echo $row['ans_id']; ?>" id="" name="radio[<?php echo $row['QUESTION_NO'];?>]"> <?php echo $row['ANSWER_2'];?>
</br>
<input type="radio" value="<?php echo $row['ans_id']; ?>" id="" name="radio[<?php echo $row['QUESTION_NO'];?>]"> <?php echo $row['ANSWER_3'];?>
</br>
<input type="radio" value="<?php echo $row['ans_id']; ?>" id="" name="radio[<?php echo $row['QUESTION_NO'];?>]"> <?php echo $row['ANSWER_4'];?>
</div>
</div>
<?php
$i++;
}
?>
<center><button id='next<?php echo $i;?>' class='next btn btn-success' name="finish" type='submit'>Finish</button></center>
</form>
After that when user select an answer, ans_id will be saved along with the que_id in results table, from there you can manage all the information and comparison..
set Answerquestions to value for radio button
<input type="radio" value="ANSWER1" name="name">
<input type="radio" value="ANSWER2" name="name">
in php code
check the value submited with the correct Answer
get the correct Answer from db and save it in var
$query "SELECT correct_answer FROM TABEL_NAME";
and fetch query in var correct_a for example
$user_answer = $_POST['name'];
if($user_answer == $correct_a)
return true
else
return false

get submit form action with array

i have a form based on database mysql ,just like this
<form name="" id="form1" method="GET" action="submit.php"></form>
<?php
include 'connection.php';
$myquery="SELECT * FROM `tbl` " ;
$params=mysql_query($myquery) or die (mysql_error());
while ($param=mysql_fetch_assoc($params))
{?>
<input name="<?php echo $param['id'] ?>" value="<?php echo $param['child_id'] ?>" />
<input type="checkbox" name="checkbox<?php echo $param['id'] ?>" value="Yes" form="form1" />
<input class="form-control text-center" form="form1" name="desc<?php echo $param['id'] ?>" value="<?php echo $param['desc'] ?>"/>
<input class="form-control text-center" form="form1" name="value <?php echo $param['id'] ?>" value="<?php echo $param['value'] ?>"/>
<select class="form-control" form="form1" id="" name="select<?php echo $param['id'] ?>">
<option value=""></option>
<option value="OK">OK</option>
<option value="NOK">NOK</option>
</select>
<?php } ?>
<button type="submit" class="btn btn-success btn-s-md btn-rounded" form="form1"><i class="icon-save"></i>Save</button>
how can i submit (do update ) to mysql database with this form , i am a newbie in php .
thanks
So yes you have to add the balise form at this end. Then I advice you to add a hidden value with your id.
<input type="hidden" name"id" value="<?php echo $param['id'] ; ?>">
After ad in your page submit.php,
<?php
include 'connection.php';
if (isset($_POST["id"])){
$id=$_POST["id"];#so you can recover your id
$myquery="UPDATE ... " ;
mysql_query($myquery) or die (mysql_error());
echo "update success";# you can add whatever you want here
}?>
You have to wrap all your form elements into the <form> tag. So place your closing tag (</form>) at the end of your form.
<form name="" id="form1" method="GET" action="submit.php">
<input name="..." />
<select>
<option value="...">...</option>
...
</select>
<button>...</button>
</form>
By the way: PHP's mysql-functions are deprecated. Use mysqli instead!

Categories