I have a table with these columns (database)
like this http://netelity.com/table.JPG.
and i have a static form through which user define the installments. Static 24 input boxes are there like this
<form name="installment" method="post" action="" enctype="multipart/form-data" onsubmit="return validate()">
<table id="dt_hScroll" class="table table-striped">
<thead>
<tr>
<th>SL No.</th>
<th>Amount</th>
<th>Due Date</th>
</tr>
<tr>
<td> 1. </td>
<td>
<input type="text" name="installment1" id="installment" class="span4" value="0" />
</td>
<td>
<input type="text" name="due_date1" class="tcal span4" value="<?php //echo date("Y-m-d"); ?>" />
</td>
</tr>
<tr>
<td> 2. </td>
<td>
<input type="text" name="installment2" id="installment" class="span4" value="0" />
</td>
<td>
<input type="text" name="due_date2" class="tcal span4" />
</td>
</tr>
<tr>
<td> 3. </td>
<td>
<input type="text" name="installment3" id="installment" class="span4" value="0" />
</td>
<td>
<input type="text" name="due_date3" class="tcal span4" />
</td>
</tr>
<tr>
<td> 4. </td>
<td>
<input type="text" name="installment4" id="installment" class="span4" value="0" />
</td>
<td>
<input type="text" name="due_date4" class="tcal span4" />
</td>
</tr>
<tr>
<td> 5. </td>
<td>
<input type="text" name="installment5" id="installment" class="span4" value="0" />
</td>
<td>
<input type="text" name="due_date5" class="tcal span4" />
</td>
</tr>
<tr>
<td> 6. </td>
<td>
<input type="text" name="installment6" id="installment" class="span4" value="0" />
</td>
<td>
<input type="text" name="due_date6" class="tcal span4" />
</td>
</tr>
<tr>
<td> 7. </td>
<td>
<input type="text" name="installment7" id="installment" class="span4" value="0" />
</td>
<td>
<input type="text" name="due_date7" class="tcal span4" />
</td>
</tr>
<tr>
<td> 8. </td>
<td>
<input type="text" name="installment8" id="installment" class="span4" value="0" />
</td>
<td>
<input type="text" name="due_date8" class="tcal span4" />
</td>
</tr>
<tr>
<td> 9. </td>
<td>
<input type="text" name="installment9" id="installment" class="span4" value="0" />
</td>
<td>
<input type="text" name="due_date9" class="tcal span4" />
</td>
</tr>
<tr>
<td> 10. </td>
<td>
<input type="text" name="installment10" id="installment" class="span4" value="0" />
</td>
<td>
<input type="text" name="due_date10" class="tcal span4" />
</td>
</tr>
<tr>
<td> 11. </td>
<td>
<input type="text" name="installment11" id="installment" class="span4" value="0" />
</td>
<td>
<input type="text" name="due_date11" class="tcal span4" />
</td>
</tr>
<tr>
<td> 12. </td>
<td>
<input type="text" name="installment12" id="installment" class="span4" value="0" />
</td>
<td>
<input type="text" name="due_date12" class="tcal span4" />
</td>
</tr>
<tr>
<td> 13. </td>
<td>
<input type="text" name="installment13" id="installment" class="span4" value="0" />
</td>
<td>
<input type="text" name="due_date13" class="tcal span4" />
</td>
</tr>
<tr>
<td> 14. </td>
<td>
<input type="text" name="installment14" id="installment" class="span4" value="0" />
</td>
<td>
<input type="text" name="due_date14" class="tcal span4" />
</td>
</tr>
<tr>
<td> 15. </td>
<td>
<input type="text" name="installment15" id="installment" class="span4" value="0" />
</td>
<td>
<input type="text" name="due_date15" class="tcal span4" />
</td>
</tr>
<tr>
<td> 16. </td>
<td>
<input type="text" name="installment16" id="installment" class="span4" value="0" />
</td>
<td>
<input type="text" name="due_date16" class="tcal span4" />
</td>
</tr>
<tr>
<td> 17. </td>
<td>
<input type="text" name="installment17" id="installment" class="span4" value="0" />
</td>
<td>
<input type="text" name="due_date10" class="tcal span4" />
</td>
</tr>
<tr>
<td> 18. </td>
<td>
<input type="text" name="installment18" id="installment" class="span4" value="0" />
</td>
<td>
<input type="text" name="due_date18" class="tcal span4" />
</td>
</tr>
<tr>
<td> 19. </td>
<td>
<input type="text" name="installment19" id="installment" class="span4" value="0" />
</td>
<td>
<input type="text" name="due_date19" class="tcal span4" />
</td>
</tr>
<tr>
<td> 20. </td>
<td>
<input type="text" name="installment20" id="installment" class="span4" value="0" />
</td>
<td>
<input type="text" name="due_date20" class="tcal span4" />
</td>
</tr>
<tr>
<td> 21. </td>
<td>
<input type="text" name="installment21" id="installment" class="span4" value="0" />
</td>
<td>
<input type="text" name="due_date21" class="tcal span4" />
</td>
</tr>
<tr>
<td> 22. </td>
<td>
<input type="text" name="installment22" id="installment" class="span4" value="0" />
</td>
<td>
<input type="text" name="due_date22" class="tcal span4" />
</td>
</tr>
<tr>
<td> 23. </td>
<td>
<input type="text" name="installment23" id="installment" class="span4" value="0" />
</td>
<td>
<input type="text" name="due_date23" class="tcal span4" />
</td>
</tr>
<tr>
<td> 24. </td>
<td>
<input type="text" name="installment24" id="installment" class="span4" value="0" />
</td>
<td>
<input type="text" name="due_date24" class="tcal span4" />
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" name="save" id="save" value="Save" class="btn btn-info span3" />
<input type="hidden" readonly="readonly" name="roll_no" value="<?php echo $roll_no; ?>" class="span5" />
</td>
<td></td>
</tr>
</thead>
</table>
</form>
Here i am not getting how to store it in that table. Actually all the 24 inputs are not mandatory. If user fills only 2 then also it should store it in the database table.
I tried to do like this
if(isset($_POST['save']))
{
$roll_no = $_POST['roll_no'];
$ins_amt1 = $_POST['installment1'];
$due_date1 = $_POST['due_date1'];
$ins_amt2 = $_POST['installment2'];
$due_date2 = $_POST['due_date2'];
$ins_amt3 = $_POST['installment3'];
$due_date3 = $_POST['due_date3'];
$ins_amt4 = $_POST['installment4'];
$due_date4 = $_POST['due_date4'];
$ins_amt5 = $_POST['installment5'];
$due_date5 = $_POST['due_date5'];
$ins_amt6 = $_POST['installment6'];
$due_date6 = $_POST['due_date6'];
$ins_amt7 = $_POST['installment7'];
$due_date7 = $_POST['due_date7'];
$ins_amt8 = $_POST['installment8'];
$due_date8 = $_POST['due_date8'];
$ins_amt9 = $_POST['installment9'];
$due_date9 = $_POST['due_date9'];
$ins_amt10 = $_POST['installment10'];
$due_date10 = $_POST['due_date10'];
$ins_amt11 = $_POST['installment11'];
$due_date11 = $_POST['due_date11'];
$ins_amt12 = $_POST['installment12'];
$due_date12 = $_POST['due_date12'];
$ins_amt13 = $_POST['installment13'];
$due_date13 = $_POST['due_date13'];
$ins_amt14 = $_POST['installment14'];
$due_date14 = $_POST['due_date14'];
$ins_amt15 = $_POST['installment15'];
$due_date15 = $_POST['due_date15'];
$ins_amt16 = $_POST['installment16'];
$due_date16 = $_POST['due_date16'];
$ins_amt17 = $_POST['installment17'];
$due_date17 = $_POST['due_date17'];
$ins_amt18 = $_POST['installment18'];
$due_date18 = $_POST['due_date18'];
$ins_amt19 = $_POST['installment19'];
$due_date19 = $_POST['due_date19'];
$ins_amt20 = $_POST['installment20'];
$due_date20 = $_POST['due_date20'];
$ins_amt21 = $_POST['installment21'];
$due_date21 = $_POST['due_date21'];
$ins_amt22 = $_POST['installment22'];
$due_date22 = $_POST['due_date22'];
$ins_amt23 = $_POST['installment23'];
$due_date23 = $_POST['due_date23'];
$ins_amt24 = $_POST['installment24'];
$due_date24 = $_POST['due_date24'];
$items = array();
$installment[] =array($ins_amt1, $ins_amt2, $ins_amt3, $ins_amt4, $ins_amt5, $ins_amt6, $ins_amt7, $ins_amt8, $ins_amt9, $ins_amt10, $ins_amt11, $ins_amt12, $ins_amt13, $ins_amt14, $ins_amt15, $ins_amt16, $ins_amt17, $ins_amt18, $ins_amt19, $ins_amt20, $ins_amt21, $ins_amt22, $ins_amt23, $ins_amt24);
//$in_values= serialize($installment);
$due_date[] = array($due_date1, $due_date2, $due_date3, $due_date4, $due_date5, $due_date6, $due_date7, $due_date8, $due_date9, $due_date10, $due_date11, $due_date12, $due_date13, $due_date14, $due_date15, $due_date16, $due_date17, $due_date18, $due_date19, $due_date20, $due_date21, $due_date22, $due_date23, $due_date24);
//$in_dates= serialize($due_date);
//$s1 = "insert into installment(id, fee_id, student_id, amount, due_date, paid_date, status, rec_no) values ('', ".$fee_id.", '".$roll_no."', ".$in_values.", '".$in_dates."', '', 'unpaid', ''";
foreach($installment as $row_key => $value)
{
$item = $value;
$uom = $due_date[$row_key];
$items[] = sprintf("(%d, %d, '%s', %d, '%s', '%s', '%s', '%s')", '',
$fee_id,
mysql_real_escape_string($roll_no),
intval($item),
$uom,
'',
'',
''
);
}
$msql = 'INSERT INTO installment (id, fee_id, student_id, amount, due_date, paid_date, status, rec_no) VALUES '.implode(', ', $items);
But it is taking only one data. Can somebody please suggest.
The data has to be saved like this:
Like This http://netelity.com/saveddata.JPG
A normal INSERT statement should look like:
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
So try changing
$msql = 'INSERT INTO installment (id, fee_id, student_id, amount, due_date, paid_date, status, rec_no) VALUES '.implode(', ', $items);
To:
$values = implode(', ', $items);
$msql = 'INSERT INTO installment (id, fee_id, student_id, amount, due_date, paid_date, status, rec_no) VALUES ('.$values.');
And don't forget to actually run the sql command. I don't see that in your script anywhere :)
Related
I have a html repeated values in array that i need to update to database, i need a help on how to use php script to update to database where staffid mataches . See below my HTML code
<!DOCTYPE html>
<html>
<head lang="en">
<meta chartaxt="UTF-8">
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
<script src="js/jquery.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
<title>Sample salary computation</title>
</head>
<body>
<form method="POST" name="regis" id="regis">
<table align="center" border="1" width="200">
<tr>
<th>Staff ID</th>
<th>Salary</th>
<th>Total Tax</th>
<th>Total Net Pay</th>
</tr>
<!--Staff 1-->
<tr data-id="STAFF/2016/001">
<td>
<input type="text" name="staffid[]" class="staffid" value="STAFF/2016/001" readonly="readonly">
</td>
<td>
<input type="text" name="salary[]" class="salary" placeholder="salary" value="5000">
</td>
<td>
<input type="text" name="tax[]" class="tax" placeholder="tax" value="500">
</td>
<td>
<input type="text" name="total[]" class="total" placeholder="total" readonly="readonly" value="4500">
</td>
</tr>
<!--Staff 2-->
<tr data-id="STAFF/2016/002">
<td>
<input type="text" name="staffid[]" class="staffid" value="STAFF/2016/002" readonly="readonly">
</td>
<td>
<input name="salary[]" class="salary" type="text" placeholder="salary" value="10000">
</td>
<td>
<input type="text" placeholder="tax" name="tax[]" class="tax" value="1000">
</td>
<td>
<input type="text" placeholder="total" class="total" name="total[]" readonly="readonly" value="9000">
</td>
</tr>
<!--Staff 3-->
<tr data-id="STAFF/2016/003">
<td>
<input type="text" name="staffid[]" class="staffid" value="STAFF/2016/003" readonly="readonly">
</td>
<td>
<input name="salary[]" class="salary" type="text" placeholder="salary" value="8400">
</td>
<td>
<input type="text" placeholder="tax" name="tax[]" class="tax" value="400">
</td>
<td>
<input type="text" placeholder="total" class="total" name="total[]" readonly="readonly" value="800">
</td>
</tr>
<tr>
<td width="300">
<input type="button" name="update" value="Update Values" class="btn btn-danger">
</td>
</tr>
</table>
</form>
</body>
</html>
Kindly assist. I want the SQL update query to look like
"Update payroll SET salary ='".$_POST['salary']."', tax='".$_POST['tax']."', total ='".$_POST['total']."' WHERE staffid= '".$_POST['staffid']."'"
I would recommend to rename the input names to create a single array with all the information you need. Use the staff ID as array keys.
<!--Staff 1-->
<tr data-id="STAFF/2016/001">
<td>
<input type="text" name="staff[STAFF/2016/001][id]" class="staffid" value="STAFF/2016/001" readonly="readonly">
</td>
<td>
<input type="text" name="staff[STAFF/2016/001][salary]" class="salary" placeholder="salary" value="5000">
</td>
<td>
<input type="text" name="staff[STAFF/2016/001][tax]" class="tax" placeholder="tax" value="500">
</td>
<td>
<input type="text" name="staff[STAFF/2016/001][total]" class="total" placeholder="total" readonly="readonly" value="4500">
</td>
</tr>
<!--Staff 2-->
<tr data-id="STAFF/2016/002">
<td>
<input type="text" name="staff[STAFF/2016/002][id]" class="staffid" value="STAFF/2016/002" readonly="readonly">
</td>
<td>
<input type="text" name="staff[STAFF/2016/002][salary]" class="salary" placeholder="salary" value="10000">
</td>
<td>
<input type="text" name="staff[STAFF/2016/002][tax]" placeholder="tax" class="tax" value="1000">
</td>
<td>
<input type="text" name="staff[STAFF/2016/002][total]" placeholder="total" class="total" readonly="readonly" value="9000">
</td>
</tr>
In PHP you can then iterate over this single array:
<?php
$db = new PDO('mysql:host=localhost;dbname=database;', 'root', '');
if (!empty($_POST['staff'])) {
$stmt = $db->prepare('UPDATE payroll SET salary = :salary, tax = :tax, total = :total WHERE staffid = :staffid');
foreach ((array)$_POST['staff'] as $staffId => $staffInfo) {
$stmt->bindValue(':salary', $staffInfo['salary']);
$stmt->bindValue(':tax', $staffInfo['tax']);
$stmt->bindValue(':total', $staffInfo['total']);
$stmt->bindValue(':staffid', $staffId); // you can also use $staffInfo['id'] here instead of $staffId
$stmt->execute();
}
}
See also:
http://php.net/manual/en/pdo.prepared-statements.php
http://php.net/manual/de/pdo.prepare.php
I want to received 4,5 row data in php. When I put values in the form like 3 or 4 row than I want to received the same data in php :(
I am trying to make a pos. When the customer order multiple product than the order row automatically increase but there is no limit 5, 10, 15 or else.
And I want to received the data value in same rows like
<form name="data" method="post" action="data_rec.php" enctype="multipart/form-data">
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
</tr>
<tr>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
</tr>
<tr>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
</tr>
<tr>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
</tr>
<tr>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
</tr>
</table>
<input name="Submit" type="submit" value="SEND">
</form>
<?php
foreach (array_combine($_POST['data'], $_POST['data']) as $i => $data) {
$i."<br />";
echo $data."<br />";
echo "SAGOR"."<br />";
}
?>
Output :
1
SAGOR
2
SAGOR
3
SAGOR
4
SAGOR
5
SAGOR
6
SAGOR
7
SAGOR
8
SAGOR
SAGOR
But I need :
1 2 3 4
SAGOR
5 6 7 8
You can add rows to your form on the browser with JS or JQuery.
You must give each row's elements unique ids/names like: <input name="data2" type="text" id="data2" /> <input name="data3" type="text" id="data3" />...
You will receive them all in you $_POST when the user submit the form
I had tried to insert bulk data with same name field contains multiple rows. But only single row is inserted.
How to insert bulk data as different values to insert into the database.
INSERT INTO table_name (username, luck_number, test, tester) VALUES (('$username', '$luck_number', '$test', '$tester').
<tr>
<td>1</td>
<input type="hidden" name="username" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number" value="" />
</td>
<td>
<input type="text" name="big" value="" />
</td>
<td>
<input type="text" name="test" value="" />
</td>
<td>
<input type="text" name="tester" value="" />
</td>
</tr>
<tr>
<td>2</td>
<input type="hidden" name="username" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number" value="" />
</td>
<td>
<input type="text" name="big" value="" />
</td>
<td>
<input type="text" name="test" value="" />
</td>
<td>
<input type="text" name="tester" value="" />
</td>
</tr>
<tr>
<td>3</td>
<input type="hidden" name="username" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number" value="" />
</td>
<td>
<input type="text" name="big" value="" />
</td>
<td>
<input type="text" name="test" value="" />
</td>
<td>
<input type="text" name="tester" value="" />
</td>
</tr>
#nisha,In your scenario only single row is inserted because variables are same name so it's overridden, Please try below code, It will give you array of fields so you can easily create for-loop & do multiple insert with your query.
<form method="post" name="userdata">
<tr>
<td>1</td>
<input type="hidden" name="username[]" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number[]" value="" />
</td>
<td>
<input type="text" name="big[]" value="" />
</td>
<td>
<input type="text" name="test[]" value="" />
</td>
<td>
<input type="text" name="tester[]" value="" />
</td>
</tr>
<tr>
<td>2</td>
<input type="hidden" name="username[]" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number[]" value="" />
</td>
<td>
<input type="text" name="big[]" value="" />
</td>
<td>
<input type="text" name="test[]" value="" />
</td>
<td>
<input type="text" name="tester[]" value="" />
</td>
</tr>
<tr>
<td>3</td>
<input type="hidden" name="username[]" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number[]" value="" />
</td>
<td>
<input type="text" name="big[]" value="" />
</td>
<td>
<input type="text" name="test[]" value="" />
</td>
<td>
<input type="text" name="tester[]" value="" />
</td>
</tr>
<input type="submit" name="submit">
</form>
**Note:**you can worry about the security issue letter. read the answer with the comment.
Store them in array by adding [ ] this in your input field
<tr>
<td>1</td>
<form action="" method="POST">
<input type="hidden" name="username" value="<?php echo $login_session; ?>"/>
<td><input type="text" name="luck_number[]" value=""/></td>
<td><input type="text" name="big[]" value=""/></td>
<td><input type="text" name="test[]" value=""/></td>
<td><input type="text" name="tester[]" value=""/></td>
</tr>
<tr>
<td>2</td>
<input type="hidden" name="username" value="<?php echo $login_session; ?>"/>
<td><input type="text" name="luck_number[]" value=""/></td>
<td><input type="text" name="big[]" value=""/></td>
<td><input type="text" name="test[]" value=""/></td>
<td><input type="text" name="tester[]" value=""/></td>
</tr>
<tr>
<td>3</td>
<input type="hidden" name="username" value="<?php echo $login_session; ?>"/>
<td><input type="text" name="luck_number[]" value=""/></td>
<td><input type="text" name="big[]" value=""/></td>
<td><input type="text" name="test[]" value=""/></td>
<td><input type="text" name="tester[]" value=""/></td>
</tr>
<tr></td><input type="submit" name="submit" value="submit"/><tr></td>
</form>
<?php
//connect with your database
for($i=0;$i<count($_POST['luck_number']);$i++)
{
//set the value for variable
$luck_number=$_POST['luck_number'][$i];
$test=$_POST['test'][$i];
$tester=$_POST['tester'][$i];
//run your query
//INSERT INTO table_name (username, luck_number, test, tester) VALUES (('$username', '$luck_number', '$test', '$tester').
}
First of all change all field names by adding [] at the end.
Second step, to parsing all values you may use something like this
for($i=0; $i < $count($_GET['username']); $i++)
{
$username = $_GET['username'][$i];
$luck_number= $_GET['luck_number'][$i];
$big= $_GET['big'][$i];
$test= $_GET['test'][$i];
$tester= $_GET['tester'][$i];
// insert into database
}
The reason of inserting single row instead of multiple rows is your input field name. You are using same name in different input field so when the server gets the reply it replace the duplicate name and the last occurrence is outputted.
The thing you have to do is to use array. If you use known number of rows then you can simply use a for loop to insert data.
<tr>
<td>1</td>
<input type="hidden" name="username[]" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number[]" value="" />
</td>
<td>
<input type="text" name="big[]" value="" />
</td>
<td>
<input type="text" name="test[]" value="" />
</td>
<td>
<input type="text" name="tester[]" value="" />
</td>
</tr>
<tr>
<td>2</td>
<input type="hidden" name="username[]" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number[]" value="" />
</td>
<td>
<input type="text" name="big[]" value="" />
</td>
<td>
<input type="text" name="test[]" value="" />
</td>
<td>
<input type="text" name="tester[]" value="" />
</td>
</tr>
<tr>
<td>3</td>
<input type="hidden" name="username[]" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number[]" value="" />
</td>
<td>
<input type="text" name="big[]" value="" />
</td>
<td>
<input type="text" name="test[]" value="" />
</td>
<td>
<input type="text" name="tester[]" value="" />
</td>
</tr>
<?php
for ($i=0; $i<count($_POST['username']); $i++)
{
mysql_query("INSERT INTO table_name (`username`, `luck_number`, `test`, `tester`) VALUES (('".$_POST['username'][$i]."', '".$_POST['luck_number'][$i]."', '".$_POST['test'][$i]."', '".$_POST['tester'][$i]."')");
}
?>
Note: Sanitizing variable is always been a good practice and strongly recommended.
<form action="execute_new.php" id="modalform" method="post">
<td width="147"> <strong>
<font color="#033">Sticker No:</font><font color="#FF0000">*</font></td>
<td width="146"><input type="text" name="stickerno" /></td>
<tr>
<td><strong>
<font color="#033">TO : </font></td>
</tr>
<tr>
<td >
<input name="fname" type="text" />
</td>
<td >
<input name="mname" type="text" />
</td>
<td width="143" align="right">
<input name="lname" type="text" />
</td>
</tr>
<tr>
<td>
First Name</font><font color="#FF0000">*</font>
</td>
<td >
Middle Name</font><font color="#FF0000">*</font>
</td>
<td>
Last Name</font><font color="#FF0000">*</font>
</td>
</tr>
</table><br>
<br>
<table>
<tr>
<td><strong>
<font color="#033"> ADDRESS:</font></font><font color="#FF0000">*</font> </td>
<td align="center"><input name="address" type="text" value="" size="70"></td>
</tr>
</table>
<p> </p>
<font color="#033">
<table border="15" bgcolor="#00FF66" bordercolor="#000000" width="650" cellpadding="1" cellspacing="1">
<br>
<br>
<tr>
<td >LICENSE NO:<font color="#FF0000">*</font></td>
<td align="center"><input type="text" name="license"/></td>
<td >BIRTH DATE:</td>
<td align="center"><input name="bdate" id="demo1" type="text"><img src="cal.gif" width="16" height="16" border="0"></td>
<tr>
<td >PLATE NO:<font color="#FF0000">*</font></td>
<td align="center"><input type="text" name="plateno"/></td>
<td >PUV BODY NAME:</td>
<td align="center"><input type="text" name="puvbname" /></td>
</tr>
<tr>
<td >TYPE OF VEHICLE:</td>
<td align="center"><select id="vehicletype" name="vehicletype" style="width:134px">
<option value="Private">Private</option>
<option value="Public">Public</option>
</select>
<td >PUV BODY NO:</td>
<td align="center"><input type="text" name="puvbno" /></td>
</tr>
<tr>
<td >OWNER OF VEHICLE:</td>
<td align="center"><input type="text" name="vehicleowner" /></td>
<td >ADDRESS:</td>
<td align="center"><input type="text" name="taddress" /></td>
</tr>
<tr>
<td >PLACE OF VIOLATION:<font color="#FF0000">*</font></td>
<td align="center"><input type="text" name="violationplace" /></td>
<td>DATE / TIME:<font color="#FF0000">*</font></td>
<td align="center"><input name="datetime" id="demo2" type="text"><img src="cal.gif" width="16" height="16" border="0">
</tr>
</table><br>
<table border="15" bordercolor="#000000" width="150" cellpadding="1" cellspacing="1">
<tr>Confiscated:</tr>
<br>
<tr><td><input type="radio" name="RadioGroup1" value="License ID" id="RadioGroup1_0" checked>License ID</tr><br>
<tr><td><input type="radio" name="RadioGroup1" value="License O.R." id="RadioGroup1_1">License O.R.</tr>
<tr>
<td> <input type="radio" name="RadioGroup1" value="TCP/TOP" id="RadioGroup1_2">
TCP/TOP</tr>
<tr><td><input type="radio" name="RadioGroup1" value="License Plate" id="RadioGroup1_3">
License Plate</tr>
<tr><td><input type="radio" name="RadioGroup1" value="Not Confiscated" id="RadioGroup1_4">
Not Confiscated</tr>
</tr></table><br />
<center><strong>You are hereby cited for committing the traffic violations / checked hereunder</strong><br /></center>
<br>
<br> <table width="100%px" border="7" cellspacing="1" cellpadding="1" bordercolordark="#000000" style="font-size:13px">
<tr>
<td><input type="checkbox" name="cbox[]" value="Unattended/Illegaly Parked Vehicle" />
Unattended / Illegaly Parked Vehicle</td>
<td><input type="checkbox" name="cbox[]" value="Out of Line Operation/Route Violation" />
Out of Line Operation/Route Violation</br></td>
<tr>
<td><input type="checkbox" name="cbox[]" value="Illegal Parking" />
Illegal Parking</td>
<td><input type="checkbox" name="cbox[]" value="Trip Cutting" />
Trip Cutting</br></td>
<tr>
<td><input type="checkbox" name="cbox[]" value="Illegal Topload" />
Illegal Topload</td>
<td>
<input type="checkbox" name="cbox[]" value="Over Charging" />
Over Charging</td></tr>
<tr>
<td><input type="checkbox" name="cbox[]" value="Truck Ban/Tricycle Ban" />
Truck Ban/Tricycle Ban</td>
<td><input type="checkbox" name="cbox[]" value="Refusal to Convey Passenger"/>
Refusal to Convey Passenger</td>
</tr>
<tr>
<td><input type="checkbox" name="cbox[]" value="Driving in Slippers/Sleeveless shirt" />
Driving in Slippers/Sleeveless shirt</td>
<td>
<input type="checkbox" name="cbox[]" value="Overspeeding/Drag Racing/Reckless Driving" />Overspeeding/Drag Racing/Reckless Driving</td>
</tr>
<tr>
<td><input type="checkbox" name="cbox[]" value="Driving w/o License/Expired/Invalid License" />
Driving w/o License/Expired/Invalid License</td>
<td><input type="checkbox" name="cbox[]" value="One-way Street Violation" />One-way Street Violation</td>
</tr>
<tr>
<td><input type="checkbox" name="cbox[]" value="No PUV ID" />
No PUV ID</td>
<td><input type="checkbox" name="cbox[]" value="Illegal Loading/Unloading" />Illegal Loading/Unloading</td>
</tr>
<tr>
<td><input type="checkbox" name="cbox[]" value="Driving Dilapidated">
Driving Dilapidated</td>
<td><input type="checkbox" name="cbox[]" value="Traffic Obstruction"/>Traffic Obstruction</td>
</tr>
<tr>
<td><input type="checkbox" name="cbox[]" value="Junk Vehicle" />
Junk Vehicle</td>
<td><input type="checkbox" name="cbox[]" value="Disregarding Traffic Sign/Light" />Disregarding Traffic Sign/Light</td>
</tr>
<tr>
<td><input type="checkbox" name="cbox[]" value="No Smoking 20% Discount Sticker" />
No "No Smoking"/20% Discount Sticker</td>
<td><input type="checkbox" name="cbox[]" value="Illegal Blowing of Horn" />Illegal Blowing of Horn</td>
</tr>
<tr>
<td><input type="checkbox" name="cbox[]" value="No Permit Decalsticker" />
No Permit Decalsticker</td>
<td><input type="checkbox" name="cbox[]" value="Failure to obey Police Order" />Failure to obey Police Order</td>
</tr>
<tr>
<td><input type="checkbox" name="cbox[]" value="Stereo Set Operated at Excessive Sound Volume" />
Stereo Set Operated at Excessive Sound Volume</td>
<td>
<input type="checkbox" name="cbox[]" value="No Helmet/Head Gear" />
No Helmet/Head Gear</td>
<tr>
<td><input type="checkbox" name="cbox[]" value="No Trash Can/Receptacle" />
No Trash Can/Receptacle</td>
<td><input type="checkbox" name="cbox[]" value="Smoke Belching" />
Smoke Belching</td>
</tr>
<tr>
<td><input type="checkbox" name="cbox[]" value="Colorum PUV" />
Colorum PUV</td>
<td><input type="checkbox" name="cbox[]" value="Failure to Switch Headlights" />Failure to Switch Headlights</td>
</tr>
<tr>
<td><input type="checkbox" name="cbox[]" value="No Plate" />
No Plate</td>
<td><input type="checkbox" name="cbox[]" value="No U-turn" /> No U-turn</td>
</tr>
<tr>
<td><input type="checkbox" name="cbox[]" value="Driving under the Influence of Liquor" />
Driving under the Influence of Liquor</td>
<td><input type="checkbox" name="cbox[]" value="Dirty Plate" />
Dirty Plate</td>
</tr>
<tr>
<td><input type="checkbox" name="cbox[]" value="Counter Flow" />
Counter Flow</td>
<td><input type="checkbox" name="cbox[]" value="Arrogant Driver/Conductor" />Arrogant Driver/Conductor</td>
</tr>
<tr>
<td><input type="checkbox" name="cbox[]" value="Driver's/Conductor's Uniform/I.D." />
Driver's/Conductor's Uniform / I.D.</td>
<td><input type="text" name="oviolation" />
Other Traffic Violations(Specify)</td>
</tr></table>
<br>
</label>
<p align="center">
<p align="center"><strong>Apprehending Office ( Rank & Name )</strong><font color="#FF0000">*</font></p>
<label> </label>
<select name="type" size="1">
<option>Job Order</option>
<option>Permanent</option>
</select>
<input type="text" name="officer" placeholder="Officer Name"/>
</p>
<h5 align="center">PUBLIC ORDER & SAFETY OFFICE</h5>
<h5 align="center"> </h5>
<h5 align="center">
</h5>
</font>
<input type="submit" name="register" value="Add" class="adds"/>
<input type="reset" name="cancel" value="Reset" class="add"/>
<br />
</h5>
</form>
<?php
date_default_timezone_set('Asia/Manila');
$conn=mysql_connect("localhost","root","");
mysql_select_db("dbposo",$conn);
if(isset($_POST['register']))
{
$license=$_POST['license'];
$query = mysql_query("SELECT * FROM tblviolator WHERE license = '$license'");
$fetch_rows = mysql_num_rows($query);
if ( $fetch_rows > 0 )
{
echo "<script>alert('License # already exist')</script>";
header("Refresh: 0;url=new.php");
}
elseif( empty($_POST['stickerno']))
{
echo "<script>alert('Please check if all the * marker field is filled!')</script>";
header("Refresh: 0;url=new.php");
}
else
{
$stickerno=$_POST['stickerno'];
$fname=$_POST['fname'];
$mname=$_POST['mname'];
$lname=$_POST['lname'];
$address=$_POST['address'];
$bdate=$_POST['bdate'];
$plateno=$_POST['plateno'];
$puvbname=$_POST['puvbname'];
$vehicletype=$_POST['vehicletype'];
$puvbno=$_POST['puvbno'];
$vehicleowner=$_POST['vehicleowner'];
$taddress=$_POST['taddress'];
$violationplace=$_POST['violationplace'];
$datetime=$_POST['datetime'];
$confiscated=$_POST['RadioGroup1'];
$violation=$_POST['cbox'];
$oviolation=$_POST['oviolation'];
$type=$_POST['type'];
$officer=$_POST['officer'];
mysql_query("insert into tblviolator(license, stickerno, fname, mname, lname, address, bdate, plateno, puvbname, vehicletype, puvbno, vehicleowner, taddress, violationplace, datetime, confiscated, violation, oviolation, type, officer) values('$license','$stickerno','$fname','$mname','$lname','$address','$bdate','$plateno','$puvbname','$vehicletype','$puvbno','$vehicleowner','$taddress','$violationplace','$datetime','$confiscated','$violation','$oviolation','$type','$officer')",$conn) or die(mysql_error());
echo "<script>alert('Successfully Recorded, <br>
First penalty: 100php!')</script>";
header("Refresh: 0;url=new.php");
}
}
?>
What is my error here?
if you select more than one checkbox then $_POST['cbox'] becomes array .so try this
if(is_array($_POST['cbox']))
$violation=implode(',',$_POST['cbox']);
else
$violation=$_POST['cbox'];
To avoid injection better use http://php.net/manual/de/function.mysql-real-escape-string.php
If you check more than one option for the checkbox the variable $violation will be an array. Try to check multiple options and just safe the first one.
Probably if you want to store it as varchar you shoud concatenate all values.
this is the code I have, I need my message to appear in the top of my form but I got an error.
I need to print my message right where I put the code print $msg
any idea about this please.
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<table width="300" border="0">
<tr>
<td colspan="2"><?php print($msg); ?></td>
</tr>
<tr>
<td> *First name</td>
<td><span id="sprytextfield3">
<input type="text" value="" name="RFname" />
<br>
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr>
<td>*Last name</td>
<td><span id="sprytextfield4">
<input type="text" value="" name="RLname" />
<br>
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr>
<td>*User name</td>
<td><span id="sprytextfield1">
<input type="text" name="UserName" value="" />
<br />
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr>
<td>*Password</td>
<td><span id="sprypassword1">
<input type="password" name="UserPass" value="" />
<span class="passwordRequiredMsg"><br />
A value is required.</span></span></td>
</tr>
<tr>
<td>*Email</td>
<td><span id="sprytextfield2">
<input type="text" name="UserEmail" value="" />
<br />
<span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
</tr>
<tr>
<td>Web site</td>
<td><input type="text" name="WebSite" value="" /></td>
</tr>
<tr>
<td> </td>
<td><input name="submited" type="submit" onClick="MM_swapImage('Image1','','images/blog.png',1)" value="Register"/></td>
</tr>
</table>
</form>
</div>
<div class="image_NewUserRegister"><iframe src="images/Untitled-2.jpg" name="now_regist" width="207" height="280" scrolling="no" frameborder="0"></iframe></div>
</div>
<?php
if(isset($_POST['submited'])) {
$RFname = $_POST['RFname'];
$RLname = $_POST['RLname'];
$USERname = $_POST['UserName'];
$USERpassword = $_POST['UserPass'];
$USERemail = $_POST['UserEmail'];
$USERwebsite = $_POST['WebSite'];
$check=$db->query("select 1 from loginaccess where Email ='".$USERemail."'");
if(mysqli_num_rows($check) > 0 ) {
$msg = "<span style='color:#F00; font-size:14px; font-weight:bold;'>This email already taken</span>";
}
this is the code I have, I need my message to appear in the top of my form but I got an error. I need to print my message right where I put the code print $msg
any idea about this please.
You really need to set the message before you try to output it.
To elaborate on David's answer: move the PHP block before the form markup, like so:
<?php
if(isset($_POST['submited'])) {
$RFname = $_POST['RFname'];
$RLname = $_POST['RLname'];
$USERname = $_POST['UserName'];
$USERpassword = $_POST['UserPass'];
$USERemail = $_POST['UserEmail'];
$USERwebsite = $_POST['WebSite'];
$check=$db->query("select 1 from loginaccess where Email ='".$USERemail."'");
if(mysqli_num_rows($check) > 0 ) {
$msg = "<span style='color:#F00; font-size:14px; font-weight:bold;'>This email already taken</span>";
}
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<table width="300" border="0">
<tr>
<td colspan="2"><?php print($msg); ?></td>
</tr>
<tr>
<td> *First name</td>
<td><span id="sprytextfield3">
<input type="text" value="" name="RFname" />
<br>
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr>
<td>*Last name</td>
<td><span id="sprytextfield4">
<input type="text" value="" name="RLname" />
<br>
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr>
<td>*User name</td>
<td><span id="sprytextfield1">
<input type="text" name="UserName" value="" />
<br />
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr>
<td>*Password</td>
<td><span id="sprypassword1">
<input type="password" name="UserPass" value="" />
<span class="passwordRequiredMsg"><br />
A value is required.</span></span></td>
</tr>
<tr>
<td>*Email</td>
<td><span id="sprytextfield2">
<input type="text" name="UserEmail" value="" />
<br />
<span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
</tr>
<tr>
<td>Web site</td>
<td><input type="text" name="WebSite" value="" /></td>
</tr>
<tr>
<td> </td>
<td><input name="submited" type="submit" onClick="MM_swapImage('Image1','','images/blog.png',1)" value="Register"/></td>
</tr>
</table>
</form>
</div>
<div class="image_NewUserRegister"><iframe src="images/Untitled-2.jpg" name="now_regist" width="207" height="280" scrolling="no" frameborder="0"></iframe></div>
</div>
HTH.