Inserting multiple rows with PHP - php

After importing a csv file, I'm trying to import the data with an insert statement, which is sending around 250 rows for one table.
The inserting, and code is working very well, but I had to print the insert query and the insert just worked till the row number 100.
PHP screen, which I import csv file data to the screen:
if(#$_FILES[file][type] == 'text/csv' || #$_FILES[file][type] == 'text/comma-separated-values' || #$_FILES[file][type] == 'application/vnd.ms-excel'){
if($_POST['FUNCAO'] == 'FORM_IMPORT')
{
error_reporting(E_ALL);
##### UPLOAD FILE
$uploaddir = 'gap_files/';
#$filename="gap_files/".$_FILES[file][name];
#$uploadfile = $filename;
$count = 0;
$id_linha = 0;
$itens_po = '';
$pos_to_import = '';
if (!#move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile))
{
print "<pre>";
print_r($_FILES);
print "$php_errormsg</pre>";
}
if(#$_FILES[file][name])
{
print '
<html>
<head>
<title>PO</title>
<link rel="stylesheet" type="text/css" href="../_includes/css/padrao.css" >
<script language="JavaScript" type="text/javascript" src="../_includes/js/padrao.js"></script>
<script language="JavaScript" type="text/javascript" src="../_includes/js/po.js"></script>
<script language="JavaScript" type="text/javascript" src="../_includes/js/label.js"></script>
</head>
<body>
<div id="div_report" style="background-color:#2B2B2B; position:fixed; top:0px; width:200%; height:200%; display:none; opacity:0.35; filter:alpha(opacity=35);"> </div>
<div id="div_link_color" style="display:none; position:fixed; margin-left: -300px; margin-top: -240px; left:50%; top:50%; width:600px; height:280px; background-color:#525252; text-align:center; z-index: 1000; text-align:center; border-radius: 0.4em; ">
<center>
<table cellpadding="3" cellspacing="0">
<tr>
<td colspan="3" class="REPORT_EDIT_TITLE" colspan="3" align="center" id="title_box_comment"></td>
</tr>
<tr>
<td colspan="3">
<iframe style="width:587px; height:230px; background-color:#EAF0FA; text-align:center; text-align:center; border-radius: 0.4em; overflow:auto; border:none;" id="frame_link_color" name="frame_link_color"></iframe>
</td>
</tr>
<tr>
<td colspan="3" align="center"><input type="button" name="comments" id="pre_production_comm" onclick="close_link_color();" value="Close" class="button" style="width:60px; height:30px;"></td>
</tr>
</table>
</center>
</div>
<div width="100%" id="FRAMESBOTTON">
<table cellpadding="1" cellspacing="0">
<tr>
<td class="botton-apple"><img src="../img/user.png" /> <b>Information</b> </td>
<td class="botton-apple" onclick="seleciona_div_po(3)"><img src="../img/help.png" /> <b>Help</b> </td>
<td class="botton-apple" onclick="seleciona_div_po(9)"><img src="../img/application.png" /> <b>Import Label</b> </td>
</tr>
</table>
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td class="titlestyle" colspan="6" align="center"><b>Labels to import</b></td>
</tr>
</table>
</div>
';
$po = '';
$line=1;
$po_show = '';
$itens_po = '';
$factory_ids = '';
$wrong_price = '';
$import_error = 0;
$invoice_port = '';
$invoice_number = '';
$number_of_invoices = 0;
$invoice_gross_weight = '';
$invoice_total_cartons = '';
$total_pairs_invoice = 0;
$total_amount_invoice = 0;
$total_amount_discount_invoice = 0;
$tr_check_po = '';
$filename="gap_files/".#$_FILES[file][name];
$handle = fopen("$filename", "r");
$actual_po = '';
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
foreach($data as $varname => $value)
{
if($value){
$data[$varname]=mysql_real_escape_string($value);
}
else{
$data[$varname]="NULL";
}
}
$check_if_inserted_invoice = mysql_query("select * from po_label where po_label.po = '".$data[3]."'");
if(mysql_num_rows($check_if_inserted_invoice) == 0){
if($line != 0){
$count++;
$itens_po .= '
<input type="hidden" name="iten_'.$count.'" value="'.$line.';'.$data[3].';'.$data[26].';'.$data[15].';'.$data[4].';'.$data[8].';'.$data[0].';'.$data[23].';'.$data[1].';"/>
<tr style="background-color:#ECEBEB;" onmouseover="ButtonRegSet(this,1)" onmouseout="ButtonRegSet(this)">
<td class="fontrecord" align="center"><input type="hidden" name="carton_number_'.$line.'" value="'.$line.'" readonly>'.$line.'</td>
<td class="fontrecord" align="center"><input type="hidden" name="po_'.$line.'" value="'.$data[3].'" readonly>'.$data[3].'</td>
<td class="fontrecord" align="center"><input type="hidden" name="carton_'.$line.'" value="'.$data[26].'" readonly>'.$data[26].'</td>
<td class="fontrecord" align="center"><input type="hidden" name="EANCode_'.$line.'" value="'.$data[15].'" readonly>'.$data[15].'</td>
<td class="fontrecord" align="center"><input type="hidden" name="size_'.$line.'" value="'.$data[4].'" readonly>'.$data[4].'</td>
<td class="fontrecord" align="center"><input type="hidden" name="pairs_'.$line.'" value="'.$data[8].'" readonly>'.$data[8].'</td>
<td class="fontrecord" align="center"><input type="hidden" name="customer_style_'.$line.'" value="'.$data[0].'" readonly>'.$data[0].'</td>
<td class="fontrecord" align="center"><input type="hidden" name="material_'.$line.'" value="'.$data[23].'" readonly>'.$data[23].'</td>
<td class="fontrecord" align="center"><input type="hidden" name="colors_'.$line.'" value="'.$data[1].'" readonly>'.$data[1].'</td>
</tr>';
}
$line++;
}
}
fclose($handle);
}
$number_of_invoices++;
print '
<form method="post" action="../_registers/po_import_label.php">
<input type="hidden" maxlength="30" name="FUNCAO" value="insert_import_po" readonly>
<input type="hidden" maxlength="30" name="itens_form" value="'.$line.'" readonly>
<center>
</br>
<table class="round_sub_information_title_close" width="90%">
<tr>
<td colspan="8" class="round_sub_information_title_close" style="background-color:#FFFFFF;">
<div id="id_itens_invoices_'.$number_of_invoices.'" style="display:block;">
<center>
<table cellpadding="0" cellspacing="1" style="min-width:99%; width:99.8%;">
<tr class="line_grid">
<td class="fontrecord" align="center">Carton Number</td>
<td class="fontrecord" align="center">PO</td>
<td class="fontrecord" align="center">Carton Code</td>
<td class="fontrecord" align="center">Case Code</td>
<td class="fontrecord" align="center">Size</td>
<td class="fontrecord" align="center">Quantity</td>
<td class="fontrecord" align="center">Style</td>
<td class="fontrecord" align="center">Material</td>
<td class="fontrecord" align="center">Color</td>
</tr>
'.$itens_po.'
</table>
</center>
</div>
</td>
</tr>
<tr>';
print '
<tr><td class="round_sub_information_title_close" style="background-color:#FFFFFF;"><input type="submit" value="Save"/></td></tr>';
print '</tr>
</table>
</center>
</form>';
}
}
File: "../_registers/po_import_label.php"
<?php
session_start();
if(!$_SESSION["uid"]){
die(header("location: ../login.php"));
}
include('../_library/config.php');
include('../_library/opendb.php');
include("../_library/functions.php");
$s = "insert into po_label (carton,
carton_number,
po,
size,
pairs,
colors,
material,
customer_style,
EANCode) values";
for($i=1;$i<$_POST['itens_form'];$i++)
{
$s .="('".$_POST['carton_'.$i]."',
'".$_POST['carton_number_'.$i]."',
'".$_POST['po_'.$i]."',
'".$_POST['size_'.$i]."',
'".$_POST['pairs_'.$i]."',
'".$_POST['colors_'.$i]."',
'".$_POST['material_'.$i]."',
'".$_POST['customer_style_'.$i]."',
'".$_POST['EANCode_'.$i]."'),";
}
$s = rtrim($s,",");
print $s;
return mysql_query($s) ? 'INSERTED.' : 'ERROR: '.mysql_error();
include '../library/closedb.php';
?>
After import to the screen, I call the insert function to send the data to the database, it is when I got the error.
insert into po_label (carton, carton_number, po, size, pairs, colors, material, customer_style, EANCode) values (...),('10355000000098','98','0355CC0001','7',' 6','CAMEL','SOFT NUBUCK','CC-CRYSTALL','191644000270'),('10355000000099','99','0355CC0001','7',' 6','CAMEL','SOFT NUBUCK','CC-CRYSTALL','191644000270'),('10355000000100','100','0355CC0001','7',' 6','','SOFT NUBUCK','CC-CRYSTALL','191644000270'),('','','','','','','','',''),('','','','','','','','',''),('','','','','','','','',''),('','','','','','','','',''),(...)
Adding an observation: Before I updated the files in my official system, I tried and test it in the Wamp server, where everything works well.
If i have only 100 rows, the insert works well
If i have more than 100 rows, you can check in the bottom that after the row 100 the insert becomes empty

1) Please confirm whether all values coming without spaces by following function
print_r()
2) Use the trim() function to every single data passed in insert query
INSERT INTO TBL_NAME VALUES(
'".trim($value[0])."',
'".trim($value[1]))";
May it will help

I think you should improve your below code with given
// Your code
for($i=1;$i<$_POST['itens_form'];$i++)
{
$s .="('".$_POST['carton_'.$i]."',
'".$_POST['carton_number_'.$i]."',
'".$_POST['po_'.$i]."',
'".$_POST['size_'.$i]."',
'".$_POST['pairs_'.$i]."',
'".$_POST['colors_'.$i]."',
'".$_POST['material_'.$i]."',
'".$_POST['customer_style_'.$i]."',
'".$_POST['EANCode_'.$i]."'),";
}
for($i=1;$i<$_POST['itens_form'];$i++)
{
// Remove POST for columns in which you have allowed null in data table
if($_POST['carton_'.$i] != '' && $_POST['carton_number_'.$i] != '' && $_POST['po_'.$i] != '' && $_POST['size_'.$i] != '' && $_POST['pairs_'.$i] != '' && $_POST['colors_'.$i] != '' && $_POST['material_'.$i] != '' && $_POST['customer_style_'.$i] != '' && $_POST['EANCode_'.$i] != '')
{
$s .="('".$_POST['carton_'.$i]."',
'".$_POST['carton_number_'.$i]."',
'".$_POST['po_'.$i]."',
'".$_POST['size_'.$i]."',
'".$_POST['pairs_'.$i]."',
'".$_POST['colors_'.$i]."',
'".$_POST['material_'.$i]."',
'".$_POST['customer_style_'.$i]."',
'".$_POST['EANCode_'.$i]."'),";
}
}
UPDATE
As per the given comment, you should increase the length of URI. Please check below Increase your URI limit in your apache server.
but the best way to insert bulk data is mysqldump please see below answer for more information regarding mysqldump using PHP Answer

Related

Including PHP files - separate files works fine, included files produces error

Newbie here with weird error.
Before I start - I've read and understood (at least I think so) this topic:
How to fix "Headers already sent" error in PHP
which has great overview of this error, but I did not find solution there.
Now the problem:
I have 2 simple .php files (05.php and 10.php)... both of them contain 5 table rows. First 3 columns are not relevant (simple text). Fourth column reads content of .txt file and displays it. Fifth column has 2 submit buttons("+" and "-") which basicly open .txt file, read number in it, changes it's value, replace value in file with changed one and refreshes site. I need to include both of those files in my "main.php" file.
When I open them separetly, they work fine, when I include any of them in "main.php", everything works fine, but when I include both of them, input buttons on firt 7 rows work fine, but buttons on rows 8-10 give me this error:
Warning: Cannot modify header information - headers already sent by
(output started at C:\xampp\htdocs\test\10.php:94) in
C:\xampp\htdocs\test\10.php on line 120
Second line number changes when I press different input buttons (which is obvious). First line number is always the same, and points to line with this code :
</form>
I cannot figure out what's wrong...
I know that this code is not "nice" and best way of doing this kind of stuff, but like I said... newbie on deck :)
Here are full codes of "05.php", "10.php" and "main.php"...
"05.php"
<tr>
<td align="right" valign="middle">Papier półpołysk klej standard</td>
<td align="right" valign="middle">DIG UV INKJET MC80 FSC S2000N-BG40BR</td>
<td align="right" valign="middle">0,166</td>
<td align="right" valign="middle">
<?php $fp01 = fopen("licznik01.txt", "r+");
$count01 = fgets($fp01);
fclose($fp01);
echo $count01;
?>
</td>
<td align="center" valign="middle">
<form method="post">
<input class="czerwony" name="minus01" id="minus01" value="-" type="submit">
<?php function odejmij01()
{
$fp01 = fopen("licznik01.txt", "r+");
$count01 = fgets($fp01);
$count01 = $count01 - 1;
ftruncate($fp01, 0);
fseek($fp01, 0);
fputs($fp01, $count01);
fclose($fp01);
header("Refresh:0");
}
if(array_key_exists('minus01',$_POST)){
odejmij01();
}
?>
<input class="zielony" name="plus01" id="plus01" value="+" type="submit">
<?php function dodaj01()
{
$fp01 = fopen("licznik01.txt", "r+");
$count01 = fgets($fp01);
$count01 = $count01 + 1;
ftruncate($fp01, 0);
fseek($fp01, 0);
fputs($fp01, $count01);
fclose($fp01);
header("Refresh:0");
}
if(array_key_exists('plus01',$_POST)){
dodaj01();
}
?>
</form>
</td>
</tr>
<tr>
<td align="right" valign="middle"></td>
<td align="right" valign="middle"></td>
<td align="right" valign="middle">0,200</td>
<td align="right" valign="middle">
<?php $fp02 = fopen("licznik02.txt", "r+");
$count02 = fgets($fp02);
fclose($fp02);
echo $count02;
?>
</td>
<td align="center" valign="middle">
<form method="post">
<input class="czerwony" name="minus02" id="minus02" value="-" type="submit">
<?php function odejmij02()
{
$fp02 = fopen("licznik02.txt", "r+");
$count02 = fgets($fp02);
$count02 = $count02 - 1;
ftruncate($fp02, 0);
fseek($fp02, 0);
fputs($fp02, $count02);
fclose($fp02);
header("Refresh:0");
}
if(array_key_exists('minus02',$_POST)){
odejmij02();
}
?>
<input class="zielony" name="plus02" id="plus02" value="+" type="submit">
<?php function dodaj02()
{
$fp02 = fopen("licznik02.txt", "r+");
$count02 = fgets($fp02);
$count02 = $count02 + 1;
ftruncate($fp02, 0);
fseek($fp02, 0);
fputs($fp02, $count02);
fclose($fp02);
header("Refresh:0");
}
if(array_key_exists('plus02',$_POST)){
dodaj02();
}
?>
</form>
</td>
</tr>
<tr>
<td align="right" valign="middle"></td>
<td align="right" valign="middle"></td>
<td align="right" valign="middle">0,250</td>
<td align="right" valign="middle">
<?php $fp03 = fopen("licznik03.txt", "r+");
$count03 = fgets($fp03);
fclose($fp03);
echo $count03;
?>
</td>
<td align="center" valign="middle">
<form method="post">
<input class="czerwony" name="minus03" id="minus03" value="-" type="submit">
<?php function odejmij03()
{
$fp03 = fopen("licznik03.txt", "r+");
$count03 = fgets($fp03);
$count03 = $count03 - 1;
ftruncate($fp03, 0);
fseek($fp03, 0);
fputs($fp03, $count03);
fclose($fp03);
header("Refresh:0");
}
if(array_key_exists('minus03',$_POST)){
odejmij03();
}
?>
<input class="zielony" name="plus03" id="plus03" value="+" type="submit">
<?php function dodaj03()
{
$fp03 = fopen("licznik03.txt", "r+");
$count03 = fgets($fp03);
$count03 = $count03 + 1;
ftruncate($fp03, 0);
fseek($fp03, 0);
fputs($fp03, $count03);
fclose($fp03);
header("Refresh:0");
}
if(array_key_exists('plus03',$_POST)){
dodaj03();
}
?>
</form>
</td>
</tr>
<tr>
<td align="right" valign="middle"></td>
<td align="right" valign="middle"></td>
<td align="right" valign="middle">0,333</td>
<td align="right" valign="middle">
<?php $fp04 = fopen("licznik04.txt", "r+");
$count04 = fgets($fp04);
fclose($fp04);
echo $count04;
?>
</td>
<td align="center" valign="middle">
<form method="post">
<input class="czerwony" name="minus04" id="minus04" value="-" type="submit">
<?php function odejmij04()
{
$fp04 = fopen("licznik04.txt", "r+");
$count04 = fgets($fp04);
$count04 = $count04 - 1;
ftruncate($fp04, 0);
fseek($fp04, 0);
fputs($fp04, $count04);
fclose($fp04);
header("Refresh:0");
}
if(array_key_exists('minus04',$_POST)){
odejmij04();
}
?>
<input class="zielony" name="plus04" id="plus04" value="+" type="submit">
<?php function dodaj04()
{
$fp04 = fopen("licznik04.txt", "r+");
$count04 = fgets($fp04);
$count04 = $count04 + 1;
ftruncate($fp04, 0);
fseek($fp04, 0);
fputs($fp04, $count04);
fclose($fp04);
header("Refresh:0");
}
if(array_key_exists('plus04',$_POST)){
dodaj04();
}
?>
</form>
</td>
</tr>
<tr>
<td align="right" valign="middle">Papier półpołysk klej mocny</td>
<td align="right" valign="middle">DIG UV INKJET MC80 FSC S2045N-BG40BR </td>
<td align="right" valign="middle">0,166</td>
<td align="right" valign="middle">
<?php $fp05 = fopen("licznik05.txt", "r+");
$count05 = fgets($fp05);
fclose($fp05);
echo $count05;
?>
</td>
<td align="center" valign="middle">
<form method="post">
<input class="czerwony" name="minus05" id="minus05" value="-" type="submit">
<?php function odejmij05()
{
$fp05 = fopen("licznik05.txt", "r+");
$count05 = fgets($fp05);
$count05 = $count05 - 1;
ftruncate($fp05, 0);
fseek($fp05, 0);
fputs($fp05, $count05);
fclose($fp05);
header("Refresh:0");
}
if(array_key_exists('minus05',$_POST)){
odejmij05();
}
?>
<input class="zielony" name="plus05" id="plus05" value="+" type="submit">
<?php function dodaj05()
{
$fp05 = fopen("licznik05.txt", "r+");
$count05 = fgets($fp05);
$count05 = $count05 + 1;
ftruncate($fp05, 0);
fseek($fp05, 0);
fputs($fp05, $count05);
fclose($fp05);
header("Refresh:0");
}
if(array_key_exists('plus05',$_POST)){
dodaj05();
}
?>
</form>
</td>
</tr>
"10.php"
<tr>
<td align="right" valign="middle"></td>
<td align="right" valign="middle"></td>
<td align="right" valign="middle">0,250</td>
<td align="right" valign="middle">
<?php $fp06 = fopen("licznik06.txt", "r+");
$count06 = fgets($fp06);
fclose($fp06);
echo $count06;
?>
</td>
<td align="center" valign="middle">
<form method="post">
<input class="czerwony" name="minus06" id="minus06" value="-" type="submit">
<?php function odejmij06()
{
$fp06 = fopen("licznik06.txt", "r+");
$count06 = fgets($fp06);
$count06 = $count06 - 1;
ftruncate($fp06, 0);
fseek($fp06, 0);
fputs($fp06, $count06);
fclose($fp06);
header("Refresh:0");
}
if(array_key_exists('minus06',$_POST)){
odejmij06();
}
?>
<input class="zielony" name="plus06" id="plus06" value="+" type="submit">
<?php function dodaj06()
{
$fp06 = fopen("licznik06.txt", "r+");
$count06 = fgets($fp06);
$count06 = $count06 + 1;
ftruncate($fp06, 0);
fseek($fp06, 0);
fputs($fp06, $count06);
fclose($fp06);
header("Refresh:0");
}
if(array_key_exists('plus06',$_POST)){
dodaj06();
}
?>
</form>
</td>
</tr>
<tr>
<td align="right" valign="middle"></td>
<td align="right" valign="middle"></td>
<td align="right" valign="middle">0,250</td>
<td align="right" valign="middle">
<?php $fp07 = fopen("licznik07.txt", "r+");
$count07 = fgets($fp07);
fclose($fp07);
echo $count07;
?>
</td>
<td align="center" valign="middle">
<form method="post">
<input class="czerwony" name="minus07" id="minus07" value="-" type="submit">
<?php function odejmij07()
{
$fp07 = fopen("licznik07.txt", "r+");
$count07 = fgets($fp07);
$count07 = $count07 - 1;
ftruncate($fp07, 0);
fseek($fp07, 0);
fputs($fp07, $count07);
fclose($fp07);
header("Refresh:0");
}
if(array_key_exists('minus07',$_POST)){
odejmij07();
}
?>
<input class="zielony" name="plus07" id="plus07" value="+" type="submit">
<?php function dodaj07()
{
$fp07 = fopen("licznik07.txt", "r+");
$count07 = fgets($fp07);
$count07 = $count07 + 1;
ftruncate($fp07, 0);
fseek($fp07, 0);
fputs($fp07, $count07);
fclose($fp07);
header("Refresh:0");
}
if(array_key_exists('plus07',$_POST)){
dodaj07();
}
?>
</form>
</td>
</tr>
<tr>
<td align="right" valign="middle">Papier półpołysk klej odlepny</td>
<td align="right" valign="middle">DIG INDI MC80 TOP FSC R5000N-BG40BR </td>
<td align="right" valign="middle">0,166</td>
<td align="right" valign="middle">
<?php $fp08 = fopen("licznik08.txt", "r+");
$count08 = fgets($fp08);
fclose($fp08);
echo $count08;
?>
</td>
<td align="center" valign="middle">
<form method="post">
<input class="czerwony" name="minus08" id="minus08" value="-" type="submit">
<?php function odejmij08()
{
$fp08 = fopen("licznik08.txt", "r+");
$count08 = fgets($fp08);
$count08 = $count08 - 1;
ftruncate($fp08, 0);
fseek($fp08, 0);
fputs($fp08, $count08);
fclose($fp08);
header("Refresh:0");
}
if(array_key_exists('minus08',$_POST)){
odejmij08();
}
?>
<input class="zielony" name="plus08" id="plus08" value="+" type="submit">
<?php function dodaj08()
{
$fp08 = fopen("licznik08.txt", "r+");
$count08 = fgets($fp08);
$count08 = $count08 + 1;
ftruncate($fp08, 0);
fseek($fp08, 0);
fputs($fp08, $count08);
fclose($fp08);
header("Refresh:0");
}
if(array_key_exists('plus08',$_POST)){
dodaj08();
}
?>
</form>
</td>
</tr>
<tr>
<td align="right" valign="middle"></td>
<td align="right" valign="middle"></td>
<td align="right" valign="middle">0,250</td>
<td align="right" valign="middle">
<?php $fp09 = fopen("licznik09.txt", "r+");
$count09 = fgets($fp09);
fclose($fp09);
echo $count09;
?>
</td>
<td align="center" valign="middle">
<form method="post">
<input class="czerwony" name="minus09" id="minus09" value="-" type="submit">
<?php function odejmij09()
{
$fp09 = fopen("licznik09.txt", "r+");
$count09 = fgets($fp09);
$count09 = $count09 - 1;
ftruncate($fp09, 0);
fseek($fp09, 0);
fputs($fp09, $count09);
fclose($fp09);
header("Refresh:0");
}
if(array_key_exists('minus09',$_POST)){
odejmij09();
}
?>
<input class="zielony" name="plus09" id="plus09" value="+" type="submit">
<?php function dodaj09()
{
$fp09 = fopen("licznik09.txt", "r+");
$count09 = fgets($fp09);
$count09 = $count09 + 1;
ftruncate($fp09, 0);
fseek($fp09, 0);
fputs($fp09, $count09);
fclose($fp09);
header("Refresh:0");
}
if(array_key_exists('plus09',$_POST)){
dodaj09();
}
?>
</form>
</td>
</tr>
<tr>
<td align="right" valign="middle"></td>
<td align="right" valign="middle"></td>
<td align="right" valign="middle">0,333</td>
<td align="right" valign="middle">
<?php $fp10 = fopen("licznik10.txt", "r+");
$count10 = fgets($fp10);
fclose($fp10);
echo $count10;
?>
</td>
<td align="center" valign="middle">
<form method="post">
<input class="czerwony" name="minus10" id="minus10" value="-" type="submit">
<?php function odejmij10()
{
$fp10 = fopen("licznik10.txt", "r+");
$count10 = fgets($fp10);
$count10 = $count10 - 1;
ftruncate($fp10, 0);
fseek($fp10, 0);
fputs($fp10, $count10);
fclose($fp10);
header("Refresh:0");
}
if(array_key_exists('minus10',$_POST)){
odejmij10();
}
?>
<input class="zielony" name="plus10" id="plus10" value="+" type="submit">
<?php function dodaj10()
{
$fp10 = fopen("licznik10.txt", "r+");
$count10 = fgets($fp10);
$count10 = $count10 + 1;
ftruncate($fp10, 0);
fseek($fp10, 0);
fputs($fp10, $count10);
fclose($fp10);
header("Refresh:0");
}
if(array_key_exists('plus10',$_POST)){
dodaj10();
}
?>
</form>
</td>
</tr>
"main.php"
<!DOCTYPE html">
<html><head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<link rel="stylesheet" href="style.css">
<title>Etykiety - Stan magazynowy</title>
</head>
<body>
<div style="text-align: center;">
<h1>Etykiety - Stan magazynowy</h1>
<form method="post">
<h2><input class="odswiez" name="odswiez" id="odswiez" value="ODŚWIEŻ DANE" type="submit"></h2>
<?php function odswiez()
{
header("Refresh:0");
}
if(array_key_exists('odswiez',$_POST)){
odswiez();
}
?>
</form>
<table align=center style="text-align: left; width: 750px;" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<th align="right" valign="middle" height="35px">Nazwa Materiału</th>
<th align="right" valign="middle">Kod producenta</th>
<th align="right" valign="middle">Szerokość</th>
<th align="right" valign="middle">Stan</th>
<th align="center" valign="middle">Akcje</th>
</tr>
<?php include '05.php';?>
<?php include '10.php';?>
</tbody>
</table>
<form method="post">
<h2><input class="odswiez" name="odswiez2" id="odswiez2" value="ODŚWIEŻ DANE" type="submit"></h2>
<?php function odswiez2()
{
header("Refresh:0");
}
if(array_key_exists('odswiez2',$_POST)){
odswiez2();
}
?>
</form>
</div>
</body></html>
Any help is a great help :)
EDIT:
Instead of including those files, I've just copy/pasted their code into "main.php" and it is exactly the same. Separatly pasted works fine, pasted together give the same error...
Long story short
There must not be sent anything even a white space till all headers are sent.
The issue
In your case the script sends plenty of html code, text, etc. and then suddenly it tries to modify the headers with calls like header("Refresh:0"); and this is wrong.
You should send all the headers before the page content is sent to the browser.
Solution:
1. sent all headers at the beginning of the script and then continue with the content
OR
2. use Output Buffering to catch all the output and flush it after all modification of headers are done.
BUT do not mix output and headers together.
Proof of concept
You can comment all the lines with header("Refresh:0"); This should suppress the headers already sent php errors ;)

PHP Delete Data with checkbox

So im kinda trying to delete row from my mysql database using checkboxes. Here is my code. Would be glad if someone could write me down simple delete code. Tryed many but failed, stuck for hours with this :(
<html>
<head>
<title>Admin, User</title>
</head>
<body>
<?php include 'connect.php';?>
<?php include 'functions.php';?>
<?php include 'title_bar.php';?>
<h3>Sukurti Nauja uzduoti: </h3>
<form method='post'>
<?php
if(isset($_POST['submit2']))
{
$pav = $_POST['pav'];
$uzduotis = $_POST['uzduotis'];
if(empty($pav) or empty($uzduotis)){
echo "<p>Privalomi visi langai!</p>";
}
else {
$sql = ("INSERT INTO uzduotys VALUES ('','$pav','$uzduotis')");
}
if($database->query($sql) == TRUE)
{
header('location:kurtisalinti.php');
}
else {
echo "<p>Klaida!</p>";
}
}
?>
<p>Uzduoties pavadinimas:
<p><input type='text' name='pav' />
<p>Uzduotis:
<p><textarea name='uzduotis'></textarea>
<p><input type='submit' name='submit2' value='Sukurti Uzduoti' />
</form>
</p></p></p></p>
</form>
<h3>Pasalinti pasirinkta uzduoti is uzduociu saraso: </h3>
<?php
$query = mysqli_query($database,"SELECT uzid,pav,uzduotis FROM uzduotys");
$count=mysqli_num_rows($query);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Pavadinimas</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Aprasymas</strong></td>
</tr>
<?php
while($rows = mysqli_fetch_array($query)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" value="<?php echo $row['uzid']; ?>"></td>
<td bgcolor="#FFFFFF"><?php echo $rows['uzid']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['pav']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['uzduotis']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Istrinti Uzduotis"></td>
</tr>
<?php
?>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
Welcome to StackOverflow! Generally as a rule of thumb asking to write you some code doesn't get you very far, however I know that some people learn better from seeing it implemented and being able to modify off of that.
There is a number of ways you can do this. Using something like jquery and ajax you can post your requests without having to reload the page. Keep in mind what I have below may not fully work for you since taking the time to do this much already without a database or connection should give you an idea of what you're looking to do. Good luck!
CAUTION: PSUEDO CODE (This may not fully work and is not intended for production) The idea is to show you an approach to achieving what you are looking for.
<?php
include('connect.php');
include('functions.php');
include('title_bar.php');
if(isset($_POST['submit-uzduotis']))
{
$pav = $_POST['pav'];
$uzduotis = $_POST['uzduotis'];
if(empty($pav) or empty($uzduotis))
{
$result = "<span>Privalomi visi langai!</span>";
}
else
{
$sql = ("INSERT INTO uzduotys VALUES ('','$pav','$uzduotis')");
}
if($database->query($sql) == true)
{
header('location:kurtisalinti.php');
}
else
{
$result = "<span>Įvyko klaida!</span>";
}
}
if(isset($_POST['remove']))
{
$id = mysql_real_escape_string($_POST['remove']);
mysqli_query($database, "DELETE FROM uzduotys WHERE uzid = $id");
}
function showTableData()
{
$query = mysqli_query($database, "SELECT uzid, pav, uzduotis FROM uzduotys");
$count = mysqli_num_rows($query);
while($row = mysqli_fetch_array($query))
{
echo '<tr id="'. $row['uzid'] .'">
<td>
<input type="checkbox" name="checkbox[]" value="'. $row['uzid'] .'">
</td>
<td>'. $row['uzid'] .'</td>
<td>'. $row['pav'] .'</td>
<td>'. $row['uzduotis'] .'</td>
<td>
<button id="remove-single" data-id="'. $row['uzid'] .'">REMOVE</button>
</td>
</tr>';
}
}
?>
<html>
<head>
<title>Admin, User</title>
<style>
.uzduotis-table {
background-color: #ccc;
border-collapse: collapse;
}
.uzduotis-table thead > td {
background-color: #333;
font-weight: bold;
}
.uzduotis-table td {
padding: 3px;
text-align: center;
background-color: #fff;
}
label {
display:block;
position:relative;
font-weight: bold;
}
</style>
</head>
<body>
<h3>Sukurti Nauja uzduoti: </h3>
<form method='post'>
<?php echo $result; ?>
<div>
<label>Uzduoties pavadinimas:</label>
<input type='text' name='pav' />
</div>
<div>
<label>Uzduoties:</label>
<textarea name='uzduotis'></textarea>
</div>
<div>
<input type='submit' name='submit-uzduoties' value='Sukurti Uzduoti' />
</div>
</form>
<h3>Pasalinti pasirinkta uzduoti is uzduociu saraso: </h3>
<table class="uzduotis-table">
<thead>
<tr>
<td>#</td>
<td>Id</td>
<td>Pavadinimas</td>
<td>Aprasymas</td>
</tr>
</thead>
<tbody>
<?php showTableData(); ?>
<tr>
<td colspan="5">
<button id="remove-selected" data-id="'. $row['uzid'] .'">REMOVE SELECTED</button>
</td>
</tr>
</tbody>
</table>
<script src="https://code.jquery.com/jquery-2.2.3.min.js" integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo=" crossorigin="anonymous"></script>
<script>
$(document).ready(function(){
// Remove Single (Button)
$('body').on('click', '#remove-single', function() {
remove($(this).data('id'));
});
// Remove All Checked
$('body').on('click', '#remove-selected', function() {
$('.uzduotis-table tr').filter(':has(:checkbox:checked)').each(function() {
remove(this.id);
});
});
function remove(id)
{
console.log("Remove: " + id);
alert('Removing: ' + id);
$.ajax({
type: "POST",
url: '<?php echo $url; ?>',
data: { remove: id },
success: function (data) {
$('#' + id).remove();
}
});
}
})
</script>
</body>
</html>
I cleaned up a bit of the html and styles, just made it easier for me to read. Also I would recommend looking into a library such as PDO to handle your database queries as there currently isn't much in place to protect or safe guard against sql injection.

why the data does not add in database mysql? [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
I have to make one system where the system requires the process of adding new information. The process flow is the process of adding the new information carried by the employee where all the information, name and id of staff will insert in mysql database. When I tried the process of that, I found staff_id and staff_name was empty in database mysql.
This is my addStaff.php
<?php
include("authenticationStaff.php");
include ("dbase.php");
$query= "SELECT * FROM staff WHERE staff_name ='".$_SESSION['SESS_STAFF_NAME']."'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$id = $row["id"];
$staff_id=$row["staff_id"];
#mysql_free_result($result);
?>
<html>
<head>
<script>
function Validate()
{
if (document.addStaff.project_name.value == '')
{
alert('Please Insert Project Name!');
document.addStaff.project_name.focus();
return false;
}
if (document.addStaff.project_id.value == '')
{
alert('Please Insert Project ID !');
document.addStaff.project_id.focus();
return false;
}
if (document.addStaff.location.value == '')
{
alert('Please Insert Location!');
document.addStaff.location.focus();
return false;
}
if (document.addStaff.cost.value == '')
{
alert('Please Insert Cost!');
document.addStaff.cost.focus();
return false;
}
if (document.addStaff.pic.value == '')
{
alert('Please Insert Person In Charge!');
document.addStaff..pic.focus();
return false;
}
if (document.addStaff.detail.value == '')
{
alert('Please Insert Detail about the Project!');
document.addStaff.detail.focus();
return false;
}
}
</script>
</head>
<body>
<table width="869" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="645" height="50" align="left" valign="middle"><strong>Welcome , You log in as <?php echo $_SESSION['SESS_STAFF_NAME'];?> </strong></td>
<td width="224" align="right" valign="middle"><strong>Log Out</strong></td>
</tr>
</table>
<?php
$idURL = $_GET['id'];
$query ="SELECT *
FROM staff s
JOIN inter1 i
ON (s.staff_name=i.staff_name)";
$result = mysql_query($query, $conn) or die("Could not execute query");
$row = mysql_fetch_array($result, MYSQL_BOTH); // using numeric index or array index
$staff_id = $row['staff_id'];
$project_name = $row['project_name'];
$location = $row['location'];
$detail = $row['detail'];
$pic = $row['pic'];
$staff_name = $row['staff_name'];
$project_id = $row['project_id'];
#mysql_free_result ($result);
?>
<center>
<form action="addStaff_process.php?id=<?php echo $staff_id; ?>" method="post" enctype="multipart/form-data" name="add_process" id="add_process" onSubmit="return Validate()" >
<table border="1" width="70%" cellspacing="1" cellpadding="6" >
<tr align="center" bgcolor="">
<td align="left" style="padding:10px 10px 10px 10px;" >PROJECT NAME :</td>
<td align="left" style="padding:10px 10px 10px 10px;" ><textarea name="project_name" cols="50" rows="2" id="project_name"></textarea></td>
</tr>
<tr align="center" bgcolor="">
<td align="left" style="padding:10px 10px 10px 10px;" >PROJECT ID:</td>
<td align="left" style="padding:10px 10px 10px 10px;" ><textarea name="project_id" cols="50" rows="2" id="project_id"></textarea></td>
</tr>
<tr align="center" bgcolor="">
<td align="left" style="padding:10px 10px 10px 10px;" >LOCATION :</td>
<td align="left" style="padding:10px 10px 10px 10px;" ><textarea name="location" cols="50" rows="2" id="location"></textarea></td>
</tr>
<tr align="center" bgcolor="">
<td align="left" style="padding:10px 10px 10px 10px;" > COST :</td>
<td align="left" style="padding:10px 10px 10px 10px;" ><textarea name="cost" cols="50" rows="2" id="cost"></textarea></td>
</tr>
<tr align="center" bgcolor="" >
<td align="left" style="padding:10px 10px 10px 10px;" >PERSON IN CHARGE :</td>
<td align="left" style="padding:10px 10px 10px 10px;" ><textarea name="pic" cols="50" rows="3" id="pic"></textarea></td>
</tr>
<tr>
<td align="left" style="padding:10px 10px 10px 10px;">DETAIL ABOUT THE PROJECT:</td>
<td align="left" style="padding:10px 10px 10px 10px;" ><textarea name="detail" cols="50" rows="4" id="detail"></textarea></td>
</tr>
</table>
<p> </p>
<p>
<input class="form-submit" type="submit" name="submit" value="SUBMIT" onClick="return Validate()"/>
<input type = "reset" value = "RESET" />
</p>
</form>
</center>
</body>
</html>
And this is my addStaff_process.php
<?php
include("authenticationStaff.php");
include ("dbase.php");
extract( $_POST );
$idURL = $_GET['id'];
$project_name = $_POST['project_name'];
$location = $_POST['location'];
$cost= $_POST['cost'];
$pic= $_POST['pic'];
$detail = $_POST['detail'];
$project_id = $_POST['project_id'];
$staff_id = $_POST['staff_id'];
$staff_name = $_SESSION['SESS_STAFF_NAME'];
$result ="INSERT INTO inter1 (project_name,location,cost,pic,detail,project_id)
VALUES ('$project_name','$location','$cost','$pic','$detail','$project_id')";
$query = mysql_query ($result, $conn);
if(mysql_num_rows($query)){
echo "<script type='text/javascript'> window.location='pageStaff.php'</script>";
}
else{
$query1 = "UPDATE inter1 SET staff_name = '$staff_name',staff_id = '$staff_id' WHERE staff_name=$idURL";
$result1 = mysql_query ($query1, $conn);
if($result1){
echo "<script type='text/javascript'> window.location='pageStaff.php'</script>";
}
}
?>
Can someone see where I am going wrong?if you need any more info then please let me know. thanks.
You need to use session_start(); at the start of every page that accesses the session values.

If Condition inside php variable

I am stuck with a problem.
I have HTML element inside PHP variable. Now i want if condition inside html elements.
My code is like this.
<?php
$x = 1;
$html =
'<style>
b
{
paading:0;
margin:0;
}
</style>
<table width="790px" style="font-family:Open Sans;">
<!--Want If Condition Here-->
if($x != 1)
{
<tr>
<td colspan="2">
My Separate Portion
</td>
</tr>
}
<strong></strong>
<tr> <td colspan="2"> <hr style="background-color:#E44510; height:10px;"> </td> </tr>';
echo $html;
?>
If anyone can help me out, its very appericiable.
<?php
$x = 1;
$html =
'<style>
b
{
paading:0;
margin:0;
}
</style>
<table width="790px" style="font-family:Open Sans;">';
if($x != 1)
{
$html .= '<tr>
<td colspan="2">
My Separate Portion
</td>
</tr>';
}
$html .= '<strong></strong>
<tr> <td colspan="2"> <hr style="background-color:#E44510; height:10px;"> </td> </tr>';
echo $html;
?>
OR else you can use this in easy way..
<?php
$x = 1;
?>
<style>
b
{
paading:0;
margin:0;
}
</style>
<table width="790px" style="font-family:Open Sans;">
<!--Want If Condition Here-->
<?php
if($x != 1)
{
?>
<tr>
<td colspan="2">
My Separate Portion
</td>
</tr>
<?php
}
?>
<strong></strong>
<tr> <td colspan="2"> <hr style="background-color:#E44510; height:10px;"> </td> </tr>

Passing value inside JavaScript function and saving it into a PHP variable

I am creating an application like that of Facebook photo albums having comments.
I have a webpage, which shows the pics inside an album, when a pic is clicked, a lightbox opens up, and the caption and comments on the pic will be shown. Here's the pictures.php code
<?php
session_start();
ob_start;
include('connection.php');
?>
<html>
<head>
<title>Photos</title>
<style type="text/css">
.wraptocenter
{
width: 200px;
height: 150px;
overflow: hidden;
}
.wraptocenter img
{
vertical-align: top;
}
.black_overlay
{
display: none;
position: fixed;
top: 0%;
left: 0%;
width: 100.7%;
height: 100%;
background-color: black;
z-index: 1001;
-moz-opacity: 0.8;
opacity: .80;
filter: alpha(opacity=80);
}
.white_imagebox
{
display: none;
position: fixed;
top: 5%;
left: 6%;
width: 900px;
height: 500px;
padding: 0px;
border: 0px solid #a6c25c;
background-color: white;
z-index: 1002;
overflow: visible;
}
</style>
<script type="text/javascript" src="AJAX/AjaxInsertPicComment.js"></script>
<script>
function showpic(image_path,image_id,album_id,pic_caption)
{
document.getElementById('AlbumDiv').style.display = "block";
document.getElementById('fade').style.display = "block";
document.getElementById('image').src = image_path; // this line added
img = new Image();
img.src = image_path;
document.getElementById('t_albumid').value = album_id;
document.getElementById('t_imageid').value = image_id;
document.getElementById('t_albid').value = album_id;
document.getElementById('t_picid').value = image_id;
document.getElementById('albumid').value = album_id;
document.getElementById('imageid').value = image_id;
document.getElementById('t_imagepath').value = image_path;
document.getElementById('caption_holder').value = pic_caption;
if(img.width > 500 && img.height > 450)
{
if(img.width > img.height)
{
document.getElementById('image').style.width = "500px";
document.getElementById('image').style.height = 'auto';
delete img;
return false;
}
else
{
document.getElementById('image').style.height = "450px";
document.getElementById('image').style.width = 'auto';
delete img;
return false;
}
}
else if(img.height > 450 && img.width < 500)
{
document.getElementById('image').style.height = "450px";
delete img;
return false;
}
else if(img.height < 450 && img.width > 500)
{
if(img.width > img.height)
{
document.getElementById('image').style.width = "500px";
document.getElementById('image').style.height = 'auto';
delete img;
return false;
}
else
{
document.getElementById('image').style.height = "450px";
document.getElementById('image').style.width = 'auto';
delete img;
return false;
}
}
else if(img.width < 500 && img.height < 450)
{
if(img.width > img.height)
{
document.getElementById('image').style.width = "500px";
document.getElementById('image').style.height = 'auto';
delete img;
return false;
}
else
{
document.getElementById('image').style.height = "450px";
document.getElementById('image').style.width = 'auto';
delete img;
return false;
}
}
delete img;
alert("humm");
return false;
}
</script>
</head>
<body>
<div id="photo_holder">
<table width="1000px" cellpadding="0" cellspacing="0" border="0" align="center">
<tr>
<td width="50px" align="center"/>
<td align="center" colspan="2" background="Images/header_menu.png" style="padding-right:2px;">
<?php include('header.php');?>
</td>
<td width="50px" align="center"/>
</tr>
<tr>
<td width="50px" align="center" ></td>
<td width="600px" align="center">
<?php
$album_id=$_REQUEST['txt_albumid'];
echo $album_id;
/* how many columns */
$column_number='3';
/* html table start */
?>
<div id="photo_container" align="center" width="600px">
<table border="1px" cellspacing="5" cellpadding="0" align="left">
<?php
$sql="SELECT * FROM candidate_pics WHERE album_id='$album_id'";
$result=mysql_query($sql,$con);
// $row=mysql_fetch_array($result);
$recordcounter=1; /* counts the records while they are in loop */
while($row=mysql_fetch_array($result))
{
/* decide if there will be new Table row (<TR>) or not ... left of division by column number is the key */
if($recordcounter%$column_number==1)
{
echo "<tr>";
}
?>
<td align="center" width="200px">
<div class="wraptocenter" align="center">
<?php $_SESSION['pic_id']=$row[pic_id];?>
<a href="javascript:void(0)"
onClick="showpic('<?php echo $row[pic_path];?>','<?php echo $row[pic_id];?>',
'<?php echo $row[album_id];?>','<?php echo $row[pic_caption];?>')";>
<img src="<?php echo $row[pic_path];?>"/></a>
</div>
</td>
<?php
/* decide if there will be end of table row */
if($recordcounter%$column_number==0)
{
echo "</tr>";
}
$recordcounter++; /* increment the counter */
}
if(($recordcounter%$column_number)!=1){ echo "</tr>"; }
?>
</table>
</div>
</td>
<td width="300px" align="center" >
<div id="photo_uploader">
<form method="post" action="photo_upload.php" enctype="multipart/form-data">
<table width="300px" align="center" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" width="100px">Upload Pic::</td>
<td align="center" width="200px">
<input type="file" name="file" id="file" style="width: 200px;"/>
</td>
</tr>
<tr>
<td align="center" width="100px"/>
<td align="center" width="200px">
<input type="submit" name="submit_pic" id="submit_pic" value="Click to upload" style="width: 200px;"/>
<input name="txt_albumid" type="hidden" value="<?php echo $album_id;?>"/>
</td>
</tr>
</table>
</form>
</div>
</td>
<td width="50px" align="center" ></td>
</tr>
</table>
</div>
<div id="AlbumDiv" class="white_imagebox">
<table align="center" cellpadding="0" cellspacing="0" border="0" width="900px">
<tr>
<td colspan="2" height="25px">
<div id="close">
<a href="javascript:void(0)"
onclick="document.getElementById('AlbumDiv').style.display =
'none';document.getElementById('fade').style.display='none'">
<img src="images/close-icon.png" style="border-style: none; border-color: inherit;
border-width: 0px; height: 17px; width: 16px;" align="right" /></a>
</div>
</td>
</tr>
<tr>
<td width="600px" align="center">
<table width="600px" cellpadding="0" cellspacing="0" border="0">
<tr>
<td width="50px" align="center"/>
<td align="center" width="500px">
<div id="image_holder" style="width: 500px; height: 450px;
background-color:#666666;">
<input type="image" id="image" name="image"/>
</div>
</td>
<td width="50px" align="center"/>
</tr>
<tr>
<td/>
<td>
<input type="text" id="caption_holder" name="caption_holder"
style="width:500px;"/>
</td>
<td/>
</tr>
</table>
</td>
<td width="300px" align="center">
<table width="300px" cellpadding="0" cellspacing="0" border="0" align="top">
<tr>
<td align="center" width="100px">
<form method="post" action="photo_delete.php">
<input type="submit" name="delete" id="delete" value="Delete"
class="button" style="width: 100px;"/>
<input type="hidden" name="t_imageid" id="t_imageid"/>
<input type="hidden" name="t_imagepath" id="t_imagepath"/>
<input type="hidden" name="t_albumid" id="t_albumid"/>
</form>
</td>
<td align="center" width="100px">
<form>
<input type="button" name="edit" id="edit" value="Edit" class="button"
style="width: 100px;"
onclick="document.getElementById('pic_caption').style.visibility = 'visible';
document.getElementById('enter_caption').style.visibility = 'visible';"/>
</form>
</td>
<td align="center" width="100px">
<form method="post" action="">
<input type="submit" name="cover_pic" id="cover_pic" value="Set CoverPic" class="button"
style="width: 100px;"/>
<input type="hidden" name="t_albumname" id="t_albumname"/>
<input type="hidden" name="t_imagename" id="t_imagename"/>
</form>
</td>
</tr>
<tr>
<td height="100px" colspan="3">
<form method="post" action="photo_edit.php">
<input type="text" name="pic_caption" id="pic_caption" style="visibility:hidden;"/>
<input type="submit" name="enter_caption" id="enter_caption" style="visibility:hidden;"/>
<input type="hidden" name="t_picid" id="t_picid"/>
<input type="hidden" name="t_albid" id="t_albid"/>
</form>
</td>
</tr>
<tr>
<td height="300px" colspan="3">
<div id="" style="overflow-y:scroll; height:300px;">
<table width="282px" cellpadding="0" cellspacing="0" border="0"
align="center">
<tr>
<td>
<?php
echo $_SESSION[pic_id];?>
<div id="message_report">
</div>
</td>
</tr>
<tr>
<td>
<form method="post" action=
"javascript:get(document.getElementById('pic_comment_form'));" name=
"pic_comment_form" id="pic_comment_form">
<table width="280px" cellpadding="0" cellspacing="0" border="0"
align="center" >
<tr>
<td width="100px">
<input type="text" name="comment" id="comment" style="width:100px"/>
</td>
<td width="80px"/>
<td width="100px">
<input type="submit" name="comment_enter" id="comment_enter" style="width:100px"/>
</td>
<tr>
<td>
<input type="hidden" name="albumid" id="albumid"/></td>
<td>
<input type="hidden" name="imageid" id="imageid"/></td>
<td>
<input type="hidden" name="user" id="user"
value="<?php echo $_SESSION[logged_user];?>"/></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<div id="fade" class="black_overlay">
</div>
</body>
</html>
The code
<a href="javascript:void(0)"
onClick="showpic('<?php echo $row[pic_path];?>','<?php echo $row[pic_id];?>',
'<?php echo $row[album_id];?>','<?php echo $row[pic_caption];?>')";>
<img src="<?php echo $row[pic_path];?>"/></a>
pass the album_id, pic_id to the javascript functions which is then passed to the objects in the lightbox such as id="t_albumid", id="t_imageid"
I need to write a SQL query in the lightbox like this:
SELECT * FROM candidate_pics_comment WHERE pic_id='$VariableHavingPicId
but for this I must use the variable in the JavaScript image_id and pass it again to the lightbox div in the JavaScript. The code:
document.getElementById('t_imageid').value = image_id;
assigns the object, the value as image_id, but how can we assign the image_id to a php variable so that I can use it in the sql query?
I mean in the JavaScript can it be something like this:
function showpic(image_path,image_id,album_id,pic_caption)
{
<?php
$var = //assign the value image_id;?>
// rest of the code here;
}
I know it's not possible, but is there some trick to achieve this?
Moreover as I was experimenting, I did something like this:
function showpic(image_path,image_id,album_id,pic_caption)
{
<?php
$_SESSION[pic_id]="hmmm";?>
//rest codes;
}
Then when I used echo $_SESSION[pic_id] in php script, it displayed hmmm
So I was thinking can it be done something like this?
function showpic(image_path,image_id,album_id,pic_caption)
{
<?php
$_SESSION[pic_id]=//assign image_id to session variable;?>
//rest codes;
}
Besides I used AJAX application, but I saw that AJAX returns the response to the objects innerHTML, how ca it be stored in a php variable?
Update
My question is that, I am passing values from this
<a href="javascript:void(0)"
onClick="showpic('<?php echo $row[pic_path];?>','<?php echo $row[pic_id];?>',
'<?php echo $row[album_id];?>','<?php echo $row[pic_caption];?>')";>
<img src="<?php echo $row[pic_path];?>"/></a>
to the JavaScript fiunction
function showpic(image_path,image_id,album_id,pic_caption)
Now how can I assign image_id to a PHP variable which I can use in the div AlbumDiv which is used for the lightbox. If that not possible how can a session variable be assign the value of image_id, is it possible inside the JavaScript function?
ajax is the only option to get something from JavaScript to PHP.
If you are using jQuery then take a look at the docs here
Otherwise I recommend you use a Library like jQuery or jQuery itself

Categories