Updating database with for loop and mysql - php

The code for updating my questions is working but when updating the answers it is not. How can i fix this? help. thanks.:)
This is on my form:
<?php do { ?><tr><th width="170" scope="col"><input type="checkbox" name="selected[]" value="<?php echo $row_Recordset1['question_id']; ?>" />
Description:</th>
<td colspan="2" scope="col">old:
<?php echo $row_Recordset1['question_description']; ?>
new:<input name="questiondesc[]" type="text" size="50" />/td>
<td width="549" colspan="2" scope="col"><div align="left"></td>
</tr>
<input type="hidden" name="ansid[]" value="<?php echo $row_Recordset2['answer_id']; ?>" />
<input name="answerdesc[]" type="text" size="20" value="<?php echo $row_Recordset2['answer_description']; ?>" /><?php
if($row_Recordset2['answer_iscorrect'] == 1){
echo "Correct";
}
?>
<?php } while ($row_Recordset2 = mysql_fetch_assoc($Recordset2)); ?>
<?php } while ($row_Recordset2 = mysql_fetch_assoc($Recordset2)); ?>
/////This is my update code
if(isset($_POST['selected'])){
$selected = $_POST['selected'];
$question = $_POST['questiondesc'];
$answer = $_POST['answerdesc'];
$answerid = $_POST['ansid'];
for ($i = 0; $i < sizeof($selected); $i++)
{
$sql = sprintf("UPDATE exam_questions SET question_description = '%s' WHERE question_id = '%s'",
mysql_real_escape_string($question[$i]),
mysql_real_escape_string($selected[$i]));
mysql_query($sql)or die(mysql_error());
for($x = 0; $x < sizeof($answerid); $x++){
$gomugomu = sprintf("UPDATE exam_answers SET answer_description = '%s' WHERE answer_question_set_id = '%s'",
mysql_real_escape_string($answer[$x]),
mysql_real_escape_string($answerid[$x]));
mysql_query($gomugomu)or die(mysql_error());
}
}
}
?>

Your ansid and answerdesc are not arrays
<input type="hidden" name="ansid" .....
<input name="answerdesc" ....
to
<input type="hidden" name="ansid[]" ...
<input name="answerdesc[]" ...

Related

How to disable button when returned books is equal with original books volume?

I am making a book management system.I have a return button ,with that button user can return books.
Here is my release-books.php:
<?php
$sql_select = "SELECT * FROM carti ORDER BY titlu";
$rezultat = mysqli_query($conn, $sql_select);
if (mysqli_num_rows($rezultat) > 0) {
while ($row = mysqli_fetch_assoc($rezultat)) {
$disabled = $row['stoc'] > 0 ? "" : "disabled"; ?>
<tr><td><input type="submit" name="id" value="<?php echo $row['idCarte']; ?>" <?php echo $disabled; ?> formaction="imprumutare.php"></input></td>
<td><input type="submit" name="returnare" value="<?php echo $row['idCarte']; ?>" formaction="returnare_carte.php"></input>
</td>
<td><input type="text" name="nume" value="<?php echo $row['titlu']; ?>" ></input></td>
<td><input type="text" name="" value="<?php echo $row['autor']; ?>"></input></td>
<td><input type="text" name="" value="<?php echo $row['editura']; ?>"></input></td>
<td><input type="text" name="" value="<?php echo $row['categorie']; ?>"></input></td>
<td><input type="text" name="" value="<?php echo $row['data_adaugarii']; ?>"></input></td>
<td><input type="text" name="" value="<?php echo $row['stoc']; ?>"></input></td>
</tr>
<?php
}
}
?>
And here is my borrowing.php
include('conexiune.php');
//sfarsit if
//Imprumutare
if (isset($_POST['id'])) {
$identificator = $_POST['id'];
$carte_nume = $_POST['nume'];
$current_date = date('y:m:d');
$current_date_plus_14days = date('y:m:d', strtotime("+14 days"));
$nume_carte = $_POST['nume'];
$insert_in_imprumuturi = "INSERT INTO imprumuturi(idc,nume_carte,data,termen_returnare,carti_imprumutate) VALUES('$identificator','$carte_nume','$current_date','$current_date_plus_14days','1')";
mysqli_query($conn, $insert_in_imprumuturi) or die(mysqli_error($conn));
$sql_rezervare = "UPDATE carti SET stoc=stoc-1 WHERE iDCarte='$identificator' ";
if (mysqli_query($conn, $sql_rezervare)) {
header('Refresh:0,url=emitere_carti.php');
} else {
die(mysqli_error($conn));
}
}
But I do not know how to disable the return button when the returned books volume is equal or greater that the originial volume
Can somone help me?
Check If your books volume is equal or greater than original volume, if it is. Use PHP echo to disable the Return Button.
<button type="submit" name="return"
<?php
if($returned_books_volume >= $original_books_volume){
echo 'disabled';
}
?>
>Return</button>

multiple checkbox update with row

i have this form
how do i update this record by row with checkbox ? the checkbox hold a value i sent from previous form .
this would be the database
is this right??
while ($row = mysqli_fetch_array($result)){
.
.
.
.
<td><center>
<input type="hidden" name="matric[]" value="<?php echo $matric?>"/><p>
<input class="w3-check" type="checkbox" value='$hour' name="unattend<?php echo $matric?>[]">
<label> Unattend</label></p> </center></td>
<td><center>
<input type="hidden" name="matric[]" value="<?php echo $matric?>"/><p>
<input class="w3-check" type="checkbox" value='$hour' name="mc<?php echo $matric?>[]">
<label> I have MC </label></p> </center></td> }
how would the update mysql be ?
This is the form
<?php
$i = 1;
while ($row = mysqli_fetch_array($result)) {
$id=$row['matric_number'];
?>
<form method="POST" action="attend.php">
<tr>
<td><center><?php echo $i; ?></center></td>
<td><center><?php echo $row['student_name']; ?></center></td>
<td><center><?php echo $row['matric_number']; ?></center></td>
<td><center>
<p>
<input class="w3-check" name="unattend[]" type="checkbox" id="unattend[]" value="<?php echo $id; ?>">
<label> Unattend</label></p> </center></td>
<td><center>
<p>
<input class="w3-check" name="mc[]" type="checkbox" id="mc[]" value="<?php echo $id; ?>">
<label> I have MC </label></p> </center></td>
<td><center>
<?php echo "<a href=student.php?id1=".$row['matric_number'].">"."Details"; ?></center></td>
</tr>
<?php
$i++;
}
mysqli_close ($link);
?>
</table>
The php to update the data by row is
<?php
if (isset($_POST['unattend'])) {
$hour=$_POST['hour'];
$id=$_POST['unattend'];
$N = count($id);
for($i=0; $i < $N; $i++)
{
$select = mysqli_query($link,"SELECT * FROM pbu WHERE matric_number='$id[$i]'")or die(mysql_error());
while ($row = mysqli_fetch_array($select)) {
$value=$row['unattend'];
$total=$value+$hour;
$result = mysqli_query($link,"UPDATE pbu SET unattend='$total' WHERE matric_number='$id[$i]'")or die(mysql_error());
echo "unattend is updated for matric number ".$id[$i]."<br>";
}
}
}
if (isset($_POST['mc'])){
$id1=$_POST['mc'];
$N = count($id1);
for($i=0; $i < $N; $i++)
{
$select = mysqli_query($link,"SELECT * FROM pbu WHERE matric_number='$id[$i]'")or die(mysql_error());
while ($row = mysqli_fetch_array($select)) {
$values=$row['mc'];
$totals=$values+$hour;
$results = mysqli_query($link,"UPDATE pbu SET mc='$totals' WHERE matric_number='$id1[$i]'")or die(mysql_error());
echo "mc is updated for ".$id1[$i]."<br>";
}
}
}
else{
echo "nothing inserted";
}
?>

How to update multiple rows in database on one query with calculations

How should I change the code in order to update multiple rows at once, when I click on submit?
This is the code:
<html>
<title></title>
<head>
</head>
<body>
<form action="" method="post">
<table>
<tr>
<td><h6>Seatwork 1</h6></td>
<td><h6>Seatwork 2</h6></td>
<td><h6>Seatwork 3</h6></td>
<td><h6>Quiz 1</h6></td>
<td><h6>Quiz 2</h6></td>
</tr>
<?php
$query1="SELECT * FROM tbl...";
$query = mysql_query($query1) or die(mysql_error());
while($user = mysql_fetch_assoc($query)){
$userid = $user['userid'];
$first = $user['first_name'];
$last = $user['last_name'];
$subject = $user['subject'];
$sw1 = $user['sw1'];
$sw2 = $user['sw2'];
$sw3 = $user['sw3'];
$qz1 = $user['qz1'];
$qz2= $user['qz2'];
?>
<tr>
<td><h6><?php echo $first; ?> <?php echo $last; ?></h6></td>
<!-- Written Works -->
<td><input type="text" name="sw1" value="<?php echo $ws1; ?>"></td>
<td><input type="text" name="sw2" value="<?php echo $ws2; ?>"></td>
<td><input type="text" name="sw3" value="<?php echo $ws3; ?>"></td>
<td><input type="text" name="qz1" value="<?php echo $ws4; ?>"></td>
<td><input type="text" name="qz2" value="<?php echo $ws5; ?>"></td>
</tr>
<?php } ?>
</table>
<input type="submit" name="submit" value="submit">
</form>
Query for saving the rows:
It saves but only if the value of the columns in a row are the same.
Please help me solve this problem. Thanks In advance.
<?php
if ($_POST["submit"]) {
$sw1 = $_POST['sw1'];
$sw2 = $_POST['sw2'];
$sw3 = $_POST['sw3'];
$qz1 = $_POST['qz1'];
$qz2 = $_POST['qz2'];
$WST = $sw1 + $sw2 + $sw3 + $qz1 + $qz2;
$WSP1 = ($WST / 150 )*100;
$WSP = number_format((float)$WSP1, 2, '.', '');
$sql="UPDATE tbl_quarter1 set sw1 = '$sw1' , sw2 = '$sw2',
sw3 = '$sw3', qz1 = '$qz1', qz2 = '$qz2',
WST='$WST', WSP='$WSP'";
mysql_query($sql) or die(mysql_error());
}
?>
</body>
</html>
In the form you need to pass the firstname and lastname
<input type="hidden" name="first_name" value="<?php echo $first; ?>" />
<input type="hidden" name="last_name" value="<?php echo $last; ?>" />
And in the processing script you could update the rows with respect to the firstname and lastname
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$sql="UPDATE tbl_quarter1 set sw1 = '$sw1' , sw2 = '$sw2',
sw3 = '$sw3', qz1 = '$qz1', qz2 = '$qz2', WST='$WST',
WSP='$WSP' WHERE first_name = '$first_name' AND
last_name = '$last_name' ";

trouble in submiting form in php

I'm doing a database project for university and I'm having a problem in here.
I receive from a previous page an id as $_POST['ids'] and in the form I send that same value in a hidden field so it can do a sort of a cicle.
But when I click the submit button I got a lot of errors on $service_info and no information is loaded on the page. I tried do var_dump() everything and I just can't find what is the problem in here.
<?php
//error_reporting();
require 'core/init.php';
require 'db/connect.php';
require 'functions/security.php';
?>
<html>
<head>
<title>Make a reservation</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="css/common.css">
</head>
<body>
<?php require 'parts/header.php'; ?>
<hr>
<?php
$query = "SELECT * FROM service WHERE id=" . $_POST['ids'];
if ($result = $db->query($query)) {
if ($result->num_rows) {
$service_info = $result->fetch_object();
$result->close();
}
}
$query = "SELECT name FROM tour WHERE id =" . $service_info->idtour;
if ($result = $db->query($query)) {
if ($result->num_rows) {
$tour_name = $result->fetch_object();
$result->close();
}
}
$query = "SELECT SUM(nrseats) AS res_seats FROM reservation_service WHERE idservice =" . $service_info->id;
$nr_reservations_info = $db->query($query);
$nr_reservations = $nr_reservations_info->fetch_row();
$nr_reservations_info->close();
$count = $service_info->nrseats - $nr_reservations[0];
if($count === 0){
echo "<script>alert('There are no more places available for this tour. You are being redirected for the main page!')</script>";
echo "<script>window.open('index.php','_self')</script>";
}
else{
$count = $service_info->nrseats;
}
?>
<form action="" method="POST">
<div class="registering">
<table>
<tbody>
<tr>
<td>
<label for="tname">Related tour</label>
</td>
<td>
<label for="splace"><br>Service name</label>
</td><p><br></p>
</tr>
<tr>
<td>
<input type="text" readonly="" name="tour" id="tour" required="" autofocus="" value="<?php echo $tour_name->name ?>">
</td>
<td>
<input type="text" readonly="" name="name" id="name" required="" value="<?php echo $service_info->name ?>">
</td>
</tr>
<tr>
<td>
<label for="sprice"><br>Price (€)</label>
</td>
<td>
<label for="sdescription"><br>Description</label>
</td>
</tr>
<tr>
<td>
<input type="number" name="price" id="price" readonly="" required="" value="<?php echo $service_info->price ?>">
</td>
<td>
<input type="text" name="description" id="description" required="" readonly="" value="<?php echo $service_info->description ?>">
</td>
</tr>
<tr>
<td>
<label for="sseats"><br>Seats left</label>
</td>
<td>
<label for="snreservations"><br>Number of reservations (people)</label>
</td>
</tr>
</tr>
<tr>
<td>
<input type="number" name="nrseats" id="nrseats" required="" value="<?php echo $count ?>" readonly="">
</td>
<td>
<input type="number" name="nrreservations" id="nrreservations" required="" value="1">
</td>
<td>
<input type="hidden" name="ids" required="" value="<?php $service_info->id ?>">
</td>
</tr>
</tr>
<tr>
<td colspan="2">
<label for="next"><br></label>
<input type="submit" value="Next">
</td>
</tr>
</tbody>
</table>
</div>
</form>
</body>
</html>
<?php
if (!empty($_POST)) {
if (isset($_POST['name'], $_POST['ids'], $_POST['tour'], $_POST['price'], $_POST['description'], $_POST['nrseats'], $_POST['nrreservations'])) {
$_POST = array_map("trim", $_POST);
$name = $_POST['name'];
$tour = $_POST['tour'];
$price = $_POST['price'];
$description = $_POST['description'];
$nrseats = $_POST['nrseats'];
$nrreservations = $_POST['nrreservations'];
$ids = $_POST['ids'];
if (!empty($name) && !empty($ids) && !empty($tour) && !empty($price) && !empty($description) && !empty($nrseats) && !empty($nrreservations)) {
$query = "SELECT id FROM customer WHERE email='" . $_SESSION['user_email'] . "'";
if ($result = $db->query($query)) {
$id_user = $result->fetch_object();
$result->close();
}
$query = "SELECT id FROM reservation WHERE idtour={$service_info->idtour} AND idcustomer={$id_user->id}";
if ($result = $db->query($query)) {
if ($result->num_rows) {
$id_reservation = $result->fetch_object();
$result->close();
}
}
$query = "SELECT * FROM reservation_service WHERE idservice=" . $service_info->id;
if ($result = $db->query($query)) {
if ($result->num_rows) {
$reservation_service_exists = $result->fetch_object();
if ($nrreservations < 1) {
echo "<script>alert('Your must make a reservation for, at least, one person!')</script>";
echo "<script>window.open('new_reservation_service.php','_self')</script>";
} else if ($count - $nrreservations < 0) {
echo "<script>alert('You can not make the reservation because there are only " . $count . " seats available in this tour!')</script>";
echo "<script>window.open('new_reservation_service.php','_self')</script>";
} else if ($result->num_rows) {
$query = "SELECT * FROM reservation WHERE idcustomer= '" . $id_user->id . "' AND idtour= '" . $service_info->idtour . "'";
if ($result = $db->query($query)) {
if ($result->num_rows) {
$reservation_exists = $result->fetch_object();
$result->close();
if ($reservation_exists->idcustomer === $id_user->id) {
if ($reservation_exists->id === $reservation_service_exists->idreservation) {
echo "<script>alert('You already made a reservation for this service. Please see your reservation panel!')</script>";
echo "<script>window.open('reservations.php','_self')</script>";
}
}
}
}
}
}else {
$query = "INSERT INTO reservation_service (idreservation, idservice, date, nrseats) VALUES (?, ?, NOW(), ?)";
$insert = $db->prepare($query);
$insert->bind_param('iii', $id_reservation->id, $service_info->id, $nrreservations);
$insert->execute();
echo "<script>alert('You successfully made a reservation! You are being redirected to your reservations page')</script>";
echo "<script>window.open('reservations.php','_self')</script>";
}
}
}
}
}
?>
change inside your form this input hidden you created:
<input type="hidden" name="ids" required="" value="<?php $service_info->id ?>">
to
<input type="hidden" name="ids" required="" value="<?php echo $service_info->id ?>">
If you don't echoing this value, $_POST['ids'] won't be get any value passed from form.

PHP MySQL Insert survey answers into table

If i have this HTML and PHP Code:
<?php
if(isset($_POST["submit"]))
{
$num = $_POST['number'];
for ($i = 0; $i < $num; $i++)
{
if($i==0)
{
echo '0';
$answer1 = $_POST["answer1"];
$answer2 = $_POST["answer2"];
$answer3 = $_POST["answer3"];
$answer4 = $_POST["answer4"];
$answer5 = $_POST["answer5"];
$answer6 = $_POST["answer6"];
$answer7 = $_POST["answer7"];
$answer8 = $_POST["answer8"];
}
else
{
echo 'no 0';
$answer1 = $_POST["answer1$i"];
$answer2 = $_POST["answer2$i"];
$answer3 = $_POST["answer3$i"];
$answer4 = $_POST["answer4$i"];
$answer5 = $_POST["answer5$i"];
$answer6 = $_POST["answer6$i"];
$answer7 = $_POST["answer7$i"];
$answer8 = $_POST["answer8$i"];
}
//then insert the line items into the lineitems table with the bill sequence from the adhocbills table
$sql="INSERT into surveys_completed (survey_sequence, question_seq, answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8) values ('".$_POST["survey_sequence"]."', '".$_POST["question_seq"]."', '".$answer1."', '".$answer2."', '".$answer3."', '".$answer4."', '".$answer5."', '".$answer6."', '".$answer7."', '".$answer8."')";
$rs=mysql_query($sql,$conn) or die(mysql_error());
echo $sql.'<br><br>';
}
}
?>
<?php
/*
if(mysql_real_escape_string($_GET["string"]) == '')
{
echo 'No Survey was selected';
exit();
} */
if(mysql_real_escape_string($_GET["company"]) != '')
{
//sql for company
$sql="SELECT * from surveys where company = '".mysql_real_escape_string($_GET["company"])."' and string = '".mysql_real_escape_string($_GET["string"])."' ";
}
elseif(mysql_real_escape_string($_GET["string"]) != '')
{
//sql for string
$sql="SELECT * from surveys where string = '".mysql_real_escape_string($_GET["string"])."' ";
}
$rs=mysql_query($sql,$conn) or die(mysql_error());
$survey=mysql_fetch_array($rs);
echo '<h3>'.$survey["title"].'</h3>';
?>
<form method="post" action="/home.php?id=surveys/complete_survey">
<table width="600" border="0" cellspacing="5" cellpadding="5">
<?php
$sql2="SELECT * from surveys_questions where survey_seq = '".$survey["sequence"]."' ";
$rs2=mysql_query($sql2,$conn) or die(mysql_error());
$counter=0;
while($survey_questions=mysql_fetch_array($rs2))
{
$counter++;
?>
<tr>
<td colspan="2"><strong><?php echo $counter; ?>. <?php echo $survey_questions["question"]; ?></strong>
COUNTER<input type="text" name="number" id="number" value="<?php echo $counter; ?>" />
SURVEY<input type="text" name="survey_sequence" id="survey_sequence" value="<?php echo $survey["sequence"]; ?>" />
QUESTION<input type="text" name="question_seq" id="question_seq" value="<?php echo $survey_questions["sequence"]; ?>" /></td>
</tr>
<tr>
<td><?php if($survey_questions["answer1"] != '') { echo '<input type="checkbox" name="answer1'.$counter.'" value="Y" /> '.$survey_questions["answer1"]; } ?></td>
<td><?php if($survey_questions["answer2"] != '') { echo '<input type="checkbox" name="answer2'.$counter.'" value="Y" /> '.$survey_questions["answer2"]; } ?></td>
</tr>
<tr>
<td><?php if($survey_questions["answer3"] != '') { echo '<input type="checkbox" name="answer3'.$counter.'" value="Y" /> '.$survey_questions["answer3"]; } ?></td>
<td><?php if($survey_questions["answer4"] != '') { echo '<input type="checkbox" name="answer4'.$counter.'" value="Y" /> '.$survey_questions["answer4"]; } ?></td>
</tr>
<tr>
<td><?php if($survey_questions["answer5"] != '') { echo '<input type="checkbox" name="answer5'.$counter.'" value="Y" /> '.$survey_questions["answer5"]; } ?></td>
<td><?php if($survey_questions["answer6"] != '') { echo '<input type="checkbox" name="answer6'.$counter.'" value="Y" /> '.$survey_questions["answer6"]; } ?></td>
</tr>
<tr>
<td><?php if($survey_questions["answer7"] != '') { echo '<input type="checkbox" name="answer7'.$counter.'" value="Y" /> '.$survey_questions["answer7"]; } ?></td>
<td><?php if($survey_questions["answer8"] != '') { echo '<input type="checkbox" name="answer8'.$counter.'" value="Y" /> '.$survey_questions["answer8"]; } ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="2"><input type="submit" name="submit" id="submit" value="Complete Survey" />
</tr>
</table>
</form>
So the code selects the survey from the survey table and then gets all the questions from survey_questions for the selected survey.
Each question has up to 8 answers (all checkboxes with value of Y)
The user goes though and checks the box for the answer(s) they choose so when the form is submitted, how can i make it insert into surveys_completed table each answer.
So for each question, it adds a row in the surveys_completed table.
The structure of the surveys_completed table is:
sequence
survey_sequence
question_seq
answer1
answer2
answer3
answer4
answer5
answer6
answer7
answer8
create a page called getval.php then put the below code and you have to make sure you
connect to the database using pdo thats when this code is gonna work for you .
H i hope it will help you good luck
<?php
$answer1=(isset($_POST['answer1']))? trim($_POST['answer1']): '';
$answer2=(isset($_POST['answer2']))? trim($_POST['answer2']): '';
$answer3=(isset($_POST['answer3']))? trim($_POST['answer3']): '';
$answer4=(isset($_POST['answer4']))? trim($_POST['answer4']): '';
$answer5=(isset($_POST['answer5']))? trim($_POST['answer5']): '';
$answer6=(isset($_POST['answer6']))? trim($_POST['answer6']): '';
$answer7=(isset($_POST['answer7']))? trim($_POST['answer7']): '';
$answer8=(isset($_POST['answer8']))? trim($_POST['answer8']): '';
$query="INSERT INTO surveys_completed(
answer1,answer2,answer3,answer4,answer5,answer5,answer6,answer7,answer8 )
VALUES(:answer1,:answer2,:answer3,:answer4,:answer5,:answer5,
:answer6,:answer7,:answer8)";
$insert = $con->prepare($query);
$insert->execute(array(
':answer1'=>$answer1,
':answer2'=>$answer2,
':answer3'=>$answer3,
':answer4'=>$answer4,
':answer5'=>$answer5,
':answer6'=>$answer6,
':answer7'=>$answer7,
':answer8'=>$answer8
));
?>

Categories