I am having two different database tables questions and choices where i am inserting questions in one table and multiple choices in another table where questions table id is foreign key in choices table.
Questions:
Questions_number Text
1 What is HTML?
2 What is PHP?
Choices:
id question_number is_correct text
1 1 1 markup
2 1 0 Hyext
3 1 0 Hyper text markup language
4 2 0 hsdfd
5 2 0 frfwer
6 2 1 Hypertext Preprocessor
If i am trying to edit question number 1 then i need to fetch all the details of questions,Choices and correct option as well.But when i am trying to edit the record for choices as well i am getting the same data which i am getting for question.
HTML:
<?php session_start();
include 'includes/db.php';
$id = (int)$_GET['id'];
$sql = "SELECT * FROM questions q WHERE q.question_number = $id ";
$oppointArr =array();
$result = mysqli_query($mysqli,$sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result))
{
$oppointArr = $row;
echo "Text: " . $row["text"]. "<br>";
}
} else {
echo "0 results";
}
?>
<form class="form-horizontal" action="updatequestions.php" method="post" role="form">
<?php if(isset($msg)) {?>
<div class="<?php echo $msgclass; ?>" id="mydiv" style="padding:5px;"><?php echo $msg; ?></div>
<?php } ?>
<input type='hidden' value='<?=$id;?>' name='question_number'>
<h2>Edit A Question</h1>
<div class="form-group">
<label for="questionno" class="col-sm-2 control-label">Question Number</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="<?php echo $oppointArr['question_number'];?>"
name="question_number" id="question_number" readonly>
</div>
</div>
<div class="form-group">
<label for="question" class="col-sm-2 control-label">Question</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="<?php echo $oppointArr['text'];?>" name="question_text" id="question_text">
</div>
</div>
<input type='hidden' value='<?=$id;?>' name='id'>
<h2>Edit A Choice</h1>
<div class="form-group">
<label for="choice #1" class="col-sm-2 control-label">Choice #1</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="<?php echo $oppointArr['choice1'];?>" name="choice1" id="choice1">
</div>
</div>
<div class="form-group">
<label for="choice #2" class="col-sm-2 control-label">Choice #2</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="<?php echo $oppointArr['choice2'];?>" name="choice2" id="choice2">
</div>
</div>
<div class="form-group">
<label for="choice #3" class="col-sm-2 control-label">Choice #3</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="<?php echo $oppointArr['choice3'];?>" name="choice3" id="choice3">
</div>
</div>
<div class="form-group">
<label for="Correct Choice Number:" class="col-sm-2 control-label">Correct Choice Number:</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="<?php echo $oppointArr['is_correct'];?>" name="is_correct" id="is_correct">
</div>
</div>
<div class="col-sm-offset-2">
<button type="submit" class="btn btn-default" name="submit_user" id="subject">Submit</button>
<button type="cancel" class="btn btn-raised">Cancel</button>
</div>
</form>
Updatequestions:
<?php
include 'includes/db.php';
if(isset($_POST['submit_user']))
{
$questiontext = $_POST['question_text'];
$id=$_POST['question_number'];
$correct_choice = $_POST['correct_choice'];
$choices = array();
$choices[1] = $_POST['choice1'];
$choices[2] = $_POST['choice2'];
$choices[3] = $_POST['choice3'];
$choices[4] = $_POST['choice4'];
$choices[5] = $_POST['choice5'];
$query = "UPDATE questions SET text='$questiontext' WHERE question_number = $id";
$insert_row = $mysqli->query($query) or die($mysqli->error.__LINE__);
if($insert_row) {
foreach($choices as $choice => $value){
if($value != ''){
if($correct_choice == $choice){
$is_correct = 1;
} else {
$is_correct = 0;
}
$query = "UPDATE choices SET is_correct='$is_correct', text='$value' WHERE question_number=$id";
$insert_row = $mysqli->query($query) or die($mysqli->error.__LINE__);
if($insert_row){
continue;
} else {
die('Error : ('.$mysqli->errno . ') '. $mysqli->error);
}
}
}
$msg = 'Question has been added';
}
}
?>
If i try to update the record all the fields are updating with the same data.
WARNING: Do not create SQL statements by concatenating the data with SQL. Use prepared statements.
As for your problem, you use the foreign key of the question to update choices. The key is not the primary key of choices and is not unique. Try using the unique primary key for your SQL.
Instead of this:
$query = "UPDATE choices SET is_correct='$is_correct', text='$value' WHERE question_number=$id";
try this:
$query = "UPDATE choices SET is_correct='$is_correct', text='$value' WHERE id=$choice ";
But of course you should really try to do it all over again using prepared statements instead!
<form class="form-horizontal" action="updatequestions.php" method="post" role="form">
<?php if(isset($msg)) {?>
<div class="<?php echo $msgclass; ?>" id="mydiv" style="padding:5px;"><?php echo $msg; ?></div>
<?php } ?>
<input type='hidden' value='<?=$id;?>' name='question_number'>
<h2>Edit A Question</h1>
<div class="form-group">
<label for="questionno" class="col-sm-2 control-label">Question Number</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="<?php echo $oppointArr['question_number'];?>"
name="question_number" id="question_number" readonly>
</div>
</div>
<div class="form-group">
<label for="question" class="col-sm-2 control-label">Question</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="<?php echo $oppointArr['text'];?>" name="question_text" id="question_text">
</div>
</div>
<input type='hidden' value='<?=$id;?>' name='id'>
<h2>Edit A Choice</h1>
<?php
$choicesql = "SELECT * FROM `choices` WHERE question_number = $id ";
$ChoicetArr =array();
$choiceresult = mysqli_query($mysqli,$choicesql);
$inc=1;
$correctAns ="";
if (mysqli_num_rows($choiceresult) > 0)
{
while($rows = mysqli_fetch_array($choiceresult))
{
$ChoicetArr[] = $rows;
?>
<div class="form-group">
<label for="choice #<?php echo $inc;?>" class="col-sm-2 control-label">Choice #<?php echo $inc;?></label>
<div class="col-sm-5">
<input type="hidden" name="choice_id<?php echo $inc;?>" value="<?php echo $rows['id'];?>">
<input type="text" class="form-control" value="<?php echo $rows['text'];?>" name="choice<?php echo $inc;?>" id="choice<?php echo $inc;?>">
</div>
</div>
<?php
//print_r($rows);
if($rows['is_correct']=="1"){
$correctAns = '<input type="hidden" name="choice_id'.$inc.'" value="'.$rows['id'].'"><div class="form-group">
<label for="Correct Choice Number:" class="col-sm-2 control-label">Correct Choice Number:</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="'.$inc.'" name="is_correct" id="is_correct">
</div>
</div>';
}
$inc++;
}
}
echo $correctAns;
?>
<div class="col-sm-offset-2">
<button type="submit" class="btn btn-default" name="submit_user" id="subject">Submit</button>
<button type="cancel" class="btn btn-raised">Cancel</button>
</div>
</form>
updatequestions.php
<?php
include 'includes/db.php';
if(isset($_POST['submit_user']))
{
$questiontext = $_POST['question_text'];
$id=$_POST['question_number'];
$correct_choice = $_POST['is_correct'];
$choices = array();
$choices[] = array("question"=>$_POST['choice1'], "answer"=>$_POST['choice_id1']);
$choices[] = array("question"=>$_POST['choice2'], "answer"=>$_POST['choice_id2']);
$choices[] = array("question"=>$_POST['choice3'], "answer"=>$_POST['choice_id3']);
$choices[] = array("question"=>$_POST['choice4'], "answer"=>$_POST['choice_id4']);
$choices[] = array("question"=>$_POST['choice5'], "answer"=>$_POST['choice_id5']);
$query = "UPDATE questions SET text='$questiontext' WHERE question_number = $id";
$insert_row = $mysqli->query($query) or die($mysqli->error.__LINE__);
if($insert_row)
{
$inc= 0;
foreach($choices as $choice => $value){
if(count($value)>0){
$answerInc = $choice+1;
if($correct_choice == $answerInc){
$is_correct = 1;
} else {
$is_correct = 0;
}
$text= $value['question'];
$answer = $value['answer'];
//echo "<br>".$text;
//print_r($value);
echo $answerInc;
echo "<br>";
echo $query = "UPDATE choices SET is_correct='$is_correct', text='$text' WHERE id=$answer";
$insert_row = $mysqli->query($query) or die($mysqli->error.__LINE__);
if($insert_row){
continue;
} else {
die('Error : ('.$mysqli->errno . ') '. $mysqli->error);
}
}
$inc++;
}
$msg = 'Question has been Updated Successfully';
header("location:searchquestions.php");
exit;
}
}
?>
hello everyone I am just starting PHP this year and I try to make form that is content 2 part first part main data second part it content table that I can add new row to enter data and this row content multi-select input and I try many times to write PHP code that I can update this form but I failed every time I try
<div class="modal-body">
<div class="form-group">
<label class="control-label col-sm-2" for="shop_name">اسم المحل:
<a style="color: red">*</a>
</label>
<div class="col-sm-3">
<input type="text" class="form-control" id="shop_name" value="<?php echo $strshop; ?>" name="shop_name" placeholder="اسم المحل" data-index="8">
</div>
<label class="control-label col-sm-2" for="cus_name">اسم العميل:
</label>
<div class="col-sm-3">
<input type="text" class="form-control" id="cus_name" name="cus_name" value="<?php echo $strcus; ?>" placeholder="اسمع العميل" data-index="9">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="cus_address">العنوان:<a style="color: red">*</a></label>
<div class="col-sm-3">
<input type="text" class="form-control" id="cus_address" name="cus_address" value="<?php echo $straddres; ?>" placeholder="عنوان" data-index="10">
</div>
<label class="control-label col-sm-2" for="tel"> التليفون:</label>
<div class="col-sm-3">
<input type="text" class="form-control" id="tel" name="tel" value="<?php echo $strtel; ?>" placeholder="تليفون" data-index="11">
</div>
</div>
</div>
<div class="modal-footer">
<h2>مياه</h2>
<div class="row clearfix">
<div class="col-md-12 column">
<?php
if (!empty($strid)) {
$q2 = "SELECT * FROM product_table WHERE form_id = $strid;";
$result = mysqli_query($con, $q2);
$count = mysqli_num_rows($result);
?>
<table class="table table-bordered table-hover" id="data_table">
<thead>
<tr>
<th class="text-center">
المنتج
</th>
<th class="text-center">
SKus
</th>
<th class="text-center">
الكمية
</th>
<th class="text-center">
ثلاجات
</th>
<th class="text-center">
مواد دعائية
</th>
<th class="text-center">
كمية الدعايه
</th>
<th>
action
</th>
</tr>
</thead>
<tbody>
<?php
if ($count > 0) {
while ($wrow = mysqli_fetch_array($result)) {
?>
<tr id='row'>
<td>
<input hidden name="wid" value="<?php echo $wrow['id'] ?>">
<select class="form-control" id="product_name" name="product_name[]" data-index="12">
<option selected disabled hidden>اختار المنتج .....</option>
<?php
$select_water = $con->query("select * from water");
if ($select_water->num_rows > 0) {
// output data of each row
while ($w = $select_water->fetch_assoc()) {
?>
<option <?php if ($wrow['product_name'] == $w['water_name']) echo "selected";?> ><?php echo $w['water_name']?></option>
<?php
}
} else {
echo "0 results";
}
?>
</select>
</td>
<td>
<select class="form-control" id="sk" name="sk[]"
data-index="6">
<option selected disabled hidden>اختار Skus</option>
<?php
$select_area = $con->query("select * from watersk");
if ($select_area->num_rows > 0) {
// output data of each row
while ($row2 = $select_area->fetch_assoc()) {
?>
<option <?php if ($wrow['sk'] == $row2['sk_name']) echo "selected"; ?> ><?php echo $row2 ['sk_name'] ?></option>
<?php
}
} else {
echo "0 results";
}
?>
</select>
</td>
<td>
<input type="number" id="qty" name="qty[]" value="<?php echo $wrow['qty']; ?>" placeholder='الكميه' class="form-control" data-index="16"/>
</td>
<td>
<input type="number" id="friz" name="friz[]" value="<?php echo $wrow['frizer']; ?>" placeholder='ثلاجه' class="form-control" data-index="17"/>
</td>
<td>
<?php
// var_dump($row['id']);
$water_id = $wrow['id'];
$test_array=array();
$select_water_market = $con->query("SELECT * FROM product_market WHERE water_id = $water_id;");
while ($row3 = $select_water_market->fetch_assoc()) {
$test_array[]= $row3['watermarket_media'];
}
// var_dump($test_array);
?>
<select multiple class="form-control" id="market_medi" name="market_media[0][]" data-index="6">
<?php
$select_media = $con->query("select * from market_media");
if ($select_media->num_rows > 0) {
// output data of each row
while ($row = $select_media->fetch_assoc()) {
?>
<?php if(in_array($row['media_name'],$test_array)){?>
<option selected>
<?php echo $row ['media_name'] ?>
</option>
<?php
}
else
{?>
<option>
<?php echo $row ['media_name'] ?>
</option>
<?php }
}
}else {
echo "0 results";
}
?>
</select>
</td>
<td>
<input style="float: right" type="number" id="market" name="market[]" placeholder="مواد دعائيه" class="form-control" value="<?php echo $wrow['mark_qty']; ?>" data-index="18"/>
</td>
<td>
<a class="btn btn-warning btn-sm" href='save/deleterow.php?id=".$delete['id']." data-toggle="modal" style="float: left;"><span class="glyphicon glyphicon-pencil"></span></a>
<a class="btn btn-danger btn-sm" href='save/deleterow.php?id=<?php echo $wrow['id'];?>&stid=<?php echo $strid;?>' data-toggle="modal" style="float: left;"><span class="glyphicon glyphicon-remove"></span></a>
</td>
</tr>
<?php
}
}
}
?>
</tbody>
</table>
<a class="btn btn-success btn-sm" href="" data-toggle="modal" onclick="addVariablesRow();" style="float: left;"><span class="glyphicon glyphicon-plus"></span></a>
</div>
<script>
function deleterow2(o) {
//no clue what to put here?
var p = o.parentNode.parentNode;
p.parentNode.removeChild(p);
}
var i = 1;
function addVariablesRow() {
var $t = $("#data_table");
var $row = $t.find("tbody tr").first();
var $r = $row.clone();
$r.attr('id', 'rrow' + i);
var $newRow = $r.find('input').val('').end().append("<td><a class='btn btn-danger btn-sm delete' onclick='deleterow2(this);' style='float: left;'><span class='glyphicon glyphicon-trash'></span></a></td>");
$newRow.appendTo($t).show();
$("#data_table tr:last td:first select").focus();
$('#market_medi').each(function () {
$(this).attr('name', 'market_media[' + i + '][]');
});
i++;
}
</script>
</div>
</div>
Can anybody help me understand why this update query isn't updating the fields in db.
<?php
$id= $_GET['id'];
$query1 = mysql_query("SELECT * FROM invoice WHERE id=$id limit 1");
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="card">
<div class="card-header">
<strong>Create Tax</strong>
</div> <?php
while($query2=mysql_fetch_array($query1)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
?>
<div class="card-block">
<div class="col-sm-6">
<div class="form-group">
<label for="company">Coustomer Name</label>
<input type="text" class="form-control" id="company" name="cname" value="<?php echo $query2['CustomerName']?>" placeholder="Coustomer Name">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="company">Coustomer Mobile </label>
<input type="text" class="form-control" id="company" name="mobile" value="<?php echo $query2['CustomerMobile']?>" placeholder="Coustomer Mobile">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="company">Email</label>
<input type="text" class="form-control" id="company" name="email" value="<?php echo $query2['Email']?>" placeholder="Email">
</div>
</div> <div class="col-sm-6">
<div class="form-group">
<label for="company">Coustomer Streat </label>
<input type="text" class="form-control" id="company" name="streat" value="<?php echo $query2['CustomerStreat']?>" placeholder="Coustomer Streat">
</div>
</div> <div class="col-sm-6">
<div class="form-group">
<label for="company">Coustomer City</label>
<input type="text" class="form-control" id="company" name="city" value="<?php echo $query2['CustomerCity']?>" placeholder="Coustomer City">
</div>
</div> <div class="col-sm-6">
<div class="form-group">
<label for="company">Coustomer State</label>
<input type="text" class="form-control" id="company" name="state" value="<?php echo $query2['CustomerState']?>" placeholder="Coustomer State">
</div>
</div> <div class="col-sm-6">
<div class="form-group">
<label for="company">Coustomer Country</label>
<input type="text" class="form-control" id="company" name="country" value="<?php echo $query2['CustomerCountry']?>" placeholder="Coustomer Country">
</div>
</div> <div class="col-sm-6">
<div class="form-group">
<label for="company">Zip/Pin Code</label>
<input type="text" class="form-control" id="company" name="pin" value="<?php echo $query2['ZipCode']?>" placeholder="ZipCode">
</div>
</div>
<?php } ?>
<div class="col-sm-6">
<div class="form-group">
<label for="company">Tax Name </label><?php
$query1 = mysql_query("SELECT * FROM Tax");
?>
<select id="select" name="tax" class="form-control" size="1"><?php
while($query2=mysql_fetch_array($query1)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
?>
<option value="<?php echo $query2['NameOfTax']?> , <?php echo $query2['TaxPercentage']?>"><?php echo $query2['NameOfTax']?> , <?php echo $query2['TaxPercentage']?></option>
<?php $taxvalue = $query2['TaxPercentage']; ?> <?php } ?>
</select>
</div>
</div> <div class="col-sm-6">
<div class="form-group">
<label for="company">Iteam Name </label>
<input type="text" class="form-control" id="company" name="item" placeholder="Iteam Name" value="<?php echo $query2['ItemName']?>">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="company">Iteam Price </label>
<input type="text" class="form-control" id="company" name="itemprice" placeholder="Iteam price" value="<?php echo $query2['ItemPrice']?>">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="company">Paid Ammount </label>
<input type="text" class="form-control" id="company" name="pammount" placeholder="Paid Ammount" value="<?php echo $query2['PaidAmmount']?>">
</div>
</div> <div class="col-sm-6">
<div class="form-group">
<label for="company">Date</label>
<input type="text" class="form-control" id="company" name="date" placeholder="Date" value="<?php echo $query2['Date']?>">
</div>
</div>
<div class="col-sm-12">
<input type="submit" class="btn btn-outline-success" name="submit" value="Processe Invoice"/>
</div>
</div>
</div>
</div>
</div>
<!--/col-->
</form>
<?php
$cname = $_POST['cname'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$streat = $_POST['streat'];
$city = $_POST['city'];
$state = $_POST['state'];
$country = $_POST['country'];
$pin = $_POST['pin'];
$tax = $_POST['tax'];
$item = $_POST['item'];
$itemprice = $_POST['itemprice'];
$percent = ($taxvalue / 100) * $itemprice ;
$tammount = $percent + $itemprice;
$pammount = $_POST['pammount'];
$uammount = $tammount - $pammount;
$date = $_POST['date'];
$ip = $_SERVER['REMOTE_ADDR'];
$date1 = date('h,i,s');
if(isset($_POST['submit']))
{
$sql = "UPDATE `ero`.`invoice` SET `CustomerName` = '$cname', `CustomerMobile` = '$mobile', `Email` = '$email', `CustomerStreat` = '$streat', `CustomerCity` = '$city', `CustomerState` = '$state', `CustomerCountry` = '$country', `ZipCode` = '$pin', `TaxName` = '$tax', `ItemName` = '$item', `ItemPrice` = '$itemprice', `TotalAmmount` = '$tammount', `PaidAmmount` = '$pammount', `UnpaidAmmount` = '$uammount', `Date` = '$date', `IP` = '$ip', `DateTime` = '$date1' WHERE `invoice`.`id` = '$id'";
$result = mysql_query($sql);
if($result)
$url='customer.php';
echo '<script>window.location = "'.$url.'";</script>';
die;
}
?>
invoice.php
<?php
$query1 = mysql_query("SELECT * FROM invoice");
?>
<table class="table table-bordered table-striped table-condensed">
<thead>
<tr>
<th>Customer Name</th>
<th>Mobile</th>
<th>Email</th>
<th>City</th>
<th>Item Name</th>
<th>Item Price</th>
<th>Tax</th>
<th>Total Ammount</th>
<th>Paid Ammount</th>
<th>Unpaid Ammount</th>
<th>Action</th>
</tr>
</thead>
<tbody><?php
while($query2=mysql_fetch_array($query1)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
?>
<tr>
<td><?php echo $query2['CustomerName']?></td>
<td><?php echo $query2['CustomerMobile']?></td>
<td><?php echo $query2['Email']?></td>
<td><?php echo $query2['CustomerCity']?></td>
<td><?php echo $query2['ItemName']?></td>
<td><?php echo $query2['ItemPrice']?></td>
<td><?php echo $query2['TaxName']?></td>
<td><?php echo $query2['TotalAmmount']?></td>
<td><?php echo $query2['PaidAmmount']?></td>
<td <?php if ($query2['UnpaidAmmount'] > 1) echo 'style="background-color:#FF0000"' ?>><?php if ($query2['UnpaidAmmount'] < 1) echo "Ammount Paid"; else echo $query2['UnpaidAmmount'];?></td>
<td>
<button type="button" class="btn btn-outline-danger btn-sm"><a href="DeleteInvoice.php?id='<?php echo $query2['id'] ?>'" >Delete</a>
<button type="button" class="btn btn-outline-success btn-sm"><a href="ViewInvoice.php?id='<?php echo $query2['id'] ?>'" >View</a></button>
<button type="button" class="btn btn-outline-success btn-sm"><a href="updatecheck.php?id='<?php echo $query2['id'] ?>'" >Update</a></button>
</div> </td> </tr> <?php } ?>
<tr>
this scripts redirects customer.php.if i place a manual id like id = 38. its wroking. but there is no update in MySql Table.i am new to programming please explain with some code examples.
Thanks
Replace single quote in the query with double quotes and since you are already using the double quotes for the query, escape the double quotes for the variables or use string concatenation.
<?php
$id = $_GET['id'];
$cname = $_POST['cname'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$streat = $_POST['streat'];
$city = $_POST['city'];
$state = $_POST['state'];
$country = $_POST['country'];
$pin = $_POST['pin'];
$tax = $_POST['tax'];
$item = $_POST['item'];
$itemprice = $_POST['itemprice'];
$percent = ($taxvalue / 100) * $itemprice;
$tammount = $percent + $itemprice;
$pammount = $_POST['pammount'];
$uammount = $tammount - $pammount;
$date = $_POST['date'];
$ip = $_SERVER['REMOTE_ADDR'];
$date1 = date('h,i,s');
if (isset($_POST['submit']))
{
$sql = "UPDATE `ero`.`invoice` SET `CustomerName` = \"$cname\", `CustomerMobile` = \"$mobile\", `Email` = \"$email\", `CustomerStreat` = \"$streat\", `CustomerCity` = \"$city\", `CustomerState` = \"$state\", `CustomerCountry` = \"$country\", `ZipCode` = \"$pin\", `TaxName` = \"$tax\", `ItemName` = \"$item\", `ItemPrice` = \"$itemprice\", `TotalAmmount` = \"$tammount\", `PaidAmmount` = \"$pammount\", `UnpaidAmmount` = \"$uammount\", `Date` = \"$date\", `IP` = \"$ip\", `DateTime` = \"$date1\" WHERE `invoice`.`id` = \"$id\"";
$result = mysql_query($sql);
if ($result) $url = 'customer.php';
echo '<script>window.location = "' . $url . '";</script>';
die;
?>
UPDATE table
SET col_name = 'new value'
WHERE condition
this is the syntax for updating in sql, try deleting the ``
I'm trying to populate a drop down from values in DB on the same page which is from input on the same page itself.
I have tried a few options on the forum but it did not help.
After hitting submit it goes to a next page which is blank.
I have tried onchange=AjaxFunction(); as suggested in one post, but i still get a blank page.
Any help is appreciated.
This is my form.php
<form action="connection.php" class="form-solid-blue" method="get">
<div class="title">
<h2></h2>
<h2>Tracking & Receiving</h2></div>
<div class="element-input<?php frmd_add_class("input2"); ?>">
<label class="title"></label>
<div class="item-cont">
<input class="small" type="text" name="store" placeholder="Store #"/>
<span class="icon-place"></span>
</div>
</div>
<div class="element-input<?php frmd_add_class("input"); ?>">
<label class="title"></label>
<div class="item-cont">
<input class="medium" type="text" name="userid" placeholder="UserId"/>
<span class="icon-place"></span>
</div>
</div>
<div class="element-input<?php frmd_add_class("input1"); ?>">
<label class="title"></label>
<div class="item-cont">
<input class="large" type="text" name="order" placeholder="Order Number"/>
<span class="icon-place"></span>
</div>
</div>
<div class="submit">
<input type="submit" value="Send"/>
</div>
<div class="element-separator">
<hr>
<h3 class="section-break-title">Tracking Numbers</h3>
</div>
<div class="element-multiple<?php frmd_add_class("multiple"); ?>">
<label class="title"></label>
<div class="item-cont">
<div class="large">
<select data-no-selected="Nothing selected" name="multiple[]" multiple="multiple">
<option value="option_1">option 1</option>
<option value="option_2">option 2</option>
<option value="option_3">option 3</option>
</select>
<span class="icon-place"></span>
</div>
</div>
</div>
<div class="submit">
<input type="submit" value="Submit"/>
</div>
</form>
This is the connection.php - Server side code
<?php
// Create connection to Oracle
$conn = oci_connect("XXXX", "xyxyx", "xyxyx");
if (!$conn) {
$m = oci_error();
echo $m['message'], "\n";
exit;
}
$query = "SELECT TRACKING_NUMBER FROM JC_SHIPPED_ORDER_TRACKING WHERE EXT_PURCHASE_ORDERS_ID = :order_bv";
$stid = oci_parse($conn, $query);
$order = $_GET['order'];
oci_bind_by_name($stid, ':order_bv', $order);
oci_execute($stid);
//Because order is a unique value I only expect one row
$row = oci_fetch_array($stid, OCI_ASSOC);
if (!$row) {
exit("The order " . $order . " is invalid. Please check and try again" );
}
$trackID = $row['TRACKING_NUMBER'];
echo "<form name=form1 method=POST action='form.php'>";
//echo "<select name='TRACKING_NUMBER' onchange=AjaxFunction();>";
while ($row = oci_fetch_array($stid)) {
echo "<option value=\"option_1\">" . $row['TRACKING_NUMBER'] . "</option>";
}
echo "</select>";
//echo ("The order " . $order . " is valid.");
oci_free_statement($stid);
oci_close($conn);
?>
This is the best I can do with the information provided.
in form.php
<select data-no-selected="Nothing selected" name="multiple[]" multiple="multiple">
<?php require 'path to connection.php'; ?>
</select>
oh and remove these from connection.php
echo "<form name=form1 method=POST action='form.php'>";
echo "</select>";
I am creating a multiple choice quiz and I don't know how to insert multiple arrays to database. I can insert the first 2 arrays using array_combine but cannot on the 3rd, 4th, 5th, and 6th array. I can insert questions and answers but not the options A, B, C, and D.
I'm trying to do something like
foreach(array_combine($_POST['inQuestion'], $_POST['inAnswer'], $_POST['inA'], $_POST['inB'], $_POST['inC'], $_POST['inD']) as $question => $answer => $A => $B => $C => $D) {
Please help. Here is my code.
<?php
if(isset($_POST['btnCreate'])) {
$inQuestion = array($_POST['inQuestion']);
$inAnswer = array($_POST['inAnswer']);
$inA = array($_POST['inA']);
$inB = array($_POST['inB']);
$inC = array($_POST['inC']);
$inD = array($_POST['inD']);
$inLesson = $_POST['inLesson'];
$inQuizNo = $_POST['inQuizNo'];
$sql = "SELECT * FROM lessons WHERE title='$inLesson'";
$query = mysql_query ($sql);
$row = mysql_fetch_assoc($query);
$lessonID = $row['lessonID'];
foreach(array_combine($_POST['inQuestion'], $_POST['inAnswer']) as $question => $answer) {
$sql = "INSERT INTO `test` (question, answer, A, B, C, D, lessonID, quizNo) VALUES ('$question', '$answer', '$A', '$B', '$C', '$D', '$lessonID', $inQuizNo)";
$query = mysql_query( $sql );
}
}
?>
<div class="panel panel-info">
<div class="panel-heading">
<h4>Create Assessment - Multiple Choice</h4>
</div>
<div class="panel-body">
<form method="post">
<br>
<div>
<div class="col-md-4">
<strong>Lesson</strong><br>
<select class="form-control" name="inLesson">
<option></option>
<option disabled></option>
<?php
$sql2 = "SELECT * FROM lessons WHERE courseID='$_GET[courseID]'";
$query2 = mysql_query ($sql2);
while ($row2 = mysql_fetch_assoc($query2)) {
?>
<option><?php echo $row2['title']; ?></option>
<?php } ?>
</select>
</div>
<div class="col-md-4">
<strong>Quiz No.</strong><br>
<input required type="number" class="form-control" name="inQuizNo" min="1" max="100">
</div>
<div class="col-md-4">
<div class="pull-right">
<br>
<input type="button" value="Add" id="addButton" class="btn btn-info">
<input type="button" value="Remove" id="removeButton" class="btn btn-warning">
</div>
</div>
</div>
<br><br><br><br><br>
<div id='TextBoxesGroup'>
<div id="TextBoxDiv1" class="form-group">
<h2><span class="label label-primary">No. 1</span></h2>
<br><br>
<div class="col-md-10">
<label>Question</label><input type='text' id='textbox1' name='inQuestion[]' class="form-control">
</div>
<div class="col-md-2">
<label>Answer</label><input type='text' id='textbox1' name='inAnswer[]' class="form-control">
<br>
</div>
<div class="col-md-3">
<label>A</label><input type='text' id='textbox1' name='inA[]' class="form-control">
</div>
<div class="col-md-3">
<label>B</label><input type='text' id='textbox1' name='inB[]' class="form-control">
</div>
<div class="col-md-3">
<label>C</label><input type='text' id='textbox1' name='inC[]' class="form-control">
</div>
<div class="col-md-3">
<label>D</label><input type='text' id='textbox1' name='inD[]' class="form-control">
<br><br>
</div>
</div>
</div>
<div class="col-md-12">
<button type="submit" name="btnCreate" class="btn btn-success pull-right">Create Quiz
</div>
</form>
</div>
From what I understood, you are trying to insert multible rows INTO your database.
First, when you use the [] in HTML forms you don't have to declare it as an array in php
Second, each question will have a question name, answer, A,B,C,D
When you trying to create the HTML form you could use this format to keep track the question_id or number.
<label>Question</label><input type='text' id='textbox1' name='inQuestion[1]' class="form-control">
Then you will be able to retrieve this item as $_POST['inQuestion'][1]
For Example, if you need 10 questions in your test page you can do it like this:
<?php for($i = 0; $i <= 10; $i++) : ?>
<form method="post">
<div class="col-md-10">
<label>Question</label><input type='text' id='textbox1' name='inQuestion[<?=$i;?>]' class="form-control">
</div>
<div class="col-md-2">
<label>Answer</label><input type='text' id='textbox1' name='inAnswer[<?=$i;?>]' class="form-control">
<br>
</div>
<div class="col-md-3">
<label>A</label><input type='text' id='textbox1' name='inA[<?=$i;?>]' class="form-control">
</div>
<div class="col-md-3">
<label>B</label><input type='text' id='textbox1' name='inB[<?=$i;?>]' class="form-control">
</div>
<div class="col-md-3">
<label>C</label><input type='text' id='textbox1' name='inC[<?=$i;?>]' class="form-control">
</div>
<div class="col-md-3">
<label>D</label><input type='text' id='textbox1' name='inD[<?=$i;?>]' class="form-control">
<br><br>
</div>
<input type="submit" value="go">
</form>
<?php endfor; ?>
Also, why do you put the content of the Answers A,B,C,D to an array while you have them is columns in your database. Also use these variables in your query too.
$inA = $_POST['inA'];
$inB = $_POST['inB'];
$inC = $_POST['inC'];
$inD = $_POST['inD'];
Finally Let's try to insert your values now.
if($_POST['btnCreate']){
// The form has been posted!
$inLesson = $_POST['inLesson'];
$inQuizNo = $_POST['inQuizNo'];
$questionsCount = count($_POST['inQuestion']);
$items = array();
// Since we should have the same size of arrays.
// Also the empty validation has been handled before this step!
for($i = 0; $i < $questionsCount; $i++){
$temp = array();
$temp['inQuestion'] = $_POST['inQuestion'][$i];
$temp['inAnswer'] = $_POST['inAnswer'][$i];
$temp['inA'] = $_POST['inA'][$i];
$temp['inB'] = $_POST['inB'][$i];
$temp['inC'] = $_POST['inC'][$i];
$temp['inD'] = $_POST['inD'][$i];
$items[] = $temp;
}
// Now items should have an array of all questions.
foreach($items as $item){
$SQL = 'INSERT INTO `test` (question, answer, A, B, C, D, lessonID, quizNo) VALUES (
"'.$item['inQuestion'].'",
"'.$item['inAnswer'].'",
"'.$item['inA'].'",
"'.$item['inB'].'",
"'.$item['inC'].'",
"'.$item['inD'].'",
"'.$inLesson.'",
"'.$inQuizNo.'")';
$query = mysql_query( $SQL );
} // End Foreach
}
Try This
<?php
if(isset($_POST['btnCreate'])) {
$inQuestion = array($_POST['inQuestion']);
$inAnswer = array($_POST['inAnswer']);
$inA = $_POST['inA'] ? $_POST['inA'] : null;
$inB = $_POST['inB'] ? $_POST['inB'] : null;
$inC = $_POST['inC'] ? $_POST['inC'] : null;
$inD = $_POST['inD'] ? $_POST['inD'] : null;
$inLesson = $_POST['inLesson'];
$inQuizNo = $_POST['inQuizNo'];
$sql = "SELECT * FROM lessons WHERE title='$inLesson'";
$query = mysql_query ($sql);
$row = mysql_fetch_assoc($query);
$lessonID = $row['lessonID'];
$sql = "INSERT INTO `test` (question, answer, A, B, C, D, lessonID, quizNo) VALUES ('$question', '$answer', '$inA', '$inB', '$inC', '$inD', '$lessonID', $inQuizNo)";
$query = mysql_query( $sql );
}
?>
<div class="panel panel-info">
<div class="panel-heading">
<h4>Create Assessment - Multiple Choice</h4>
</div>
<div class="panel-body">
<form method="post">
<br>
<div>
<div class="col-md-4">
<strong>Lesson</strong><br>
<select class="form-control" name="inLesson">
<option></option>
<option disabled></option>
<?php
$sql2 = "SELECT * FROM lessons WHERE courseID='$_GET[courseID]'";
$query2 = mysql_query ($sql2);
while ($row2 = mysql_fetch_assoc($query2)) {
?>
<option><?php echo $row2['title']; ?></option>
<?php } ?>
</select>
</div>
<div class="col-md-4">
<strong>Quiz No.</strong><br>
<input required type="number" class="form-control" name="inQuizNo" min="1" max="100">
</div>
<div class="col-md-4">
<div class="pull-right">
<br>
<input type="button" value="Add" id="addButton" class="btn btn-info">
<input type="button" value="Remove" id="removeButton" class="btn btn-warning">
</div>
</div>
</div>
<br><br><br><br><br>
<div id='TextBoxesGroup'>
<div id="TextBoxDiv1" class="form-group">
<h2><span class="label label-primary">No. 1</span></h2>
<br><br>
<div class="col-md-10">
<label>Question</label><input type='text' id='textbox1' name='inQuestion[]' class="form-control">
</div>
<div class="col-md-2">
<label>Answer</label><input type='text' id='textbox1' name='inAnswer[]' class="form-control">
<br>
</div>
<div class="col-md-3">
<label>A</label><input type='text' id='textbox1' name='inA' class="form-control">
</div>
<div class="col-md-3">
<label>B</label><input type='text' id='textbox1' name='inB' class="form-control">
</div>
<div class="col-md-3">
<label>C</label><input type='text' id='textbox1' name='inC' class="form-control">
</div>
<div class="col-md-3">
<label>D</label><input type='text' id='textbox1' name='inD' class="form-control">
<br><br>
</div>
</div>
</div>
<div class="col-md-12">
<button type="submit" name="btnCreate" class="btn btn-success pull-right">Create Quiz
</div>
</form>
</div>
rather than posting a detailed solution, play with how to insert multiple rows to get some ideas
Something like:
$_POST['inQuestion'] = array(
'q1','q2','q3' // from input array name 'inQuestion[]'
);
$_POST['inAnswer'] = array(
'a1','a2','a3' // from input array name 'inAnswer[]'
);
$_POST['other'] = 'sunshine';
$_POST['yet_another'] = 'nice weather';
foreach($_POST['inQuestion'] as $k => $v){
$inserts[] = "(".$v.",".$_POST['inAnswer'][$k].",".$_POST['other'].",".$_POST['yet_another'].")";
}
echo "INSERT INTO table col_1, col_2, col_3, col_4 VALUES ".implode(',',$inserts)."";
// INSERT INTO table col_1, col_2, col_3, col_4 VALUES (q1,a1,sunshine,nice weather),(q2,a2,sunshine,nice weather),(q3,a3,sunshine,nice weather)