Modal not updating data on row 2 and so on - php

Hi so the modal works fine on the first row but when i clicked on the 2nd row modal doesnt fetch any data
The form where it fetches the data , this thing works on first page only or either no
$ID = $_POST['ID'];
$sql = "SELECT * FROM `unity_users` WHERE `uid` = '$ID'";
$res = mysqli_query($con,$sql);
if(mysqli_num_rows($res)>0){
$row = mysqli_fetch_assoc($res);
$htmlToReturn = '<form action="" enctype="multipart/form-data" class="formTopMargin" autocomplete="off" method="post">
<div class="row">
<div class="col-sm-3"></div>
<div class="col-sm-6">
<div class="form-group">
<label for="cat">User Name:</label>
<input type="text" class="form-control" value="'.$row['name'].'" name = "name">
</div>
<div class="form-group">
<label for="cat">Email:</label>
<input type="email" class="form-control" value="'.$row['email'].'" name = "email">
</div>
<div class="form-group">
<label for="cat">Score:</label>
<input type="number" class="form-control" value="'.$row['score'].'" name = "score">
</div>
<div class="form-group">
<label for="cat">PPA:</label>
<input type="text" class="form-control" value="'.$row['ppa'].'" name = "ppa">
</div>
<div class="form-group">
<label for="cat">Hash:</label>
<input type="text" class="form-control" value="'.$row['hash'].'" name = "hash">
</div>
<div class="form-group">
<input type="hidden" hidden value="'.$row['uid'].'" name = "uid">
</div>
</div>
<div class="col-sm-3"></div>
</div>
<div class="row">
<div class="col-sm-3"></div>
<div class="col-sm-6">
<button type="submit" name="update" class="btn loginButton">Update</button>
</div>
<div class="col-sm-3"></div>
</div>
</form>';
The Output of the code
<?php
$sql = "SELECT * FROM `unity_users`";
$res = mysqli_query($con,$sql);
if(mysqli_num_rows($res)>0){
while ($row = mysqli_fetch_assoc($res)) {
echo '<tr>';
echo '<td>'.$row['uid'].'</td>';
echo '<td>'.$row['name'].'</td>';
echo '<td>'.$row['email'].'</td>';
echo '<td>'.$row['score'].'</td>';
echo '<td>'.$row['ppa'].'</td>';
echo '<td>'.$row['hash'].'</td>';
if($row['payment']=="Released"){
$payment = "requested";
}else{
$payment = "pending";
}
echo '<td>'.$payment.'</td>';
echo '<td class="pointer"><i id="delete_'.$row['uid'].'" class="fa fa-trash fa-2x details" aria-hidden="true"></i></td>';
echo '<td class="pointer"><i data-toggle="modal" data-target="#myModal" id="edit_'.$row['uid'].'" class="fa fa-pencil-square-o fa-2x details" aria-hidden="true"></i></td>';
echo '</tr>';
}
}
?>
The modal
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 style="text-align: center;margin-top: 1%;font-size: 25px;" class="modal-title">User Data</h4>
</div>
<div class="modal-body" id="categoryModel">
</div>
</div>
</div>
First row which is working
Updating data is working
Page 2 second row not working

Related

How to Update Mysql Data with a button on Home Page that is passing id on URL?

I have created an Edit Item popup Bootstrap modal on home page. Now what I want is to update the selected item details using the PHP.
Here i am passing item id through the URL in order to select the item
Now when I press edit Button it opens a Popup with all the details in it( already filled) but when I try to click on Save Changes Button it just do nothing and My data not updated. I have also used POST method but its not working.
Here is the Button that opens the Popup Modal
<?php
$res= mysqli_query($db,"SELECT* FROM `books`;");
if(mysqli_num_rows($res)>0){
while($row= mysqli_fetch_assoc($res)){
if($row!=0)
{
$bid= $row['book_id'];
?>
<ul>
<li>
<a onclick="$('#editModal<?php echo $row['book_id']?>').modal('show');" class="btn-show-modal edit-btn" data-toggle="modal"><i style="padding-right: 140px;" class="fas fa-edit"></i></a>
</li>
<!--Here is the Popup Modal for Edit -->
<div id="editModal<?php echo $row['book_id']?>" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Edit This Book</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="updatebook.php" method="POST">
<div class="form-group">
<label for="isbn" class="col-form-label">ISBN</label>
<input type="text" class="form-control" name="isbn" value="<?php echo $row['isbn'];?>">
</div>
<div class="form-group">
<label for="title" class="col-form-label">Enter Book Title</label>
<input type="text" class="form-control" name="title" value="<?php echo $row['title'];?>">
</div>
<div class="form-group">
<label for="author" class="col-form-label">Enter Author Name</label>
<input type="text" class="form-control" name="author" value="<?php echo $row['author'];?>">
</div>
<div class="form-group">
<label for="image" class="col-form-label">Image URL</label>
<input type="text" class="form-control" name="image" value="<?php echo $row['image'];?>">
</div>
<div class="form-group">
<label for="description" class="col-form-label">Enter Book Description</label>
<textarea class="form-control" name="description" value="<?php echo $row['description'];?>"></textarea>
</div>
<div class="form-group">
<label for="url" class="col-form-label">Book URL</label>
<input type="text" class="form-control" name="url" value="<?php echo $row['url'];?>"></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" name="save">Save Changes</button>
</div>
</div>
</div>
</div>
<?php
}
}
}
else
{
echo"<h1 style='color: white;font-size:58px; font-family: Poppins; font-weight: 600;' class=m-2>U!Oh Nothing Found Here!</h1>
<button onclick=\"window.location.href='add_books_form.php'\" class=\"btn btn-info bg-primary m-5\">Contribute</button>";
}
?>
</ul>
Here is the updatebook.php Page
<?php
include "connection.inc.php";
if(isset($_POST['save']))
{
$id= $_POST['id'];
$isbn= $_POST['isbn'];
$title= $_POST['title'];
$author= $_POST['author'];
$image= $_POST['image'];
$desc= $_POST['description'];
$url= $_POST['url'];
$res = mysqli_query($db, "UPDATE books SET isbn= '$isbn', title= '$title', author= '$author',image= '$image', description= '$desc', url= '$url' WHERE book_id= '$id'");
?>
if($res)
{
<script type="text/javascript">
alert("Data Updated Successfully");
window.location.href= "home.php";
</script>
}
else
{
<script>
alert("Not Updated!");
window.location.href="home.php";
</script>
}
<?php
}
?>
Please Help me to solve this Update Issue i am facing for a long time. I have researched all through but didn't got any solution.
Change your Save button type to "submit" and remove the <a> hyperlink.
Use <form> as a part of both modal-body and modal-footer so the submit button will be part of it.
Take book_id in a hidden field so it will be the part of POST.
Look at the modal code below:
<div id="editModal<?php echo $row['book_id']?>" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Edit This Book</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form action="updatebook.php" method="POST">
<input type="hidden" name="id" value="<?php echo $row['book_id'];?>">
<div class="modal-body">
<div class="form-group">
<label for="isbn" class="col-form-label">ISBN</label>
<input type="text" class="form-control" name="isbn" value="<?php echo $row['isbn'];?>">
</div>
<div class="form-group">
<label for="title" class="col-form-label">Enter Book Title</label>
<input type="text" class="form-control" name="title" value="<?php echo $row['title'];?>">
</div>
<div class="form-group">
<label for="author" class="col-form-label">Enter Author Name</label>
<input type="text" class="form-control" name="author" value="<?php echo $row['author'];?>">
</div>
<div class="form-group">
<label for="image" class="col-form-label">Image URL</label>
<input type="text" class="form-control" name="image" value="<?php echo $row['image'];?>">
</div>
<div class="form-group">
<label for="description" class="col-form-label">Enter Book Description</label>
<textarea class="form-control" name="description" value="<?php echo $row['description'];?>"></textarea>
</div>
<div class="form-group">
<label for="url" class="col-form-label">Book URL</label>
<input type="text" class="form-control" name="url" value="<?php echo $row['url'];?>"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" name="save">Save Changes</button>
</div>
</form>
</div>
</div>
</div>

pass value with a link and use it in php

I am learning PHP and trying to make one admin panel. I have a link like below in my table.
<i class="icon-eye"></i>
when clicking on the above link icon, it's opening one modal form, in that form I want to show all details of user of that row id from the table. I have my modal code and PHP code like below
<div id="modal_form_vertical" class="modal fade" tabindex="-1">
<?php
$users1_qry="SELECT * FROM users WHERE id = 1";
$result1=mysqli_query($mysqli,$users1_qry);
$row1=mysqli_fetch_assoc($result1);
?>
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header bg-primary">
<h5 class="modal-title">More Details</h5>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<form action="#">
<div class="modal-body">
<div class="form-group">
<div class="row">
<div class="col-sm-6">
<label>Username</label>
<input type="text" value = "<?php echo $row1['username'];?>" class="form-control" disabled>
</div>
<div class="col-sm-6">
<label>Email</label>
<input type="text" value = "<?php echo $row1['email'];?>" class="form-control" disabled>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6">
<label>Full Name</label>
<input type="text" value = "<?php echo $row1['name'];?>" class="form-control" disabled>
</div>
<div class="col-sm-6">
<label>Address</label>
<input type="text" value = "<?php echo $row1['address'];?>" class="form-control" disabled>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6">
<label>Phone</label>
<input type="text" value = "<?php echo $row1['phone'];?>" class="form-control" disabled>
</div>
<div class="col-sm-6">
<label>Pin</label>
<input type="text" value = "<?php echo $row1['pin'];?>" class="form-control" disabled>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6">
<label>Gender</label>
<input type="text" value = "<?php if($row1['gender'] ==0){echo 'Male';} else{echo 'Female';};?>" class="form-control" disabled>
</div>
<div class="col-sm-6">
<label>Occupation</label>
<input type="text" value = "<?php if($row1['occupation'] ==0){echo 'Parent';} else{echo 'Teacher';};?>" class="form-control" disabled>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6">
<label>Corporation Id</label>
<input type="text" value = "<?php echo $row1['corp'];?>" class="form-control" disabled>
</div>
<div class="col-sm-6">
<label>Password</label>
<input type="text" value = "<?php echo $row1['password'];?>" class="form-control" disabled>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-link" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>
You can see currently I have used 1 as user id in my mysqli query because I don't know how I can I pass row id to this modal class and so I can retrieve and use it. Let me know if someone can help me for same.
Thanks
You can send the ID by using POST or GET method, save it in the variable in your script and then use in the sql query.
Example:
<?php
$id = $_POST['id'];
$users1_qry="SELECT * FROM users WHERE id =".$id;
$result1=mysqli_query($mysqli,$users1_qry);
$row1=mysqli_fetch_assoc($result1);
?>
Consider using prepare/execute method so that you prevent possible SQL injections.
https://www.php.net/manual/en/pdo.prepare.php

Insert records to mysql database with php using Ajax

I am trying to added record using a modal. I have use the following code but my record is not added into the database.. help me through pls
<link rel="stylesheet" type="text/css" href="/Project/Style/bootstrap.css"/>
<script src="Style/jQuery/jquery-1.11.1.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<?php
include('header.php');
require_once('Connections/conn.php');
$sql2= "SELECT * from type where cid='1' ";
$results =mysqli_query($conn,$sql2);
?>
<div id="main">
<div class="container" >
<div class="row">
<div class="form-group col-md-3">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
++Add Farm Product
</button>
</div>
<div class="input-group col-md-4 col-sm-offset-2">
<span class="input-group-addon">Search</span>
<input type="text" class="form-control" name="search-text" id="search-text" placeholder="Search By Product Name">
</div>
</div>
<div id="result"></div>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Add Farm Product</h4>
</div>
<div class="modal-body">
<form>
<div class="row">
<div class="col-md-6 padding-top-10">
<label for="proname" class="control-label">Product Name:</label>
<input type="text" class="form-control" id="proname" placeholder="Product Name" name="proname" />
</div>
<div class="col-md-5 padding-top-10">
<label for="pro-type" class="control-label">Product Type:</label>
<select class="form-control" id="pro_type" name="pro_type" >
<option>Select Product type...</option>
<?php foreach($results as $result): ?>
<option value= " <?php echo $result['tid']; ?>" > <?php echo $result['type_name']; ?>
</option>
<?php endforeach ;?>
</select>
</div>
</div><br/>
<div class="row">
<div class="col-md-4 padding-top-10">
<label for="qty" class="control-label">Quantity:</label>
<input type="text" class="form-control" id="qty" placeholder="Quantity" name="qty" />
</div>
<div class="col-md-4 padding-top-10">
<label for="price" class="control-label">Unit Price :</label>
<input type="text" class="form-control" id="price" placeholder="Unit Price" name="price" />
</div>
<div class="col-md-4 padding-top-10">
<label for="pdate" class="control-label"> Production Date :</label>
<input type="date" class="form-control" id="pdate" placeholder="Production Date" name="pdate" />
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" >Close</button>
<button type="button" id="add" name="add" class="btn btn-primary">Add Product</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
<script>
// haddling the post for adding record
$('#add').click(function(){
var proname = $('#proname').val();
var pro_type = $('#pro_type').val();
var qty = $('#qty').val();
var price = $('#price').val();
var pdate = $('#pdate').val();
var datas="proname="+proname+"&pro_type="+pro_type+"&qty="+qty+"&price="+price+"&pdate"+pdate;
$.ajax({
type: "POST",
url: "addpro_exec.php",
data: datas,
success:function(data){
},
error: function(){
}
});
});
</script>
The code handling the posted data is below
//addpro_exec.php file
<?php
require_once('Connections/conn.php');
if(isset($_POST['add'])){
$proname=$_POST['proname'];
$pro_type= $_POST['pro_type'];
//$typname=$_POST['type_name'];
$qty= $_POST['qty'];
$price= $_POST['price'];
$pdate= $_POST['pdate'];
$asdate = \date('F');
$sql="INSERT into product VALUES('','$proname','','$qty','$price','$pdate','','$pro_type','1') ON DUPLICATE KEY UPDATE qty = qty+$qty";
$sql2= "INSERT into year_estm VALUES('','$proname','$asdate','$qty','') ON DUPLICATE KEY update Production_input=Production_input+$qty ";
$AddProduction= mysqli_query($conn, $sql2) or die('Cannot add to production'. mysqli_error($conn));
$RecAdd= mysqli_query($conn,$sql) or die('Cannot add Product' . mysqli_error());
if($RecAdd){
header('location:viewfarm.php');
}
}
else{
}
?>
Try the following line in your javascript:
var datas="add=1&proname="+proname+"&pro_type="+pro_type+"&qty="+qty+"&price="+price+"&pdate="+pdate;
That should fix the problem.
WARNING: Always filter and a properly quote user input before inserting into a database in order to avoid SQL injection hacks. Use prepared statements to help achieve this.
http://php.net/manual/en/mysqli.quickstart.prepared-statements.php

How to prevent $_SESSION from passing variable to another form even if the value doesn't exist in mysql?

I have two forms. The first form has two inputs: date and id. The id should be in the database otherwise, it will not save. What the $_SESSION does is that it passes the value from ID when I submit the form to the second form which is in the same page too. How can I prevent this from passing the value if the user has put a value that is not in the database? My php code issues a modal which says that the ID the the user has put is invalid however, it still passes to the other form...
Here is my php code:
<?php
include 'config.php';
if (isset($_POST['documentRequest1']))
{
$chkbox = $_POST['docs'];
$id = $_POST['a'];
$totalPrice = 0;
$total_cedula = 0;
$isPartOfTotal = 0;
foreach($chkbox as $chk1)
{
if($chk1=='Certificate of Residency')
{
$chek_val=$_POST['d1'];
$result = mysqli_query($conn, "SELECT price FROM document WHERE typeOfDoc = '$chk1';");
$row = mysqli_fetch_assoc($result);
$isPartOfTotal = $chek_val * $row["price"];
$sql = mysqli_query($conn, "INSERT into requestitem (DocumentRequest_idDocumentRequest, Document_idDocument, quantity, isPartOfTotal, price, paymentStatus) VALUES ((SELECT idDocumentRequest FROM documentrequest WHERE Person_idPerson = '$id'), (SELECT idDocument FROM document WHERE typeOfDoc = '$chk1'), '$chek_val', '$isPartOfTotal',' $total_cedula', 'Unpaid');");
}
else if($chk1=='Barangay Clearance')
{
$chek_val=$_POST['d2'];
$result = mysqli_query($conn, "SELECT price FROM document WHERE typeOfDoc = '$chk1';");
$row = mysqli_fetch_assoc($result);
$isPartOfTotal = $chek_val * $row["price"];
$sql = mysqli_query($conn, "INSERT into requestitem (DocumentRequest_idDocumentRequest, Document_idDocument, quantity, isPartOfTotal, price, paymentStatus) VALUES ((SELECT idDocumentRequest FROM documentrequest WHERE Person_idPerson = '$id'), (SELECT idDocument FROM document WHERE typeOfDoc = '$chk1'), '$chek_val', '$isPartOfTotal',' $total_cedula', 'Unpaid');");
}
else if ($chk1=='Cedula')
{
$chek_val=$_POST['d3'];
//$income=$_POST['income'];
$result = mysqli_query($conn, "SELECT income FROM person WHERE idPerson = '$id';");
$row = mysqli_fetch_assoc($result);
$income = $row['income'];
$total_cedula = ($income * 12 * .001) + 5;
$isPartOfTotal = $chek_val * $total_cedula;
$sql = mysqli_query($conn, "INSERT into requestitem (DocumentRequest_idDocumentRequest, Document_idDocument, quantity, isPartOfTotal, price, paymentStatus) VALUES ((SELECT idDocumentRequest FROM documentrequest WHERE Person_idPerson = '$id'), (SELECT idDocument FROM document WHERE typeOfDoc = '$chk1'), '$chek_val', '$isPartOfTotal',' $total_cedula', 'Unpaid');");
}
// partial total
$result = mysqli_query($conn, "SELECT price FROM document WHERE typeOfDoc = '$chk1';");
$row = mysqli_fetch_assoc($result);
$isPartOfTotal = $chek_val * $row["price"];
}
?>
<!-- Modal -->
<div class="modal fade" id="step2" tabindex="-1" role="dialog" aria-labelledby="memberModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header" style = "background-color: #5bc0de; color: white;">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="memberModalLabel">Request added.</h4>
</div>
<div class="modal-body">
<h5>You have successfully added a new request. However, it is still tagged as unpaid.<br> This will redirect you to the invoice page.</h5>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php
//header("Refresh: 5;url=all_docRequest.php");
mysqli_close($conn);
}
?>
First form html code:
<form class="form-horizontal form-label-left" name = "documentRequest" enctype="multipart/form-data" role="form" method="post" novalidate>
<div class = "first">
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Date of Request <span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="reqDate" class="form-control col-md-7 col-xs-12" name="reqDate" required="required" type="date" >
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Resident's Name <span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input name="person_id" id="search-box" class="form-control col-md-7 col-xs-12" placeholder ="Type in ID number..." required="required" type="text">
</div>
</div>
</div>
<div class="ln_solid"></div>
<div class="form-group">
<div class="col-md-6 col-md-offset-3">
<button id="requestDiv" type="submit" class="btn btn-success" name="documentRequest" >Proceed</button>
<button type="submit" class="btn btn-primary">Cancel</button>
</div>
</div>
</form>
Second form:
<form class="form-horizontal form-label-left" name = "documentRequest1" enctype="multipart/form-data" role="form" method="post" novalidate>
<div class = "second">
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">ID <span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input name="a" id="search" class="form-control col-md-7 col-xs-12" value = "<?php echo $_SESSION['person_id']; ?>" required="required" type="text" readonly="readonly">
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12"> Document Request(s) <span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="checkbox" name="docs[]" id="doc1" value="Certificate of Residency"/> Certificate of Residency
<div class = "clearfix"></div>
<div class = "col-xs-3">
<input name="d1" class="form-control col-md-7 col-xs-12" required="required" type="number">
</div>
<div class = "clearfix"></div>
<input type="checkbox" name="docs[]" id="doc2" value="Barangay Clearance"/> Barangay Clearance
<div class = "clearfix"></div>
<div class = "col-xs-3">
<input name="d2" class="form-control col-md-7 col-xs-12" required="required" type="number"/>
</div>
<div class = "clearfix"></div>
<input type="checkbox" name="docs[]" id="doc3" value="Cedula"/> Cedula
<div class = "clearfix"></div>
<div class = "col-xs-3">
<input name="d3" class="form-control col-md-7 col-xs-12" required="required" value = "1" type="number" readonly = "readonly"/>
</div>
</div>
</div>
</div>
<div class="ln_solid"></div>
<div class="form-group">
<div class="col-md-6 col-md-offset-3">
<button id="requestDiv1" type="submit" class="btn btn-success" name="documentRequest1" >Proceed</button>
<button type="submit" class="btn btn-primary">Cancel</button>
</div>
</div>
</form>
This is the $_SESSION code:
<?php
session_start();
$_SESSION['person_id']=isset($_POST['person_id']) ? $_POST['person_id'] :'';
?>
I really need your help on this one... Your help will be much appreciated. Thank you.

Saving html data in two different mysql table with single submit button

I have a page with some input fields and a table whose rows are dynamically generated using jQuery. I am trying to save the input fields in one mysql table and the dynamically generated rows in another. The input fields are working fine. But with dynamically generated table rows, only the first row is inserting, other roes below the first is not inserting. Here are my codes. Please tell me what am I doing wrong. Thanks a ton.
HTML FORM
<form class="form-horizontal" role="form" action="saveFiles/savePurchase.php" method="post" target="">
<div class="form-group">
<label for="invoiceNo" class="col-md-6 control-label">Invoice No.</label>
<div class="col-md-6">
<input type="text" class="form-control" name="invoiceNo">
</div>
</div>
<div class="form-group">
<label for="supplier" class="col-md-6 control-label">Supplier</label>
<div class="col-md-5">
<select name="supplier" class="form-control">
<option>SELECT</option>
<?php include('saveFiles/connection.php');
$query = mysql_query("SELECT Id,Name FROM supplier");
while($row = mysql_fetch_assoc($query)){
$supplierId = $row['Id'];
$supplierName = $row['Name'];?>
<option value="<?php echo $supplierId;?>"><?php echo $supplierName;?></option>
<?php }
?>
</select>
</div>
<div class="col-md-1">
<img src="img/plus.png" class="pull-right" style="margin-top: 5px;" data-toggle="modal" data-target="#supplierModal">
</div>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="invoiceNo" class="col-md-6 control-label">Invoice Date</label>
<div class="col-md-6">
<input type="text" class="form-control datepicker" name="invoiceDate" readonly="readonly">
</div>
</div><br><br>
<div class="form-group">
<label for="paymentMode" class="col-md-6 control-label">Payment Mode</label>
<div class="col-md-6">
<select name="paymentMode" class="form-control">
<option>Cash</option>
<option>Cheque</option>
<option>Card</option>
<option>On Account</option>
<option>Credit</option>
</select>
</div>
</div>
</div>
<div class="col-md-3"></div>
</div>
<div class="row">
<div class="col-md-12">
<table class="table table-condensed table-responsive">
<thead>
<th>Product Name</th>
<th>Unit</th>
<th>Qty</th>
<th><button type="button" class="btn btn-info" id="addNew">Add New Row</button></th>
</thead>
<tbody>
<tr class="cloneme">
<td>
<select name="productName[]" class="form-control" style="width: 110px;">
<option>SELECT</option>
<?php
include('saveFiles/connection.php');
$selectQuery = mysql_query("SELECT Id,ProductName FROM items ORDER BY ProductName");
while($rows = mysql_fetch_assoc($selectQuery)){
$productId = $rows['Id'];
$productName = $rows['ProductName']; ?>
<option value="<?php echo $productId;?>"><?php echo $productName?></option>
<?php }
?>
</select>
</td>
<td> <input type="text" class="form-control" name="unit[]"></td>
<td> <input type="text" class="form-control" name="qty[]"></td>
</tr>
</tbody>
</table>
<div class="row">
<div class="col-md-12 text-center">
<button class="btn btn-success" type="submit" name="submit" id="save">Save</button>
<button class="btn btn-danger" type="reset" name="reser">Clear</button>
</div>
</div>
</form>
<div class="row">
<div class="col-md-12 text-center">
<iframe name="purchaseMsg" style="border: none; background-color: #00aceb; !important; width:0px; height:0px;"></iframe>
</div>
</div>
</div>
</div>
</div>
<div class="modal fade" id="supplierModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
<h4 class="modal-title">Add Supplier</h4>
</div>
<div class="modal-body">
<div class="form-group">
<form action="saveSupplier" method="post" target="categoryMsg">
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4">
<input type="text" class="form-control" name="newSupplier" placeholder="Add New Supplier">
</div>
<div class="col-md-4"></div>
</div><br>
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4">
<input type="button" class="btn btn-success"value="Save" id="saveSupplier" data-dismiss="modal">
<input type="reset" class="btn btn-danger" value="Clear">
</div>
<div class="col-md-4"></div>
</div>
</form>
And this is my php script to insert the form.
<?php
include('connection.php');
if(isset($_POST['submit'])){
$invoiceNo = $_POST['invoiceNo'];
$invoiceDate = $_POST['invoiceDate'];
$supplierId = $_POST['supplier'];
$insertQuery = mysql_query("INSERT INTO purchase(InvoiceNo,InvoiceDate,SupplierId) VALUES('$invoiceNo','$invoiceDate','$supplierId')");
$id = mysql_insert_id();
$rowData = array();
foreach($_POST['productName'] as $row=>$itemName){
$productName = mysql_real_escape_string($itemName);
$unit = mysql_real_escape_string($_POST['unit'][$row]);
$qty = mysql_real_escape_string($_POST['qty'][$row]);
$rowData[] = "('$productName','$unit','qty')";
}
$query=mysql_query("INSERT INTO purchaseDetails(ItemName, Unit, Quantity) VALUES".implode(',',$rowData));
}
?>
Please help me with this. Thank you.
I think it might be related to the way you construct the multiple insert records, so instead of this...
rowData[] = "('$productName','$unit','qty')";
You should maybe try something like this...
rowData[] = "(\"".$productName."\", \"".$unit."\", \"".$qty."\")";

Categories