This question already has an answer here:
PHP Multiple Update function not updating and gets the 1st row blank
(1 answer)
Closed 9 years ago.
First of all, I use PHP Dreamweaver to make a program with phpMyAdmin Database. This program gets my records in a 1st row blank when I submit the button, and the remaining records in a rows are still there which is 2nd row and below, but the records in a 1st row is still on the database, why should it happen? any idea for me to solve this program, it also won't update my record. this makes me sick, i need help please! tnx...
<?php require_once('Connections/tlsc_conn.php');
mysql_select_db($database_tlsc_conn, $tlsc_conn);
$query_Recordset1 = "SELECT * FROM tbl_name";
$Recordset1 = mysql_query($query_Recordset1, $tlsc_conn) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
if(isset($_POST['submit'])) {
$count = count($_POST['id']);
$submit = $_GET['id'];
for($i=0;$i<$count;$i++){
$sql1="UPDATE $tbl_name SET name='$name[$i]', lastname='$lastname[$i]', email='$email[$i]' WHERE id='$id[$i]'";
$row_Recordset1=mysql_query($sql1);
}
if($row_Recordset1){
header("location:lulu.php");
exit;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form name="form2" method="post" action="">
<table width="634" border="1">
<tr>
<td>id</td>
<td>name</td>
<td>lastname</td>
<td>email</td>
</tr>
<?php do { ?>
<tr>
<td><?php $id[]=$row_Recordset1['id']; ?><?php echo $row_Recordset1['id']; ?>
<input name="id[]" type="hidden" value="<?php echo $row_Recordset1['id']; ?>" />
</td>
<td>
<input name="name[]" type="text" value="<?php echo $row_Recordset1['name']; ?>">
</td>
<td>
<input name="lastname[]" type="text" value="<?php echo $row_Recordset1['lastname']; ?>">
</td>
<td>
<input name="email[]" type="text" value="<?php echo $row_Recordset1['email']; ?>"> </td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
<p>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
<p>
</body>
</html>
Change your looping to while instead of do-while. Check your generated html then and print_r($_POST) after the form submit to see the structure of your post array. Use mysqli or PDO as mysql is now deprecated.
Edit:
<?php while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)) { ?>
<tr>
<td><?php $id[]=$row_Recordset1['id']; ?><?php echo $row_Recordset1['id']; ?>
<input name="id[]" type="hidden" value="<?php echo $row_Recordset1['id']; ?>" />
</td>
<td>
<input name="name[]" type="text" value="<?php echo $row_Recordset1['name']; ?>"> </td>
<td>
<input name="lastname[]" type="text" value="<?php echo $row_Recordset1['lastname']; ?>">
</td>
<td>
<input name="email[]" type="text" value="<?php echo $row_Recordset1['email']; ?>">
</td>
</tr>
<?php } ?>
first remove this from line 5 :
$row_Recordset1 = mysql_fetch_assoc($Recordset1); // I think this not valid it should be only in loop
and try by while loop as:
<?php while($row_Recordset1 = mysql_fetch_assoc($Recordset1))
{
?>
<tr>
<td><?php $id[]=$row_Recordset1['id']; ?><?php echo $row_Recordset1['id']; ?>
<input name="id[]" type="hidden" value="<?php echo $row_Recordset1['id']; ?>" />
</td>
<td>
<input name="name[]" type="text" value="<?php echo $row_Recordset1['name']; ?>">
</td>
<td>
<input name="lastname[]" type="text" value="<?php echo $row_Recordset1['lastname']; ?>">
</td>
<td>
<input name="email[]" type="text" value="<?php echo $row_Recordset1['email']; ?>"> </td>
</tr>
<?php } ?>
</table>
<p>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
<p>
</body>
Related
One of my html forms is not POSTing data, the submit button respect the action attribute and redirect the page as spected BUT $_POST[] is empty for some reason. could sonebody help me find the issue here?
I already tried using html method POST and even ajax to process the form and submit the data, and theres no case. $_POST[] will allways stay empty (this is the only page that does that, I hava an exact copy of the form but without the GET method and it works fine...
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="nocep-rules.css" rel="stylesheet" type="text/css">
<?php
session_start();
$link = mysqli_connect("localhost", "xxxx", "xxxx", "xxxxxx");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>
<?php
if(!isset($_SESSION['logged'])){
header("location: /bubale/login.php");
}
?>
</head>
<body>
<div class="header">
<img class="logo" src="/bubale/img/logo1.png" alt="" />
<img class="foro-title" src="/bubale/img/loja_title.png" alt="" />
</div>
<div class="menu">
<nav class="site-nav">
<ul>
<li>HOME</li>
<li>LOJA</li>
<li>FORUM</li>
<li>CHECKOUT</li>
<li>SAIR</li>
</ul>
</nav>
</div>
<div class="info">
<table width="100%">
<tr>
<td width="20%">
<h2 class="text">USUÁRIO:</h2>
<?php echo $_SESSION['logged']; ?>
<h2 class="text2">PONTOS:</h2>
<?php
$sql5= "Select points from profile where user = '".$_SESSION['logged']."'";
$result5 = mysqli_query($link, $sql5);
if(mysqli_num_rows($result5)==1){
while($row5 = mysqli_fetch_assoc($result5)) {
echo $row5['points'];
}
}
?>
</td>
<td width="30%">
<?php
$sql2= "select * from bolsinhas where user = '".$_SESSION['logged']."' && rev_id = '0'";
$result2 = mysqli_query($link, $sql2);
$count = mysqli_num_rows($result2);
?>
<h2 class="text">BOLSINHAS:</h2>
<?php echo $count; ?>
<h2 class="text2">PRODUTOS:</h2>
<?php
$sql3= "select * from fullsize where user = '".$_SESSION['logged']."' && rev_id = '0'";
$result3 = mysqli_query($link, $sql3);
$count3 = mysqli_num_rows($result3);
echo $count3;
?>
</td>
<td>
<?php
if(!empty($_SESSION["shopping_cart"])) {
$cart_count = count(array_keys($_SESSION["shopping_cart"]));
}else{$cart_count = "0";}
?>
<img id="icon1" src="img/basket.png" title="Compras" ><span id = "count_wrapper"><a id="count">
<?php echo $cart_count; ?></a></span>
<img class="icon" src="img/sale.png" title="Promoção" alt="" >
<img class="icon" src="img/points.png" title="Fidelidade Buba'le" alt="" >
</td>
</table>
</div>
<h1 class="frete">FRETE:</h1>
<div class="wrapper_frete">
<?php
$rua1 = $_GET['1'];
$numero1 = $_GET['2'];
$apt1 = $_GET['3'];
$cep1 = $_GET['4'];
?>
<table id="forma_frete" width="100%">
<tbody>
<th>DADOS PESSOAIS</th>
<th>OOPS!!!</th>
<tr>
<form id="modform" method="POST" action="nocep2.php">
<td width="40%" class="td2">
<table id="nested" width="100%">
<tr>
<td id="td-nested-left" width="30%">
<label class="label-form" for="user">USUÁRIO:</label><br>
<label class="label-form" for="rua">RUA:</label><br>
<label class="label-form" for="numero">NÚMERO:</label><br>
<label class="label-form" for="apt">APT:</label><br>
<label class="label-form" for="cep">CEP:</label><br>
</td>
<td id="td-nested-right">
<input name="user" form="buttons" type="text" maxlenght="20" disabled value="<?php echo $_SESSION['logged']; ?>" /><br>
<input name="rua" form="buttons" type="text" value="<?php echo $rua1; ?>" /><br>
<input name="numero" form="buttons" type="text" value="<?php echo $numero1;?>" /><br>
<input name="apt" form="buttons" type="text" value="<?php echo $apt1; ?>" /><br>
<input name="cep" form="buttons" type="text" value="<?php echo $cep1;?>" />
</td>
</tr>
</table>
<input type="submit" class="refresh" value="ATUALIZAR">
</td>
</form>
<td id="td_mensage">
<h3 id="mensage">-irrelevant text-. </h3>
</td>
</tr>
<tr>
<td id="td_ob" colspan="2">
<h3 id="comentario">OBSERVAÇAÕ:</h3>
<textarea form="buttons" name="observacion" id="text_ob" placeholder="Deixe aqui sua observaçaõ..."></textarea>
</td>
</tr>
</tbody>
</table>
</div>
<h1 class="frete">SUA COMPRA:</h1>
<?php
if(isset($_SESSION["shopping_cart"])){
$total_price = 0;
?>
<?php } ?>
<table class="table">
<tbody>
<tr>
<th></th>
<th>PRODUTO</th>
<th>QUANTIDADE</th>
<th>VALOR UNITARIO</th>
<th>VALOR TOTAL</th>
</tr>
<?php
foreach ($_SESSION["shopping_cart"] as $product){
?>
<tr>
<td id="imgtd"><img src='<?php echo $product["image"]; ?>' width="90" height="80" /></td>
<td><?php echo $product["name"]; ?><br />
<form method='post' action=''>
<input type='hidden' name='code' value="<?php echo $product["code"]; ?>" />
</form></td>
<td><form method='post' action=''>
<input type='hidden' name='code2' value="<?php echo $product["code"]; ?>" />
<?php echo $product["quantity"];?>
</form></td>
<td><?php echo "R$".$product["price"]; ?></td>
<td><?php echo "R$".$product["price"]*$product["quantity"]; ?></td>
</tr>
<?php
$total_price += ($product["price"]*$product["quantity"]);
}
?>
<tr>
<td class="td_tot" colspan="5" align="right"><strong id="total">TOTAL: <?php echo "R$".$total_price; ?></strong></td>
</tr>
</tbody>
</table>
<form method="post" action="contact-venta.php" id="buttons">
<?php
?>
<input type="hidden" name="compras" value="<?php echo print_r($_SESSION["shopping_cart"], TRUE); ?>"/>
<input type="hidden" name="total" value="<?php echo $total_price; ?>">
<input type="submit" id="submit" value="ENVIAR">
</form>
<?php mysqli_close($link); ?>
</body>
</html>
the issue is with the first submit (the one with the value ACTUALIZAR), the form will go to nocep2.php BUT $_POST[] will remain empty .... I posted the entire page just in case there is an issue I'm not seeing....
Your HTML is rather confused.
Firstly, <form> is not a valid element in <tr>. The browser is rendering them outside the table, and separate from your <input>. Move your form tags inside the <td>, or move them outside the <table>
Eg
<table>
<tr>
<td>
<form method="POST">
<input type="text" name="inputName">
<input type="submit">
</form>
</td>
</tr>
</table>
Or, if you need to spread your form over several cells:
<form method="POST">
<table>
<tr>
<td>
<input type="text" name="inputName1">
</td>
<td>
<input type="text" name="inputName2">
</td>
<td>
<input type="submit">
</td>
</tr>
</table>
</form>
Secondly, you're creating a number of <input> elements of type "hidden", each with their own form tags, so they are each part of a different form. Reorder your code so that the hidden elements appear within the same <form> as the rest of your <input>
i am trying to retrieve values from an if statement, that is getting the values from a database here is the code i have;
<!DOCTYPE html>
<?PHP
include('session.php');
include('recipedbconfig.php');
if(isset($_POST['submit'])) {
$postrcpnmesrch = $_POST['rcpnmesrch'];
if($_SERVER["REQUEST_METHOD"] == "POST") {
$sql = "SELECT * FROM recipe_table WHERE recipe_name = '".$postrcpnmesrch."'";
$result = mysqli_query($db, $sql);
$row = mysqli_fetch_assoc($result);
}
}
?>
<html>
<head>
<title>Fresh Veg Admin, Edit Recipes</title>
<link rel=stylesheet type="text/css" href="/FVRC/FVRC.css">
</head>
<body>
<h1>Fresh Veg Admin, Edit Recipes</h1>
<p>User:<b> <?PHP echo $login_session; ?></b>, logged in.</p>
<form action="/FVRC/addeditrecipes.php">
<button type="submit">Back</button>
</form>
<br/>
<hr/>
<table>
<form method="POST">
<tr>
<td>
<input type="text" placeholder="Search for recipes" name="rcpnmesrch">
</td>
<td>
<button type="submit">Search</button>
</td>
</tr>
</form>
</table>
<br/>
<hr/>
<form action="editrecipecheck.php" method="POST">
<table cellspacing=0 cellpadding=5>
<tr>
<td>
</td>
<td>
<label>Recipe Name:</label>
</td>
<td>
<input type="text" placeholder="Recipe Name" name="rcpnme" value="<?php echo $row['recipe_name']; ?>" required>
</td>
</tr>
<tr>
<td>
<label>Ingredient One:</label>
</td>
<td>
<input type="text" placeholder="Ingredient" name="ingnme1" required>
</td>
i need the values that the $row array contains, so it can be referenced to in the inputs:
<td>
<input type="text" placeholder="Recipe Name" name="rcpnme" value="<?php echo $row['recipe_name']; ?>" required>
</td>
but as it is local i cant figure it out, i may just be having a brain fart moment, any help would be appreciated
all i get as an error message is this:
<br />
<b>Notice</b>: Undefined index: recipe_name in <b>E:\Program Files\xampp\htdocs\FVRC\editrecipe.php</b> on line <b>52</b>
<br />
I'm trying to create a dynamic table using PHP but what I get is in each column of my table the array goes to the end and the next column is staring from the 0 to the end.
I tried to put if conditions and do some tricks to fix it but I couldn't.
this is the result I get for below code :
<?php
$query_formsearch = mysqli_query($con,"SELECT * FROM `forms` ");
$query_usr_prvlg = mysqli_query($con,"SELECT * FROM `profile` WHERE `id`='{$_SESSION["usr_id"]}' ");
$usr_access=mysqli_fetch_assoc($query_usr_prvlg);
$count=mysqli_num_rows($query_formsearch);
$i=0;
?>
<form action="" method="POST">
<table>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
<?php while($forms_row = mysqli_fetch_assoc($query_formsearch))
{
$f_num="form_".$forms_row['num'];
?>
<tr>
<td>
<?php if(($usr_access[$f_num]=="1-1") || ($usr_access[$f_num]=="1-0")) ?>
<input class='styled-input_m' type="hidden" id="form-name" name ="form-name" value="<?php echo $forms_row['num']; ?>" />
<input class='styled-input_m' type="submit" id="sbmt-btn" name ="sbmt-btn" value="<?php echo $forms_row['name']; ?>" />
</td>
<td>
<?php if(($usr_access[$f_num]=="1-1") || ($usr_access[$f_num]=="1-0")) ?>
<input class='styled-input_m' type="hidden" id="form-name" name ="form-name" value="<?php echo $forms_row['num']; ?>" />
<input class='styled-input_m' type="submit" id="sbmt-btn" name ="sbmt-btn" value="<?php echo $forms_row['name']; ?>" />
</td>
<td>
<?php if(($usr_access[$f_num]=="1-1") || ($usr_access[$f_num]=="1-0")) ?>
<input class='styled-input_m' type="hidden" id="form-name" name ="form-name" value="<?php echo $forms_row['num']; ?>" />
<input class='styled-input_m' type="submit" id="sbmt-btn" name ="sbmt-btn" value="<?php echo $forms_row['name']; ?>" />
</td>
</tr>
<?php
}
?>
</table>
</form>
I found this code online and now im trying to make it work. I get an error when I press Edit from the first page.
Notice: Undefined variable: id in edit_form2.php on line 19
I'm not sure why this variable isn't included in the code, where should it go? or does it have something to do with my table?
Database = album
Table = data_employees
1. id primaray key & a_i
2. name
3. address
When I press the edit button the next page should load the existing data from the database. Now it just turns up empty + the error code I wrote. What should I do?
edit.php
<td align="center">DATA</td>
</tr>
<tr>
<td>
<table border="1">
<?php
include"dbinc.php";//database connection
$order = "SELECT * FROM data_employees";
$result = mysql_query($order);
while ($row=mysql_fetch_array($result)){
echo ("<tr><td>$row[name]</td>");
echo ("<td>$row[employees_number]</td>");
echo ("<td>$row[address]</td>");
echo ("<td>Edit</td></tr>");
}
?>
</table>
edit_form2.php
<?php
include "dbinc.php";//database connection
$order = "SELECT * FROM data_employees
where employees_number='$id'";
$result = mysql_query($order);
$row = mysql_fetch_array($result);
?>
<form method="post" name=form action="edit_data2.php">
<input type="hidden" name="id" value="<?php echo "$row[employees_number]"?>">
<tr>
<td>Name</td>
<td>
<input type="text" name="name"
size="20" value="<?php echo "$row[name]"?>">
</td>
</tr>
<tr>
<td>Address</td>
<td>
<input type="text" name="address" size="40"
value="<?php echo "$row[address]"?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
edit_data2.php
<?php
//edit_data.php
include "dbinc.php";
$name = $_POST["name"];
$address = $_POST["address"];
$id = $_POST["id"];
$order = "UPDATE data_employees
SET name='$name',
address='$address'
WHERE
employees_number='$id'";
mysql_query($order);
header("location:edit.php");
?>
$id variable must be declared before use; like-
$id = mysql_real_escape_string($_GET['id']);
This line is wrong
<input type="hidden" name="id" value="<?php echo "$row[employees_number]"?>">
try
<input type="hidden" name="id" value="<?php echo $row['employees_number']?>">
So quite likely as the input field does not get built properly the browser is ignoring that field completely.
edit.php
replace
echo ("<tr><td>$row[name]</td>");
echo ("<td>$row[employees_number]</td>");
echo ("<td>$row[address]</td>");
echo ("<td>Edit</td></tr>");
with
echo "<tr><td>".$row['name']."</td>";
echo "<td>".$row['employees_number']."</td>";
echo "<td>".$row['address']."</td>";
echo "<td>Edit</td></tr>";
edit_form.php
<form method="post" name=form action="edit_data2.php">
<input type="hidden" name="id" value="<?php echo "$row[employees_number]"?>">
<tr>
<td>Name</td>
<td>
<input type="text" name="name"
size="20" value="<?php echo "$row[name]"?>">
</td>
</tr>
<tr>
<td>Address</td>
<td>
<input type="text" name="address" size="40"
value="<?php echo "$row[address]"?>">
</td>
</tr>
<tr>
should be
<?php
$id = mysql_real_escape_string($_GET['id']);include "dbinc.php";//database connection
$order = "SELECT * FROM data_employees
where employees_number='$id'";
$result = mysql_query($order);
$row = mysql_fetch_array($result);
?>
<form method="post" name=form action="edit_data2.php">
<input type="hidden" name="id" value="<?php echo $row['employees_number']; ?>">
<tr>
<td>Name</td>
<td>
<input type="text" name="name"
size="20" value="<?php echo $row['name']; ?>">
</td>
</tr>
<tr>
<td>Address</td>
<td>
<input type="text" name="address" size="40"
value="<?php echo $row['address']; ?>">
</td>
</tr>
<tr>
and like Mukesh Soni said, you need to grab te id from the url
I am trying to develop an application in which "When I select option from dropdown the below fields should fill automatically(One is text field and other is TinyMCE Esitor)".
Below is the code I tried.
<?php
defined('_JEXEC') or die('Restricted access');
?>
<script type="text/javascript">
function showTemplate()
{
document.getElementById("jform_page_title").value = document.getElementById("jform").value;
alert("clicked");
}
</script>
<form action="" method="post" name="adminForm" id="adminForm">
<table class="admintable">
<tbody>
<tr>
<td>
<label for="jformid"><?php echo JText::_('JTAG_LEGAL_PAGES_CHOOSE_TEMPLATE');?>
</label>
</td>
<td>
<select id="jform" onchange="showTemplate()">
<?php foreach($this->templates as $template): //print_r($template);exit; ?>
<option value="<?php echo $template->id; ?>"> <?php echo $template->template_title; ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<tr></tr>
<tr>
<td>
<label for="jform_page_title"><?php echo JText::_('JTAG_LEGAL_PAGES_TEMPLATE_TITLE'); ?></label>
</td>
<td>
<input type="text" size="30" class="inputbox required" id="jform_page_title" value="<?php echo $this->template->template_title; ?>" name="jform[page_title]" />
</td>
</tr>
<tr></tr>
<tr>
<td>
<label for="jform_description"><?php echo JText::_('JTAG_LEGAL_PAGES_TEMPLATE_DESCRIPTION'); ?></label>
</td>
<td>
<?php
$editor =& JFactory::getEditor();
echo $editor->display('jform[description]', htmlspecialchars($this->lpsettingdata->description, ENT_QUOTES),'550','300','60','20',array('pagebreak','readmore'));
?>
</td>
</tr>
</tbody>
</table>
<input type="hidden" value="<?php echo $this->lpsettingdata->id; ?>" name="jform[id]" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="controller" value="jtaglegalpages" />
</form>
Please let me know how to give id to the editor.