mysql update multiple column in multiple rows - php

i am trying to update multiple data in both rows and columns in my table where client_id = '5' and invoice_number = '11'
My Table tbl_particulars
My code:
<table class="table table-bordered">
<tr>
<th>Date</th>
<th>Car No.</th>
<th>Particulars</th>
<th>Rate</th>
<th>Amount</th>
</tr>
<?php
$sql = "SELECT * FROM tbl_particulars WHERE client_id = '5' AND invoice_number = '11'";
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query)) {
?>
<tr>
<td width="200px" nowrap="nowrap"><table>
<tr>
<td>From<br>
<input type="hidden" name="numbers" value="3">
<input type="hidden" name="ids[]" value="<?php echo $row['id'] ?>">
<input type="text" name="date_from[]" value="<?php echo $row['date_from'] ?>" class="form-control" placeholder="DD/MM/YYYY"></td>
<td> </td>
<td>To<br>
<input type="text" name="date_to[]" value="<?php echo $row['date_to'] ?>" class="form-control" placeholder="DD/MM/YYYY"></td>
</tr>
</table></td>
<td width="100px"><input type="text" name="car_no[]" value="<?php echo $row['car_no'] ?>" class="form-control"></td>
<td width="250px"><table>
<tr>
<td><select name="particulars[]" class="form-control">
<option value="">--Please Select ur Duty--</option>
<option value="Local 80 Kms. & 8.00 Hrs. Duty" <?php if($row['particulars']=="Local 80 Kms. & 8.00 Hrs. Duty") echo 'selected="selected"'; ?> >Local 80 Kms. & 8.00 Hrs. Duty</option>
<option value="Upto" <?php if($row['particulars']=="Upto") echo 'selected="selected"'; ?>>Upto</option>
<option value="Extra Kms." <?php if($row['particulars']=="Extra Kms.") echo 'selected="selected"'; ?>>Extra Kms.</option>
<option value="Extra Hrs." <?php if($row['particulars']=="Extra Hrs.") echo 'selected="selected"'; ?> >Extra Hrs.</option>
<option value="Local Nights" <?php if($row['particulars']=="Local Nights") echo 'selected="selected"'; ?> >Local Nights</option>
<option value="Parking Toll Tax" <?php if($row['particulars']=="Parking Toll Tax") echo 'selected="selected"'; ?> >Parking Toll Tax</option>
<option value="Out Station" <?php if($row['particulars']=="Out Station") echo 'selected="selected"'; ?> >Out Station</option>
</select></td>
<td style="vertical-align:text-top !important">  #   </td>
<td width="100px"><input type="text" value="<?php echo $row['quantity'] ?>" name="quantity[]" class="form-control"></td>
</tr>
</table></td>
<td width="80px"><input type="text" name="rate[]" class="form-control" value="<?php echo $row['rate'] ?>" onChange="CalcGST()"></td>
<td width="80px"><input type="text" name="amount[]" class="form-control" value="<?php echo $row['amount'] ?>" onChange="CalcGST()"></td>
</tr>
<?php } ?>
<tr>
<td colspan="5"><button type="submit" name="update_invoicee" class="btn btn-info">Save & Continue</button></td>
</tr>
</table>
</div>
</form>
My Php Script:
<?php if(isset($_POST['update_invoicee'])){
$s = "UPDATE `tbl_particulars` SET";
for($i=0;$i<$_REQUEST['numbers'];$i++)
{
$s .="(
date_from='".$_REQUEST['date_from'][$i]."',
date_to='".$_REQUEST['date_to'][$i]."',
car_no='".$_REQUEST['car_no'][$i]."',
particulars='".$_REQUEST['particulars'][$i]."',
quantity='".$_REQUEST['quantity'][$i]."',
rate='".$_REQUEST['rate'][$i]."',
amount='".$_REQUEST['amount'][$i]."' WHERE id='".$_REQUEST['ids'][$i]."'),";
}
$s = rtrim($s,",");
if(!mysql_query($s))
echo mysql_error();
else
echo "<script>alert('Records Updated')</script>";
} ?>
now script does not work gives below error:
Erreur de syntaxe pr�s de '( date_from='newvalue', date_to='newvalue', car_no='newvalue' � la ligne 1

The syntax error you received is due to "(" symbol after the SET keyword. It does not allow set multiple statements with more than one SET or WHERE in one query (MySQL Reference).
It is possible to put conditional expressions to every field and concatenate query string for all received ids (some example). But it will be really too much - one additional loop for every field and complex code to format this query (think about writing, debugging, reviewing and supporting it in future).

Related

Onsubmit Event do form validation in PHP

I have a HTML/PHP form that needs validation of each of the entered fields before updating MySQL database. It would be nice if this cold be done as each field is entered...however, I could use the form's action attribute to run a different validation script but then if there are errors I would need to return to the form and re-populate it with previously entered data for correction - not sure how I would do all this but I guess it's possible. However, I've seen a post here that shows how this can be done with a javascript function for the validation but is this possible to do in PHP? I'm just learning PHP so I'd hate to now have to also learn javascript :( Some guidance appreciated.
<?php
if(isset($_POST)) {
echo __LINE__ . " Form has been posted <br>";
echo __LINE__ . $_POST['state_city'];
}
?>
<form action="" method="post">
<table border="0" align="center">
<tr height="29px" width = "220px">
<td>
<label for="student_id"><b>Student-ID</b></label>
</td>
<td>
<input type="text" placeholder="Student ID" name="studentid" value="<?php echo $row['studentid'];?>" required>
</td>
</tr>
<tr height="29px" width = "220px">
<td>
<label for="student_name"><b>Student name</b></label>
</td>
<td>
<input type="text" placeholder="Student name" name="name" value="<?php echo $row['$studentname'];?>" required>
</td>
</tr>
<tr height="29px" width = "220px"><td><label for="start_date"><b>Start Date</b></label></td>
<td><input type="date" placeholder="Enter start date" name="start_date" value="<?php echo $row['startdate'];;?>" required></td>
</tr>
<tr height="29px"><td width = "220px">
<b>Student Grade</b></td>
<td>
<select name="sgrade" id="sgrade">
<option value="1" <?php if ($row['$sgrade'] == '1') echo "selected"; ?>>Grade 1<option>
<option value="2" <?php if ($row['$sgrade'] == '2') echo "selected"; ?>>Grade 2<option>
<option value="3" <?php if ($row['$sgrade'] == '3') echo "selected"; ?>>Grade 3<option>
<option value="4" <?php if ($row['$sgrade'] == '4') echo "selected"; ?>>Grade 4<option>
</select>
</td>
<tr>
// value?
<tr height="29px"><td width = "220px"><b>Country of Birth</b></td>
<td>
<select name="country" id="country">
<?php
$sql = "SELECT * FROM countries";
$result = mysqli_query($con, $sql);
?>
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['id'] . "'>" . $row['name'] . "</option>";
}
?>
</select>
<?php
mysqli_free_result($result);
mysqli_close($con);
?>
</td>
</tr>
<tr height="29px" width = "220px">
<td><label for="state_city"><b>State and/or main city</b></label></td>
<td><input type="text" placeholder="Student State and/or Main City" name="state_city" value="<?php echo $row['$state_city'];?>" required></td>
</tr>
<tr height="29px" width = "220px">
<td><label for="healthcheck"><b>Checked?</b></label></td>
<td>
<input type="radio" id="checkyes" name="check" value="1" <?php if ($row['check']) echo "checked";?>>
<label for="checkyes"> Yes</label><br>
<input type="radio" id="checkno" name="check" value="0" <?php if (!$row['check']) echo "checked";?>>
<label for="checkno"> No</label><br><br>
</td>
</tr>
</table>
<button type="submit">Save</button>
<div class="container" style="background-color:#f1f1f1">
<button type="button" onclick="history.back()" value="Go back!" class="cancelbtn">Cancel</button>
</div>
</form>
<script>
function check() {
document.getElementById("checkyes").checked = true;
}
function uncheck() {
document.getElementById(hcheckyes").checked = false;
}
</script>
</body>
</html>
If you want it to be dynamic as the user enters the information you will have to use JavaScript as that would be a front-end solution. With PHP you can do you validation/sanitizing of the data on the server-side as the form is submitted. It is suggested you do both.
For your question of repopulating this is one way
<input type="text" name="name" value="<?php echo htmlspecialchars($_POST['name'] ?? '', ENT_QUOTES); ?>">
For more info : click here

insert data into database from array

i need to add data to database from array(table). but this POST.php is not working properly. so can you guys please give me any solution for this.
this is the error that it give.
Illegal string offset 'h_name' in C:\wamp\www\confirm\post.php on line 11
line 11 is: VALUES ('$row[h_name]', '$row[room]', '$row[nors]', '$row[nights]', '$row[euro]', '$row[date]', '09090')";
all inserted value give that error. Array to string conversion
Thank you.
</br>
<h4 id="italic">Hotel Details :</h4>
</br>
<div class="reqtable">
<table>
<tr ><td>Hotel Name</td><td>Room Type</td><td>Number of Rooms</td><td>Nights</td><td>EURO</td><td>Date</td></tr>
<tr><td><?php
include "conn.php";
$query = "SELECT h_id, h_name FROM hotels";
$result = mysqli_query($conn, $query) or die(mysqli_error($conn)); // Run your query
echo '<select name="list[h_name]" id="h_name" ">';
echo '<option value=""> Choose a Hotel </option>';
while($row = mysqli_fetch_assoc($result)) {
echo '<option value="'.$row['h_id'].'">'.$row['h_name'].'</option>';
}
echo '</select>';/
?>
</td>
<td>
<?php echo '
<select name="list[room]" id="room" >
<option value="">Choose a Room Type</option>
<option value="1">Single Room</option>
<option value="2">Double Room</option>
<option value="3">Triple Room</option>
<option value="4">Family Room</option>
<option value="5">Custom Room</option>
</select>';
?>
</td>
<td><input type="text" name="list[nors]" placeholder="Number of Rooms"></td>
<td> <input type="text" class="zxc" nname="list[nights]" placeholder="Nights"></td>
<td><input type="text" name="list[euro]" placeholder="euro"></td>
<td><input type="date" name="list[date]" placeholder="Date"></td>
<td><pre> </pre></td>
<td><input type='button' class='AddNew' value='Add new item'></td></tr>
<tr><td>Total</td><td></td><td></td><td><input name="result" id="result"></td><td></td><td></td></tr>
</table>
</div>
</br>
<input type="submit" id="submit" name="submit" value="Register" color="red" style="width: 77px; height: 50px"></div>
</form>
post.php(php process)
<?php
include "conn.php";
print_r($_POST['list']);
foreach ($_POST as $row) {
$query = "INSERT INTO reqhotels (reqh_h_name, reqh_rtype, reqh_nor, reqh_nights, reqh_euro, reqh_date, reqh_req_no)
VALUES ('$row[h_name]', '$row[room]', '$row[nors]', '$row[nights]', '$row[euro]', '$row[date]', '09090')";
$result = mysqli_query($conn, $query) or die(mysqli_error($conn));
if($result > 0) {
echo"successfull";
}
else {
echo"fail";
}
}
?>
EDIT : ported from answer to question : Aug.05
this is my html codes. (html table). in here used a jquery to add some extra rows to this table. so i want to insert all data to database.
table columns:
rq_h_name, rq_rtype, rq_nors, rq_nights, rq_euro, rq_date
<form action="post.php" method="POST" id="register-form" novalidate="novalidate">
</br>
<h4 id="italic">Hotel Details :</h4>
</br>
<div class="reqtable">
<table>
<tr >
<td>Hotel Name</td>
<td>Room Type</td>
<td>Number of Rooms</td>
<td>Nights</td>
<td>EURO</td>
<td>Date</td>
</tr>
<tr>
<td>
<?php
include "conn.php";
$query = "SELECT h_id, h_name FROM hotels";
$result = mysqli_query($conn, $query) or die(mysqli_error($conn)); // Run your query
echo '<select name="list[0][]" id="h_name" ">';
echo '<option value=""> Choose a Hotel </option>';
while($row = mysqli_fetch_assoc($result)) {
echo '<option value="'.$row['h_id'].'">'.$row['h_name'].'</option>';
}
echo '</select>';
?>
</td>
<td>
<?php echo '
<select name="list[0][]" id="room" >
<option value="">Choose a Room Type</option>
<option value="1">Single Room</option>
<option value="2">Double Room</option>
<option value="3">Triple Room</option>
<option value="4">Family Room</option>
<option value="5">Custom Room</option>
</select>';
?>
</td>
<td><input type="text" name="list[0][]" placeholder="Number of Rooms"></td>
<td> <input type="text" class="zxc" name="list[0][]" placeholder="Nights"></td>
<td><input type="text" name="list[0][]" placeholder="euro"></td>
<td><input type="date" name="list[0][]" placeholder="Date"></td>
<td><input type='button' class='AddNew' value='Add new item'></td></tr>
<tr>
<td>Total</td>
<td><input name="result" id="result"></td>
</tr>
</table>
</div>
</br>
probably you want to do the following
foreach ($_POST['list'] as $row) {
or similar instead of current
foreach ($_POST as $row) {
In your current manner you have an array in array so and the array could not be substituted directly to a string. I'm not sure this will exactly work, but that's the idea you shoud follow and shows the problem you have. Your code is a bit messy so I'won't guarantee you have no other errors.

One input not posting PHP/MSSQL

Trying to use this page to update an MSSQL table. My hidden field named hdnLine (2nd to last line of code) is not being posted on submit, though it is being populated with a value prior to the form being submitted. I had the page working, and all of the sudden I keep getting "Notice: Undefined index: hdnLine in C:\inetpub\wwwroot\clientadmin\agent-edit.php on line 11" when I try to submit. I have been messing with it all day trying to debug etc. and have had no luck with it. I'm assuming its a syntax error of sorts but can't figure it out. Any help would be gladly appreciated. The code is all below minus a few includes for db connectivity. I will do my best to help answer any questions pertaining.
<?php
if($_GET["Action"]=="Save"){
for($i=1;$i<=$_POST["hdnLine"];$i++)
{
$strSQL = "UPDATE RENTAGNT SET ";
$strSQL .="SPIFF = '".$_POST["txtSPIFF$i"]."' ";
$strSQL .=",PROMO = '".$_POST["txtPROMO$i"]."' ";
$strSQL .=",SPIFFVAL = '".$_POST["txtSPIFFVAL$i"]."' ";
$strSQL .=",AGNTLVL = '".$_POST["txtAGNTLVL$i"]."' ";
$strSQL .=",SPIFFDAYS = '".$_POST["txtSPIFFDAYS$i"]."' ";
$strSQL .=",MASTAGNTID = '".$_POST["txtMASTAGNTID$i"]."' ";
$strSQL .="WHERE RENTAGNTID = '".$_POST["hdnRENTAGNTID$i"]."' ";
$stmt = $conn->query($strSQL);
}
//exit();
}
$strSQL="SELECT RENTAGNTID, RENTAGNT.AGENTCODE, NAME, AGNTLVL,SPIFF, SPIFFVAL, PROMO, SPIFFDAYS,INACTIVE, MASTAGNTID FROM RENTAGNT WHERE INACTIVE='0' GROUP BY MASTAGNTID, RENTAGNT.AGENTCODE, RENTAGNT.NAME, RENTAGNTID, SPIFF, SPIFFVAL, SPIFFDAYS, AGNTLVL, INACTIVE, PROMO";
$stmt = $conn->query($strSQL);
?>
<form name="frmMain" method="POST" action="?Action=Save" >
<table id="res-list-table" class="tablesorter" width="90%" border="1" background="#fff">
<thead> <tr>
<th width="10%"> <div align="center">Rental Agent ID</div></th>
<th width="20%"> <div align="center">Agent Code </div></th>
<th width="35%"> <div align="center">Name </div></th>
<th width="5%"> <div align="center">1 Free</div></th>
<th width="5%"> <div align="center">Spiff Yes/No</div></th>
<th width="5%"> <div align="center">Spiff Amount </div></th>
<th width="5%"> <div align="center">Days Req. <br/> for Spiff</div></th>
<th width="5%"> <div align="center">Master Agent</div></th>
<th width="5%"> <div align="center">Agent Level</div></th>
</tr>
</thead><tbody>
<?php
$i = 1;
while ($row = $stmt->fetch( PDO::FETCH_ASSOC ) ){
$i++;
?>
<tr>
<td><div align="center">
<?php echo $row["RENTAGNTID"]; ?>
</div></td>
<td><div align="center">
<?php echo $row["AGENTCODE"]; ?>
</div></td>
<td><div align="center">
<?php echo $row["NAME"]; ?>
</div></td>
<td>
<input type="hidden" name="hdnRENTAGNTID<?=$i;?>" size="6" value="<?=$row["RENTAGNTID"];?>"/>
<select name="txtPROMO<?=$i;?>">
<option value="0" <?php if($row["PROMO"]=="0") { echo "selected='selected'"; } ?> >No</option>
<option value="1" <?php if($row["PROMO"]=="1") { echo "selected='selected'"; } ?> >Yes</option>
</select></td>
<td>
<select name="txtSPIFF<?=$i;?>">
<option value="0" <?php if($row["SPIFF"]=="0") { echo "selected='selected'"; } ?> >No</option>
<option value="1" <?php if($row["SPIFF"]=="1") { echo "selected='selected'"; }?> >Yes</option>
</select></td>
<td><input type="text" name="txtSPIFFVAL<?=$i;?>" size="5" style="width:75px;" value="<?=$row["SPIFFVAL"];?>" /></td>
<td><input type="text" name="txtSPIFFDAYS<?=$i;?>" size="5" style="width:75px;" value="<?=$row["SPIFFDAYS"];?>" /></td>
<td><input type="text" name="txtMASTAGNTID<?=$i;?>" size="5" style="width:75px;" value="<?=$row["MASTAGNTID"];?>" /></td>
<td>
<select name="txtAGNTLVL<?=$i;?>">
<option value="1" <?php if($row["AGNTLVL"]==1) { echo "selected='selected'"; }?> >Agent</option>
<option value="2" <?php if($row["AGNTLVL"]==2) { echo "selected='selected'"; }?> >Master Agent</option>
<option value="3" <?php if($row["AGNTLVL"]==3) { echo "selected='selected'"; }?> >Admin</option>
</select>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<input type="submit" name="submit" value="submit1" />
<input type="hidden" name="hdnLine" value="<?=$i;?>"/>
</form>
Seems like the short tags used to assign the value for the <input type="hidden" name="hdnLine" value="<?=$i;?>"/>. Is the short_open_tag = On in your php.ini?
If not change to on.
Or
Change throughout the code where <?=?> to <?php=?>

Select time from database after choose a ticket number

I need to make a edit / update function based from searching ticket number. When user type a ticket number he had, would appear a form that contains data from database based on ticket number that he had.I can see name value, ticket number value and date value from database but I can't see a clock time value in select tag.
<?php
$no = $_GET['ticket'];
$st = "SELECT * FROM event WHERE no='$no'";
$check = mysql_query($st,$connection) or die("Failed");
$c = mysql_fetch_array($check);
?>
<form name="form" method="POST" action="">
<table>
<tr>
<td>Reference Number</td>
<td> : </td>
<td>
<input type="text" name="no" value="<?php echo $c['no'] ;?>" disabled>
<input type="hidden" name="no" value="<?php echo $c['no'] ;?>">
</td>
</tr>
<tr>
<td>Name</td>
<td> : </td>
<td>
<input type="text" name="name" value="<?php echo $c['name'] ;?>" disabled>
<input type="text" name="name" value="<?php echo $c['name'] ;?>" disabled>
</td>
</tr>
<tr>
<td>Date</td>
<td> : </td>
<td>
<input type="text" id="date" name="date" value="<?php echo $c['date'] ;?>">
</td>
</tr>
<tr>
<td>Clock Time</td>
<td> : </td>
<td>
<select name="time" value="<?php echo $c['time'] ;?>">
</td>
</tr>
I want to see the clock time value on database in select tag. After that, if user want to make changes on clock time based on the date he chosen, he can do that thing.
Thanks before,
The stucture of drop down in htm is
<select name ="postname">
<option value="postvalue"> display value</option>
more options
.
.
.
.
</select>
use following code in your case
<select name="postname" >
<option value="<?php echo $c['date'] ;?>" > <?php echo $c['date'] ;?> </option>
</select>
select tags must have options. you need to popuate the select tag with options and then check it against your value to set the selected option.
this method is wrong <select name="time" value="<?php echo $c['date'] ;?>">
<select name="time">
<?php foreach($options as $option) { ?>
<?php $sel = ''; if ($option->value = $c['date']) { $sel = 'selected'; } ?>
<option value="<?php echo $option->value; ?>" <?php echo $sel; ?>><?php echo $option->name; ?></option>
<?php } ?>
</select>
put your select like this:-
<select name="time" >
<option value=value="<?php echo $c['date'] ;?>" selected > <?php echo $c['date'] ;?> </option>
</select>

How do i retain form data when doing a page lookup

I'm trying to do a table lookup. I fill out the fields then click the combobox to do a lookup. When I return to the page, the Last Name Field goes blank. I need a way of keeping the field Data when I return to the page.
<?php /*?>
<?php if(isset($_POST['First'])) { echo htmlentities ($_POST['First']); }?>
<?php */?>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Last Name:</td>
<td><input type="text" name="Last_Name" id="Last_Name" autofocus value="" placeholder="last name" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">City:</td>
<td>
<select name="LookupCity" id="LookupCity">
<option value=""
<?php if (!(strcmp("", $_GET['City']))) {echo "selected=\"selected\"";} ?>>
Select from menu
</option>
<?php do { ?>
<option value="<?php echo $row_fmCity['City']?>"<?php if (!(strcmp($row_fmCity['City'], $_GET['City']))) {echo "selected=\"selected\"";} ?>><?php echo $row_fmCity['City']?>
</option>
<?php
} while ($row_fmCity = mysql_fetch_assoc($fmCity));
$rows = mysql_num_rows($fmCity);
if($rows > 0) {
mysql_data_seek($fmCity, 0);
$row_fmCity = mysql_fetch_assoc($fmCity);
}
?>
</select>
select
</td>
Please give an example.
Just set the "value" attribute of the controls.

Categories