I created dynamic textboxes, the other one has a type of file. My problem is
that I don't know how to save the image in the database and have a copy of it inside a folder cause I'm gonna retrieve it later on. But since it's in an array form I don't have an idea how to do it. I used blob for it, but if you have better way , lets see. I know how to make a copy of inside a folder but I didn't use a foreach, this I don't know.
Here's the code:
<script type="text/javascript">
$(document).ready(function () {
$("#append").click(function (e) {
e.preventDefault();
var textboxes = $(".textbox").length;
$(".inc").append("<div class='controls'><input class='textbox form-control' type='file' name='textbox[]'><input class='textbox form-control' type='text' name='box[]' ><input class='textbox form-control' type='text' name='box1[]' ><input class='textbox form-control' type='text' name='box2[]' ><a href='#' class='remove_this btn btn-danger'>remove</a> </div>");
});
$(document).on('click', '.remove_this', function (e) {
e.preventDefault();
$(this).parent().remove();
});
});
</script>
HTML:
<form class="form-horizontal" method= "POST">
<div class="control-group">
<div class="inc">
<div class="controls">
<button style="margin-left: 50px" class="btn btn-info" type="submit" id="append" name="append">
Add Textbox</button>
<br>
<br>
</div>
</div>
<input type="submit" class="btn btn-info" name="submit" value="submit"/>
</div>
PHP:
<?php
$host = "localhost";
$dbname = "lala";
$user = "root";
$pass = "";
$conn = new PDO("mysql:host=$host; dbname=$dbname", $user, $pass);
if (isset($_POST['submit'])) {
$textboxes = $_POST['textbox'];
$textboxes1 = $_POST['box1'];
$textboxes2 = $_POST['box2'];
$boxes = $_POST['box'];
$textboxes=addslashes(file_get_contents($_FILES['textboxes']['tmp_name']));
$image_name1=addslashes($_FILES['textboxes']['name']);
$image_size1=getimagesize($_FILES['textboxes']['tmp_name']);
move_uploaded_file($_FILES['textboxes']['tmp_name'],"images/".$_FILES['textboxes']['name']);
$location1="images/".$_FILES['textboxes']['name'];
foreach ($location1 as $key => $textbox ) {
$box=$textboxes[$key];
$box1=$textboxes1[$key];
$box2=$textboxes2[$key];
$sql = "INSERT into sasa (sasa,sasa1,sasa2,sasa3) values('$textbox','$box','$box1','$box2')";
echo $sql . "<br>";
$q = $conn->query($sql);
}
}
?>
Related
In my code I send $id to Java Script section and it returns the same value inside <p id="retid"></p> and I can't get the value as integer inside $text variable.
`while ($row = $result->fetch_assoc())
{
$id = $row["id"];
$regname = $row["reg-name"];
echo '<button class="btnicon btndel" type="button" name="answer" onclick="showDiv('.$id.')"><i class="fa fa-trash"></i> '.$regname.'</button>';
}
} else {
//header("Location: login.php?Msg=wrongPass"); }
}
?>
</div>
<div id="DelMessageDiv" style="display:none;" class="answer_list" >
<form action="_modify.php" method="get">
<div id="alerts">
<?php
$text = '<p id="retid"></p>';
echo $text.'-+-'.strip_tags($text,'<p>').'
Are you sure for delete?
<input name="id" type="hidden" value="'.strip_tags($text).'" />
<button class="btnicon btndel" type="submit" name="operation" value="0+regDelete">Yes Delete</button>
';
?>
</div>
</form>
</div>
</div>
<?php include '_footer.php'; ?>
<script src="js/extention/choices.js"></script>
<script>
const choices = new Choices('[data-trigger]',
{
searchEnabled: false
});
function showDiv(x) {
document.getElementById('DelMessageDiv').style.display = "block";
document.getElementById('retid').innerHTML = x;
}
</script>`
Thanks for all helps.
i want to insert picture in particular name but there is no insert and some facing error
my table like this
how can insert pic in front of particular name
config.php file is
<?php
session_start();
$db = new mysqli('localhost','root','','dharmesh');
?>
and code is here :
<?php
include_once('config.php');
if(isset($_POST['family_member_btn'])) {
$family_member = $_POST['family_member'];
}
if(isset($_POST["submit4"])) {
for($i=0;$i<$_POST['num'];$i++) {
$insert = $db->query("INSERT into `multiple_insert` (`f_name`,`m_name`,`l_name`,`birth_date`) values ('".$_POST['f_name'][$i]."','".$_POST['m_name'][$i]."','".$_POST['l_name'][$i]."','".$_POST['b_date'][$i]."')");
if($insert) {
echo "<script>alert('insert successfully');</script>";
} else {
echo "<script>alert('!!!insert unsuccessfully');</script>";
}
}
}
if(isset($_POST["upload"])) {
for($i=0; $i<$_POST['img'];$i++) {
$imag = $_FILES['f_name_pic']['name'][$i];
$tmp = $_FILES['f_name_pic']['tmp_name'][$i];
$dir = "images/".$imag;
move_uploaded_file($tmp,$dir);
$query = $db->query("UPDATE `multiple_insert` SET `picture`='$dir' WHERE f_name='".$_POST['f_name'][$i]."' where user_id='1'");
}
}
?>
<html>
<head>
<title>
</title>
</head>
<body>
<form method="post" action="">
<?php
$select = $db->query("SELECT * from multiple_insert where user_id='1'");
while($select_f_name = $select->fetch_assoc()){
echo "<input type='hidden' value='1' name='img' />";
echo "<p style='background-color:red;color:yellow;width:5%;'>".$select_f_name['f_name']."</p>";
echo "<span><input type='file' name='f_name_pic[]' /></span><br><br>";
}
echo "<button name='upload'>Uplaod</button><br><br>";
?>
<label><font size="2">HOW MANY MEMBER IN YOUR FAMILY ?</font></label><br><br>
<input type="text" name="family_member" class="form-control" />
<button type="submit" name="family_member_btn" class="btn btn-lg btn-info" /><span>SUBMIT</span></button><br><br>
<?php
for($i=1;$i<=$family_member;$i++) {
?>
<input type="hidden" value="<?php echo $family_member;?>" name="num" />
<label><b><?php echo "RECORED # ".$i;?></b></label><br>
<label><font size="2">First Name</font></label>
<input type="text" name="f_name[]" class="form-control" /><br>
<label><font size="2">Middle Name</font></label>
<input type="text" name="m_name[]" class="form-control" ><br>
<label><font size="2">Last name</font></label>
<input type="text" name="l_name[]" class="form-control" /><br>
<label><font size="2">birthdate</font></label>
<input id="datepicker3" class="form-control" name="b_date[]" type="text" />
<hr style="border:solid 2px rgba(0,0,0,0.2);">
<?php } ?>
<button class="btn btn-info btn-cons from-left pull-right" type="submit" name="submit4">
<span>SUBMIT</span>
</button>
</form>
</body>
</html>
how can i done this please send me updated code for this...
You cannot do that! The interface you are looking at, is phpMyAdmin---unless you are willing to hack the core code (HIGHLY NOT RECOMMENDED TO MODIFY CORE CODE OF ANY PROJECT!).
I have an HTML table that can be edited and saved. When saved I want it to update any changes made, to the database. Right now I have it so that whenever the table is saved, it automatically saves "23" into the "Buyer_ID" column because it is hardcoded in. However, how can I get it so that it is not hardcoded in and will read and save any values that are typed, added, or edited in the table?
HTML/PHP code:
<div id="dialog-form" title="Add Vendor">
<p class="validateTips">All form fields are required.</p>
<form>
<fieldset>
<label for="mr_name">Vendor</label>
<input type="text" name="mr_name" id="mr_name" class="text ui-widget-content ui-corner-all">
<label for="buyer_id">Buyer ID</label>
<input type="text" name="buyer_id" id="buyer_id" class="text ui-widget-content ui-corner-all">
<label for="poc_n">POC Name</label>
<input type="text" name="poc_n" id="poc_n" class="text ui-widget-content ui-corner-all">
<label for="poc_p">POC Email</label>
<input type="text" name="poc_e" id="poc_e" class="text ui-widget-content ui-corner-all">
<label for="poc_p">POC Phone</label>
<input type="text" name="poc_p" id="poc_p" class="text ui-widget-content ui-corner-all">
<!-- Allow form submission with keyboard without duplicating the dialog button -->
<input type="submit" id="submit" tabindex="-1" style="position:absolute; top:-1000px">
</fieldset>
</form>
</div>
<form method="post" action="ajaxsubmit.php">
<div id="users-contain" class="ui-widget">
<table id="html_master" class="ui-widget ui-widget-content">
<thead>
<tr class="ui-widget-header">
<td>ID</td>
<td>Vendor</td>
<td>Buyer ID</td>
<td>POC Name</td>
<td>POC Email</td>
<td>POC Phone</td>
<td>Edit/Delete</td>
</tr>
</thead>
<tbody>
<?php
foreach ($dbh->query($sql) as $rows){
?>
<tr>
<td class="mr_id" contenteditable="false"><?php echo intval ($rows['MR_ID'])?></td>
<td class="mr_name" name="field" contenteditable="false"><?php echo $rows['MR_Name']?></td>
<td class="buyer_id" contenteditable="false"><?php echo $rows['Buyer_ID']?></td>
<td class="poc_n" contenteditable="false"><?php echo $rows['MR_POC_N']?></td>
<td class="poc_e" contenteditable="false"><?php echo $rows['MR_POC_E']?></td>
<td class="poc_p" contenteditable="false"><?php echo $rows['MR_POC_P']?></td>
<td><input type="button" class="edit" name="edit" value="Edit">
<input type="button" class="deactivate" name="deactivate" value="Deactivate"></td>
</tr>
<?php
}
?>
</tbody>
<input type="button" class="create-user" value="Add Row">
<input type="submit" value="Save Table" class="save">
</table>
</div>
<input type="button" class="create-user" value="Add Row">
<input type="submit" value="Save Table" class="save">
</form>
JavaScript save function:
// ----- Save Table -----
$(document).on("click", ".save", function () {
// ------ Confirmation box in order to deactivate/activate row -----
if (confirm('Saving will update the entire table. Are you sure you want to save?')) {
var tabledata=[];
$( "#html_master tbody tr" ).each(function( index ) {
tabledata[index] = {
mr_id: $( this ).find("td.mr_id").text(),
mr_name: $( this ).find("td.mr_name").text(),
buyer_id: $( this ).find("td.buyer_id").text(),
poc_n: $( this ).find("td.poc_n").text(),
poc_e: $( this ).find("td.poc_e").text(),
poc_p: $( this ).find("td.poc_p").text()
};
});
}
});
Ajax submit code:
<?php
$host="xxxxxxxx";
$dbName="xxxxx";
$dbUser="xxxxxxxxxxx";
$dbPass="xxxxxxxx";
$dbh = new PDO( "sqlsrv:server=".$host."; Database=".$dbName, $dbUser, $dbPass);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
try {
$conn = new PDO( "sqlsrv:server=".$host."; Database=".$dbName, $dbUser, $dbPass);
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$qry=$conn->prepare('INSERT INTO Stage_Rebate_Master (Buyer_ID) VALUES (23)');
$qry->execute(Array("23" => $field));
} catch (PDOException $e) {
echo 'Error: ' . $e->getMessage() . " file: " . $e->getFile() . " line: " . $e->getLine();
exit;
}
?>
<form method="post">
<input type="text" name="field" value="<?php print $field; ?>">
<input type="submit" name="youraction" value="save">
</form>
Remove all of your <form> elements and use only AJAX. Change your JS on click handler to this.
$(document).on("click", ".save", function () {
if (confirm('Saving will update the entire table. Are you sure you want to save?')) {
var tabledata=[];
$( "#html_master tbody tr" ).each(function( index ) {
tabledata[index] = {
mr_id: $( this ).find("td.mr_id").text(),
mr_name: $( this ).find("td.mr_name").text(),
buyer_id: $( this ).find("td.buyer_id").text(),
poc_n: $( this ).find("td.poc_n").text(),
poc_e: $( this ).find("td.poc_e").text(),
poc_p: $( this ).find("td.poc_p").text()
}
});
$.ajax({
url: 'yourPHPfile.php', //Change this to your PHP file
data: {tableData: tabledata},
type: 'POST',
dataType: 'JSON',
error: function (request, error) {
console.log(error);
},
success: function (data) {
console.log(data);
}
});
}
});
PHP
<?php
$host="xxxxxxxx";
$dbName="xxxxx";
$dbUser="xxxxxxxxxxx";
$dbPass="xxxxxxxx";
$dbh = new PDO( "sqlsrv:server=".$host."; Database=".$dbName, $dbUser, $dbPass);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$tableArray = $_POST['tableData'];
try {
$conn = new PDO( "sqlsrv:server=".$host."; Database=".$dbName, $dbUser, $dbPass);
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
//Loop through each Array object and grab each objects value for the specified database column
foreach($tableArray as $key=>$value){
$qry=$conn->prepare('UPDATE Stage_Rebate_Master SET `Mr_Name`=?,`Buyer_ID`=?,`Poc_n`=?,`Poc_e`=?,`Poc_p`=? WHERE `Mr_Id`=?');
$qry->execute(Array($value->mr_name,$value->buyer_id,$value->poc_n,$value->poc_e,$value->poc_p,$value->mr_id));
}
} catch (PDOException $e) {
echo 'Error: ' . $e->getMessage() . " file: " . $e->getFile() . " line: " . $e->getLine();
exit;
}
?>
This passes a JSON array of objects to PHP with the AJAX post method. Change the column names in the prepare statement to match your database columns. It will update the entire database table with all the data in the HTML table once you click "confirm" in your "Save" dialog.
I suggest you do a lot more reading on PDO prepared statements as what you have currently is not even close to a prepared statement.
This is also assuming the mr_id column is your primary key. If it is not, you'll need to make those changes accordingly.
I would like to know how a multiple update in MySQL with checkbox because I'm not getting the id attribute checkbox someone could help me?
HTML code
<form method="post" action="../sys/fav.php">
<input type="hidden" id="id_photo" name="id" value="">
<div class="col-md-12">
<p align="right"><button class="btn btn-primary" id="final" name="final">Send</button></p>
</div>
<?php
require "../sys/connect.php";
$email = $_GET['email'];
$sql = mysqli_query($mysqli, "SELECT * FROM gallery WHERE email ='$email'");
while($row = mysqli_fetch_array($sql)){
$id = $row['id'];
$name = $row['nome_galeria'];
$emailcontact = $row['email_contact'];
$pass = $row['pass'];
$email = $row['email'];
$img = $row['img'];
print"<div class=\"col-lg-3 col-md-4 col-sm-6 col-xs-12\">
<input type=\"hidden\" name=\"contact\" value=\"$emailcontact\">
<input type=\"hidden\" name=\"login\" value=\"$email\">
<input type=\"hidden\" name=\"pass\" value=\"$pass\">
<input type=\"hidden\" name=\"name\" value=\"$name\">
<div class=\"hovereffect\">
<img id=\"he\" class=\"img-responsive\" src=\"../images/images/gallery/big/$img\" alt=\"$name\">
<div class=\"overlay\">
<div class=\"btn-group\" data-toggle=\"buttons\">
<label class=\"btn btn-primary cke\">
<input type=\"checkbox\" name=\"ck[]\" class=\"ck\" value=\"not\" id=\"ck_$id\"><i class=\"fa fa-heart\"></i>
</label>
</div>
</div>
</div>
</div>";
}
mysqli_close($mysqli)
?>
</form>
PHP Code
require "conexao.php";
if(isset($_POST['final'])){
$id = $_POST['id'];
foreach($_POST['ck'] as $ck){
$check = $ck;
$sql = mysqli_query($mysqli,"UPDATE gallery SET fav = '$check' WHERE id = '$id'")or die(mysqli_error($mysqli));
}
if($sql)
echo "Success!";
else
echo "Fail!";
}
Code Js to get the id of the checkbox
$("#final").click(function(){
var str = "";
var boxes = $(".ck");
for(var i = 0; i< boxes.length; i++){
if(boxes[i].checked == true){
var tmp = boxes[i].id.split("_");
str+=(i ? "," : "")+tmp[1];
}
}
document.getElementById('id_fotos').value=str;
});
You can get values of checkbox using below code and pass through ajax
var ckArray = new Array();
$.each($('input[name="ck[]"]:checked'),
function(index, ele){
ckArray.push($(ele).val());
});
I created an dynamic adding and removing text boxes, 4 textboxes in a row. Now i want the data in each textbox to be saved into database in one row; but under different attribute. I already tried it with only two textboxes and it works. But in case of 4 textboxes the data saved in the database is wrong. Every time you press the add textbox button another 4 textboxes will appear and the data in those 4 textboxes will be saved under another row.
Heres my code, please tell me whats wrong.
Javascript:
<script type="text/javascript">
$(document).ready(function () {
$("#append").click(function (e) {
e.preventDefault();
var textboxes = $(".textbox").length;
$(".inc").append("<div class='controls'><input class='textbox form-control' type='text' name='textbox[]'><input class='textbox form-control' type='text' name='box[]' ><input class='textbox form-control' type='text' name='box1[]' ><input class='textbox form-control' type='text' name='box2[]' ><a href='#' class='remove_this btn btn-danger'>remove</a> </div>");
});
$(document).on('click', '.remove_this', function (e) {
e.preventDefault();
$(this).parent().remove();
});
});
</script>
Html:
<form class="form-horizontal" method= "POST">
<div class="control-group">
<div class="inc">
<div class="controls">
<button style="margin-left: 50px" class="btn btn-info" type="submit" id="append" name="append">
Add Textbox</button>
<br>
<br>
</div>
</div>
<input type="submit" class="btn btn-info" name="submit" value="submit"/>
</div>
PHP:
$host = "localhost";
$dbname = "lala";
$user = "root";
$pass = "";
$conn = new PDO("mysql:host=$host; dbname=$dbname", $user, $pass);
if (isset($_POST['submit'])) {
$textboxes = $_POST['textbox'];
$textboxes1 = $_POST['box1'];
$textboxes2 = $_POST['box2'];
$boxes = $_POST['box'];
$x=0;
$a=0;
$z=0;
foreach ($textboxes as $textbox ) {
$y=0;
if($y==$x){
foreach ($boxes as $box ) {
if($z==$y){
foreach ($textboxes1 as $box1 ) {
if($a==$z){
foreach ($textboxes as $box2 ) {
if($y==$x){
$sql = "INSERT into sasa (sasa,sasa1,sasa2,sasa3) values('$textbox','$box','$box1','$box2')";
echo $sql . "<br>";
$q = $conn->query($sql);
}
$x++;
$y++;
}
}
$a++;
}
}
$z++;
}
}
}
}
Multiple nested forEach are not needed.
Try this:
$(document).ready(function() {
$("#append").click(function(e) {
e.preventDefault();
var textboxes = $(".textbox").length;
$(".inc").append("<div class='controls'><input class='textbox form-control' type='text' name='textbox[]'><input class='textbox form-control' type='text' name='box[]' ><input class='textbox form-control' type='text' name='box1[]' ><input class='textbox form-control' type='text' name='box2[]' ><a href='#' class='remove_this btn btn-danger'>remove</a> </div>");
});
$(document).on('click', '.remove_this', function(e) {
e.preventDefault();
$(this).parent().remove();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<form class="form-horizontal" method="POST">
<div class="control-group">
<div class="inc">
<div class="controls">
<button style="margin-left: 50px" class="btn btn-info" type="submit" id="append" name="append">
Add Textbox</button>
<br>
<br>
</div>
</div>
<input type="submit" class="btn btn-info" name="submit" value="submit" />
</div>
PHP:
<?php
$host = "localhost";
$dbname = "lala";
$user = "root";
$pass = "";
$conn = new PDO("mysql:host=$host; dbname=$dbname", $user, $pass);
if (isset($_POST['submit'])) {
$textboxes = $_POST['textbox'];
$textboxes1 = $_POST['box1'];
$textboxes2 = $_POST['box2'];
$boxes = $_POST['box'];
$x=0;
$a=0;
$z=0;
foreach ($textboxes as $key => $textbox ) {
$box=$textboxes[$key];
$box1=$textboxes1[$key];
$box2=$textboxes2[$key];
$sql = "INSERT into sasa (sasa,sasa1,sasa2,sasa3) values('$textbox','$box','$box1','$box2')";
echo $sql . "<br>";
$q = $conn->query($sql);
}
}
?>