Checkbox update ONLY - php

I have an issue with my code because when I press the update button I want it to display on the redirected page only the field that are checked in the checkbox.
I will post the code from the first page , then the code from the second page , where the update code is and some pictures of the website page .
First page :
firstpage
code firstpage:
<?php
if (isset ($_POST['search'])){
$ValueToSearch=$_POST['valueToSearch'];
$query1="SELECT * FROM `customer` AS cust,`type` AS type,`valability` AS val WHERE cust.Customer_ID=val.Customer_ID AND val.Insurance_ID=type.Insurance_ID AND CONCAT(cust.Customer_ID,cust.Employee_ID,cust.Name_c,cust.Surname_c,cust.brth,cust.phone,cust.adress,cust.email,type.series,type.type,type.name_i,type.Company_N,type.value,val.nr,val.entry_d,val.exp_d) LIKE '%".$ValueToSearch."%'";
echo "<!-- ". $query1 ." -->";
$search_result=filterTable($query1);
}else{
$query1 = "SELECT * FROM `customer`,`type`,`valability` WHERE customer.Customer_ID=valability.Customer_ID AND valability.Insurance_ID=type.Insurance_ID";
$search_result=filterTable($query1);
}
function filterTable($query1){
$connect=mysqli_connect("localhost" ,"root", "","asig");
$filter_Result=mysqli_query($connect,$query1);
return $filter_Result;
}
?>
<?php
if (isset($_POST['update'])) {
//$connect=mysqli_connect("localhost" ,"root", "","asig");
//$update_id=$_POST['checkbox'];
Header("Location: displayupdate.php");
//die();
//exit;
}else{
echo"can't redirect";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Display Table </title>
<style>
table {
width: 100%;
background-color: #f1f1c1;
}
table ,tr, th, td
{
border:1px solid black;
border-collapse: collapse;
}
th,td{
padding: 5px;
}
</style>
</head>
<!--Beginning of CSS page-->
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<body>
<div class="w3-display-container" style="margin-bottom:50px">
<img src="image2/Insurance-2.jpg" style="width:100%">
<div class="w3-display-bottomleft w3-container w3-amber w3-hover-orange w3-hide-small"
style="bottom:10%;opacity:0.7;width:70%">
<h2><b>4 Good Reasons<br>For being a SafeAsig client</b></h2>
</div>
</div>
<!--Buton pentru display timp-->
<button type="button"
onclick="document.getElementById('demo').innerHTML = Date()">
Click me to display Date and Time.</button>
<p id="demo"></p>
<form action="display.php" method="post">
<input type="text" name="valueToSearch" placeholder="ValueToSearch">
<input type="submit" name="search" value="Filter">
<input type="submit" name="delete" value="Delete">
<input type="submit" name="update" value="Update">
<hr>
<h1 style="text-align:center;"> Date Clienti</h1>
<table>
<tr>
<th></th>
<th>id</th>
<th>Nume</th>
<th>Prenume</th>
<th>DataNastere</th>
<th>telefon</th>
<th>Adresa</th>
<th>email</th>
<th>serie</th>
<th>type</th>
<th>numeAsig</th>
<th>Companie</th>
<th>Valoare></th>
<th>Numar</th>
<th>DataIntrare</th>
<th>DataExpirare</th>
</tr>
<?php while ($row=mysqli_fetch_array($search_result)):?>
<tr>
<td><input type="checkbox" name="checkbox[]" value="<?php echo $row['Customer_ID']; ?>"/></td>
<td><?php echo $row['Customer_ID']; ?></td>
<td><?php echo $row['Name_c']; ?></td>
<td><?php echo $row['Surname_c']; ?></td>
<td><?php echo $row['brth']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['adress']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['series']; ?></td>
<td><?php echo $row['type']; ?></td>
<td><?php echo $row['name_i']; ?></td>
<td><?php echo $row['Company_N']; ?></td>
<td><?php echo $row['value']; ?></td>
<td><?php echo $row['nr']; ?></td>
<td><?php echo $row['entry_d']; ?></td>
<td><?php echo $row['exp_d']; ?></td>
</tr>
<?php endwhile;?>
</table>
</form>
<!--Link pentru inserari-->
Inserare Clienti <br><br>
Inserare angajati <br><br>
<div class="w3-row w3-container" style="margin:50px 0">
<div class="w3-half w3-container">
<div class="w3-topbar w3-border-amber">
<img src="image2/profesionalism.jpg" style="width:100%">
<h2>Profesionalism and dedication</h2>
<p>Only for our clients , the best services provided.</p>
</div>
</div>
<div class="w3-half w3-container">
<div class="w3-topbar w3-border-amber">
<img src="image2/protection.jpg" style="width:100%">
<h2>100% protection</h2>
<p>Up to 100% protection with our wide varaity insurances.</p>
</div>
</div>
</div>
<div class="w3-row w3-container" style="margin:50px 0">
<div class="w3-half w3-container">
<div class="w3-topbar w3-border-orange">
<img src="image2/stability.jpg" style="width:100%">
<h2>Stability all times</h2>
<p>Wherever you travel our services provide stability.</p>
</div>
</div>
<div class="w3-half w3-container">
<div class="w3-topbar w3-border-orange">
<img src="image2/support.jpg" style="width:100%">
<h2>For good and worst</h2>
<p>Don't wait until it is to late.</p>
</div>
</div>
</div>
<?php
if (isset($_POST['delete'])) {
$connect=mysqli_connect("localhost" ,"root", "","asig");
$del_id = $_POST['checkbox'];
while(list($key, $val) = #each($del_id)){
mysqli_query($connect, "delete from customer where customer_id = $val");
}
}
?>
</body>
</html>
picture second page:
secondpage
code second page:
<html>
<?php
//Connect to database
$connect=mysqli_connect("localhost" ,"root", "","asig");
//Select database
mysqli_select_db($connect,'asig');
//Select Query
$sql = "SELECT * FROM `customer`,`type`,`valability` WHERE customer.Customer_ID=valability.Customer_ID AND valability.Insurance_ID=type.Insurance_ID";
//Execte query
$records=mysqli_query($connect,$sql);
?>
<head>
</head>
<body>
<table>
<tr>
<th>id</th>
<th>Nume</th>
<th>Prenume</th>
<th>DataNastere</th>
<th>telefon</th>
<th>Adresa</th>
<th>email</th>
<th>serie</th>
<th>type</th>
<th>numeAsig</th>
<th>Companie</th>
<th>Valoare></th>
<th>Numar</th>
<th>DataIntrare</th>
<th>DataExpirare</th>
</tr>
<?php
while($row=mysqli_fetch_array($records)){
echo "<tr><form action=update.php method=post>";
// echo "<td><input type=checkbox name=checkbox[] value='".$row['Customer_ID']."'></td>";
echo "<td><input type=hidden name =id value='".$row['Customer_ID']."' ></td>";
echo "<td><input type=text name= name value='".$row['Name_c']."'> </td>";
echo "<td><input type=text name=prenume value='".$row['Surname_c']."'> </td>";
echo "<td><input type=text name=brth value='".$row['brth']."'> </td>";
echo "<td><input type=text name=phone value='".$row['phone']."'> </td>";
echo "<td><input type=text name=adress value='".$row['adress']."'> </td>";
echo "<td><input type=text name=email value='".$row['email']."'> </td>";
echo "<td><input type=text name=serie value='".$row['series']."'> </td>";
echo "<td><input type=text name=type value='".$row['type']."'> </td>";
echo "<td><input type=text name=namei value='".$row['name_i']."'> </td>";
echo "<td><input type=text name=compn value='".$row['Company_N']."'> </td>";
echo "<td><input type=text name=value value='".$row['value']."'> </td>";
echo "<td><input type=text name=nr value='".$row['nr']."'> </td>";
echo "<td><input type=text name=entryin value='".$row['entry_d']."'> </td>";
echo "<td><input type=text name=entryout value='".$row['exp_d']."'> </td>";
echo "<td><input type=submit>";
echo "</form></tr>";
}
?>
</table>
</body>
</html>

Since your action goes to display.php, I will assume that your 'second page' is display.php. If that is the case then you need to retrieve the submitted checked values with $_POST[].
In your example the check boxes are named 'checkbox' so your POST should look like below...
<?php
$customer_id_arr = $_POST['checkbox'];
$customer_id_str = implode(",", $customer_id_arr);
//Connect to database
$connect=mysqli_connect("localhost" ,"root", "","asig");
//Select database
mysqli_select_db($connect,'asig');
//Select Query
$sql = "SELECT * FROM `customer`,`type`,`valability` WHERE customer.Customer_ID IN(" . $customer_id_str . ") AND valability.Insurance_ID=type.Insurance_ID";
//Execte query
$records=mysqli_query($connect,$sql);
?>
<html>
Your code overall needs a good bit of work/cleanup.
Hope that helps.

Related

Adding an upload feature blocked my multiple deletion

Multiple deletion used to work just fine before adding the upload page and condition
am sure the problem comes from a in one of the echo's
I didn't manage to figure out a way to get them both working at the same time.
You can check by yourself here: agencedevoyages.tk
Enter the
email: abc#gmail.com
Password: 2
You'll notice that upload does work and multiple deletion doesn't work anymore
Code:
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Accueil</title>
</head>
<body>
<?php
if(isset($_SESSION['email']) AND isset($_SESSION['password']))
{
echo "<b>Welcome ".$_SESSION['prenom']." ".$_SESSION['nom']." !";
echo "<div align='right'><a align='right' href='deconnect.php'><button type='button'>Se déconnecter</button></a></div>";
?>
<table border="1" align="center">
<caption><b><h1>La liste des étudiants</h1></caption>
<tr><td align="center"><b>Photo</td>
<td align="center"><b>Matricule</td>
<td align="center"><b>Nom</td>
<td align="center"><b>Prénom</td>
<td align="center"><b>Adress</td>
<td align="center"><b>Birthday</td>
<td align="center"><b>E-mail</td>
<td align="center" colspan="2"><b>Option</td>
<td align="center"><b>Multiple Deletion</td>
<td align="center"><b>Upload</td></tr>
<?php
include 'connect.php';
$sql = "SELECT * FROM etudiant";
$res = mysqli_query($connect, $sql);
if (mysqli_num_rows($res)>0){
while($row = mysqli_fetch_assoc($res))
{
echo "<tr><td>";
?>
<?php
if(empty($row['photo']))
{
?>
<img src='photos/image.jpg' width="100" height="100">
<?php
}
else
{
?>
<img src='photos/<?php echo $row['photo'] ?>' width="100" height="100">
<?php
}
echo "</td>";
echo "<td align='center'>".$row['matricule']."</td>";
echo "<td align='center'>".$row['nom']."</td>";
echo "<td align='center'>".$row['prenom']."</td>";
echo "<td align='center'>".$row['adresse']."</td>";
echo "<td align='center'>".$row['date_naissance']."</td>";
echo "<td align='center'>".$row['email']."</td>";
if($_SESSION['email']==$row['email'] OR $_SESSION['email']=='abc#gmail.com')
{
echo "<td><a href='modif.php?id=".$row['id']."'><button type='button'>Edit</button></a></td>";
echo "<td><a href='supp.php?id=".$row['id']."'><button type='button'>Delete</button></a></td>";
echo "<td align='center'><form method='post' action='supp3.php'><input type='checkbox' name='sup[]' value='".$row['id']."'></form></td>";
echo "<td align='center'><form method='post' action='upload.php' enctype='multipart/form-data'><input type='file' name='fichier'><input type='submit' value='Confirm'></td><input type='hidden' name='id' value='".$row['id']."'></td></tr>";
}
}
echo "<tr><td colspan='9'></td><td colspan='1' align='center'><button>Multiple Deletion</button></td></tr></form>";
}
else
{
echo "<tr><td colspan ='10' align='center'>No users.</td></tr>";
$sql = "TRUNCATE etudiant";
$res = mysqli_query($connect, $sql);
}
?>
</table>
<p align="center"><button>Add a user</button></p>
<?php
}
else
{
echo "<div align='center'><b><h1>Not allowed.";
echo "<br><a href='index.php'><button type='button'>Back</button></a></div>";
}
?>
</body>
</html>

getting the id from a table result

I have a table of exams where I have an edit part. When I select one of exam to edit it doesn't take the exam's id. And I can't figure it out why. Here is my code :
This is my principal page content where is showing the table
<div class="view">
<table class="tabel">
<tr class="tepprand">
<td>Data</td>
<td>An</td>
<td>Materia</td>
<td>Profesor</td>
<td>Asistent</td>
<td>Sala</td>
<td>Tip Examen</td>
</tr>
<?php
$sqll = "SELECT r.id AS id , r.data AS data , ra.an AS an, rm.numemat AS numemat, rp1.numep AS numep1 , rp1.prenumep AS prenumep1, rp2.numep AS numep2, rp2.prenumep AS prenumep2, rs.salaa AS salaa , re.tip AS tip
FROM examen_programat AS r
LEFT JOIN an ra ON (r.id_ann=ra.id_an)
LEFT JOIN materii rm ON (r.id_mate=rm.id_mat)
LEFT JOIN profesor rp1 ON (r.id_prof1=rp1.id_prof)
LEFT JOIN profesor rp2 ON (r.id_prof2=rp2.id_prof)
LEFT JOIN sala rs ON (r.id_sala=rs.id_s)
LEFT JOIN examen re ON (r.id_tipp = re.id_tip)
ORDER by data";
$result = mysqli_query($link,$sqll);
while($rows=mysqli_fetch_array($result,MYSQLI_BOTH)) {
?>
<tr>
<td><?php echo $rows['data']; ?></td>
<td><?php echo $rows['an']; ?></td>
<td><?php echo $rows['numemat']; ?></td>
<td><?php echo $rows['numep1']." ".$rows['prenumep1']; ?></td>
<td><?php echo $rows['numep2']." ".$rows['prenumep2']; ?></td>
<td><?php echo $rows['salaa']; ?></td>
<td><?php echo $rows['tip']; ?></td>
<td> Edit </td>
<td> Stergere </td>
</tr>
<?php
}
?>
</table>
</div>
This is what happens when I press EDIT :
<div class="view">
<?php
$idul=$_GET['id'];
$str = "SELECT * FROM examen_programat WHERE id =$idul ";
$rez = mysqli_query($link,$str);
$row = mysqli_fetch_array($rez);
$an=$row['id_ann'];
$materie=$row['id_mate'];
$profesor=$row['id_prof1'];
$asistent=$row['id_prof2'];
$salaa=$row['id_sala'];
$tip=$row['id_tipp'];
?>
<form name="tabel" method="post" action="updateexamen.php">
<input type="hidden" name="idu" value="<?php echo " $idul" ?>">
<table>
<tr>
<td>Data</td>
<td><input type="date" name="data" value="<?php echo $row['data'] ?>" required="required"/><br></td>
</tr>
<tr>
<td>An</td>
<td>
<?php
$sql_year="SELECT * FROM an";
$rez_year = mysqli_query($link,$sql_year);
echo "<select name=\"year\" >";
while($year=mysqli_fetch_array($rez_year))
{
echo "
<option value=\"".$year['id_an'];
if ($year[
'id_an']==$an)
echo "\" selected=\"selected\">".$year['grupa']."</option>\n";
else
echo "\">".$year['grupa']."</option>\n";
}
echo "</select>";
?><br>
</td>
</tr>
<tr>
<td>Materie</td>
<td>
<?php
$sql_mat="SELECT * FROM materii";
$rez_mat = mysqli_query($link,$sql_mat);
echo "<select name=\"mat\" >";
while($mat=mysqli_fetch_array($rez_mat))
{
echo "
<option value=\"".$mat['id_mat'];
if ($mat[
'id_mat']==$materie)
echo "\" selected=\"selected\">".$mat['numemat']."</option>\n";
else
echo "\">".$mat['numemat']."</option>\n";
}
echo "</select>";
?><br>
</td>
</tr>
<tr>
<td>Profesor</td>
<td>
<?php
$sql_proff="SELECT * FROM profesor";
$rez_proff = mysqli_query($link,$sql_proff);
echo "<select name=\"proff\" >";
while($proff=mysqli_fetch_array($rez_proff))
{
echo "
<option value=\"".$proff['id_prof'] ;
if($proff[
'id_prof']==$profesor)
echo "\" selected=\"selected\">".$proff['numep']." ".$proff['prenumep']."</option>\n";
else
echo "\">".$proff['numep']." ".$proff['prenumep']."</option>\n";
}
echo "</select>";
?><br>
</td>
</tr>
<tr>
<td>Asistent</td>
<td>
<?php
$sql_profff="SELECT * FROM profesor";
$rez_profff = mysqli_query($link,$sql_profff);
echo "<select name=\"profff\" >";
while($profff=mysqli_fetch_array($rez_profff))
{
echo "
<option value=\"".$profff['id_prof'];
if($profff[
'id_prof']==$asistent)
echo "\" selected=\"selected\">".$profff['numep']." ".$profff['prenumep']."</option>\n";
else
echo "\">".$profff['numep']." ".$profff['prenumep']."</option>\n";
}
echo "</select>";
?><br>
</td>
</tr>
<tr>
<td>Sala</td>
<td>
<?php
$sql_sala="SELECT * FROM sala";
$rez_sala= mysqli_query($link,$sql_sala);
echo "<select name=\"sala\" >";
while($sala=mysqli_fetch_array($rez_sala))
{
echo "
<option value=\"".$sala['id_s'];
if ($sala[
'id_s']==$salaa)
echo "\" selected=\"selected\">".$sala['salaa']."</option>\n";
else
echo "\">".$sala['salaa']."</option>\n";
}
echo "</select>";
?><br>
</td>
</tr>
<tr>
<td>Tip</td>
<td>
<?php
$sql_type="SELECT * FROM examen";
$rez_type= mysqli_query($link,$sql_type);
echo "<select name=\"type\" >";
while($type=mysqli_fetch_array($rez_type))
{
echo "
<option value=\"".$type['id_tip'];
if ($type[
'id_tip']==$tip)
echo "\" selected=\"selected\">".$type['tip']."</option>\n";
else
echo "\">".$type['tip']."</option>\n";
}
echo "</select>";
?><br>
</td>
</tr>
<tr>
<td><input name="submit" type="submit" value="Update"/></td>
<td><input name="reset" type="reset" value="Reset"/></td>
</tr>
</table>
</form>
</div>
And this is the UPDATE:
<?php
include ('conect.php');
$idd=$_POST['idu'];
$a1=$_POST['data'];
$b1=$_POST['year'];
$c1=$_POST['mat'];
$d1=$_POST['proff'];
$e1=$_POST['profff'];
$f1=$_POST['sala'];
$g1=$_POST['type'];
$str = "UPDATE examen_programat
SET data='$a1',
id_ann='$b1',
id_mate='$c1',
id_prof1='$d1',
id_prof2='$e1',
id_sala='$f1',
id_tipp='$g1'
WHERE id='$idd'";
$result=mysqli_query($link,$str);
// if successfully updated.
if($result){
header("Location:tabelex.php");
}
else {
echo "ERROR";
}
?>
And my table of examen_programat it looks like this:
id id_ann id_mate id_prof1 id_prof2 id_sala id_tipp
Short open tag may cause this problem, on your last two td element. Add php after your ?
<td>
Edit
</td>
<td>
Stergere
</td>
From PHP DOC: http://php.net/manual/en/language.basic-syntax.phptags.php
PHP also allows for short open tag <? (which is discouraged since it
is only available if enabled using the short_open_tag php.ini
configuration file directive, or if PHP was configured with the
--enable-short-tags option).

How do I hardcode a dynamic url to an image map?

I am working on a Zip Code search, and have successfully built the search. Now, I need to have the ability to click on an image map of a map, and hardcode an href to a dynamic URL
So for example, the search works well displaying contact information for a zip code in PA. But if I have an image map, I'd like the ability for it to display the same contact information if I click the image map of PA on the map image.
Here is the code I'm using to do the search, and at the bottom is the code I need help with for the image map.
<form action="search6.php" method="post">
<p><span class="orange16">Zip Code:</span>
<input type="text" name="search_name"> <input type="submit" value="Search" />
</p>
</form>
<br />
<table width="700" border="0">
<?php
if (isset($_POST['search_name'])) {
$search_name = $_POST['search_name'];
if (!empty($search_name)) {
if (strlen($search_name)>=5) {
$query = "SELECT * FROM `search4` WHERE `ZipCode` LIKE '%".mysql_real_escape_string($search_name)."%'";
$query_run = mysql_query($query);
if (mysql_num_rows($query_run)>=1) {
echo "<table width=700' border='0'>";
echo "<tr>";
echo "<td width='700' valign='top'><table width='100%' border='0'>";
echo "<tr>";
echo "<td><p><strong>Results found: </strong></p>";
while ($query_row = mysql_fetch_assoc($query_run)) {{
echo $query_row['ZipCode'].', ';
echo $query_row['ZipCity'].', ';
echo $query_row['ZipState'].'<br><br>';
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo '<span class="productdescription"><p>Office: </p></span></h2>';
echo $query_row['Office'].'<br>';
echo $query_row['Address1'].'<br>';
if(!empty($query_row['Address2'])) // This will skip if the field if it's empty
echo $query_row['Address2'].'<br>';
echo $query_row['City'].', ';
echo $query_row['State'].' ';
echo $query_row['Zip'].'<br>';
echo '<p><strong>Phone Number: </strong></p>';
echo $query_row['Phone'].'<br>';
echo '<p><strong>Fax Number: </strong></p>';
echo $query_row['Fax'].'<br><br>';
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</td>";
//BeginImage display result
$res=mysql_query("select * from Images");
{
echo "<td width='703' align='right' valign='top'>";?> <img src="<?php echo $query_row["Image"]; ?>"> <?php echo "</td>";
echo "</tr>";
}
//EndImage display result
echo ("<table width='700px' border='0' cellpadding='5' cellspacing='1'>
<tr>
<td width='13%' align='left' bgcolor='#C1DDF4'><p><strong>Service Type:</strong></p></td>
<td width='13%' align='left' bgcolor='#C1DDF4'><p><strong>Name:</strong></p></td>
<td width='13%' align='left' bgcolor='#C1DDF4'><p><strong>Phone:</strong></p></td>
<td width='13%' align='left' bgcolor='#C1DDF4'><p><strong>Email:</strong></p></td>
</tr>");
echo ("
<td align='left'><p><strong>Sales</strong></p></td>
<td align='left'><p>$query_row[SalesName]</p></td>
<td align='left'><p>$query_row[SalesPhone]</p></td>
<td align='left'><p><a href='mailto:$query_row[SalesEmail]'class='admin_links'>$query_row[SalesEmail]</p></a></td>
</tr>");
echo ("
<td align='left'><p><strong>Service</strong></p></td>
<td align='left'><p>$query_row[ServiceName]</p></td>
<td align='left'><p>$query_row[ServicePhone]</p></td>
<td align='left'><p><a href='mailto:$query_row[ServiceEmail]'class='admin_links'>$query_row[ServiceEmail]</p></a></td>
</tr>");
echo ("
<td align='left'><p><strong>Service Coordinator</strong></p></td>
<td align='left'><p>$query_row[ServiceCoorName]</p></td>
<td align='left'><p>$query_row[ServiceCoorPhone]</p></td>
<td align='left'><p><a href='mailto:$query_row[ServiceCoorEmail]'class='admin_links'>$query_row[ServiceCoorEmail]</p></a></td>
</tr>");
echo ("</table>");
}
}
}else{
echo 'No results found.';
}
}else{
echo 'Your search must be a 5-digit zip code.';
}
}
}
?>
</table>
This is the code for the Image map I need help with. I'd like to use the "CustClassID" data row for my value of lets say "23-LA".
<?php
$query = "SELECT * FROM search4 WHERE CustClassID = {$_GET['CustClassID']}";
echo ("<table width='600' border='0'>
<tr>
<td align='center'> <img src='images/greymap.png' border='0' usemap='#Map' />
<map name='Map' id='Map'>
<area shape='rect' coords='42,21,136,98' href='search6.php?CustClassID=23-LA' />
</map></td>
</tr>
</table>
");
?>
Basically, is there another way to get the same results as the search, by clicking on specific areas on the imagemap? Please point me in right direction.

Why does my php code to edit my mySQL table show as blank page?

I have an edit button at the end of a table of mysql records that displays
in a webpage.
When I click the edit button, however, my edit.php just generates a blank page.
I can't figure out why:
<?php
// contact to database
$connect = mysql_connect("localhost", "xxx", "xxx") or die ("Error , check your server connection.");
mysql_select_db("xxx");
?>
<?php
$posted_id = $_POST['ID'];
?>
<!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=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<div class="form">
<?php
$result = mysql_query("SELECT * FROM ff_projections WHERE ID = '$posted_id'") or die ("Error in query");
if ($row = mysql_fetch_array($result)) {
echo "<form method='post' action='update.php'>";
echo "<label for='Player'>Player Name:</label> <input type='text' name='Player' value='" . $row['Player'] . "' />";
echo "<label for='Pass_Yds'>Pass Yds:</label> <input class='short' type='text' name='Pass_Yds' value='" . $row['Pass_Yds'] . "' />";
echo "<label for='Pass_TDs'>Pass TDs:</label> <input class='short' type='text' name='Pass_TDs' value='" . $row['Pass_TDs'] . "' />";
echo "<label for='Int_Thrown'>Int Thrown:</label> <input class='short' type='text' name='Int_Thrown' value='" . $row['Int_Thrown'] . "' />";
echo "<label for='Rush_Yds'>Rush Yds:</label> <input class='short' type='text' name='Rush_Yds' value='" . $row['Rush_Yds'] . "' />";
echo "<label for='Rush_TDs'>Rush TDs:</label> <input class='short' type='text' name='Rush_TDs' value='" . $row['Rush_TDs'] . "' />";
echo "<input type='submit' name='submit' value='Update Player' />";
echo "<input type='hidden' name='ID' value='" . $row['ID'] . "' />";
echo "</form>";
}
?>
</div>
</body>
</html>
I have ID as a hidden field because I don't want it visible or editable.
Here is the page that uses edit.php:
<?php
// contact to database
$connect = mysql_connect("localhost", "xxx", "xxx") or die ("Error , check your server connection.");
mysql_select_db("xxx");
?>
<!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=utf-8" />
<title>Kick Ass Fantasy Football Projections</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".tab_content").hide();
$(".tab_content:first").show();
$("ul.tabs li").click(function() {
$("ul.tabs li").removeClass("active");
$(this).addClass("active");
$(".tab_content").hide();
var activeTab = $(this).attr("rel");
$("#"+activeTab).fadeIn();
});
});
</script>
</head>
<body>
<div>
<FORM METHOD="LINK" ACTION="index.php" style="margin:10px 0px">
<INPUT TYPE="submit" VALUE="Add More Players" style="padding:6px">
</FORM>
</div>
<ul class="tabs">
<li class="active" rel="tab1"> QB</li>
<li rel="tab2"> RB</li>
<li rel="tab3"> WR</li>
<li rel="tab4"> TE</li>
<li rel="tab5"> K</li>
<li rel="tab6"> Def / ST</li>
</ul>
<div class="tab_container">
<div id="tab1" class="tab_content">
<table cellspacing="0" cellpadding="5" border="1" width="560">
<tr style="text-align:center">
<td style="text-align:left ; width:175px">Player Name</td>
<td>Team</td>
<td>Pass Yds</td>
<td>Pass TDs</td>
<td>Int Thrown</td>
<td>Rush Yds</td>
<td>Rush TDs</td>
<td>TFP</td>
<td>Edit Player</td>
</tr>
<?php
$result = mysql_query("SELECT Player, Team, Pass_Yds, Pass_TDs, Int_Thrown, Rush_Yds, Rush_TDs, Total_Fantasy_Pts, ID FROM ff_projections WHERE Position = 'QB' ORDER BY Pass_Yds DESC;");
while($row = mysql_fetch_array($result))
{
echo "<tr style=\"text-align:center\"><td style=\"text-align:left\">{$row['Player']}</td>";
echo "<td>{$row['Team']}</td>";
echo "<td>{$row['Pass_Yds']}</td>";
echo "<td>{$row['Pass_TDs']}</td>";
echo "<td>{$row['Int_Thrown']}</td>";
echo "<td>{$row['Rush_Yds']}</td>";
echo "<td>{$row['Rush_TDs']}</td>";
echo "<td>{$row['Total_Fantasy_Pts']}</td>";
echo "<td>{$row['ID']}</td>";
echo "<td><form action=\"edit.php\" method=\"post\"><input type=\"submit\" name=\"submit\" value=\"Edit\"></form></td></tr>";
}
?>
</table>
</div>
</div>
<div class="tab_container">
<div id="tab2" class="tab_content">
<table cellspacing="0" cellpadding="5" border="1" width="560">
<tr style="text-align:center">
<td style="width:175px ; text-align:left">Player Name</td>
<td>Team</td>
<td>Rush Yds</td>
<td>Rush TDs</td>
<td>Rec Yds</td>
<td>Rec TDs</td>
<td>Rec</td>
<td>Fumbles</td>
<td>TFP</td>
</tr>
<?php
$result = mysql_query("SELECT Player, Team, Rush_Yds, Rush_TDs, Rec_Yds, Rec_TDs, Receptions, Fumbles, Total_Fantasy_Pts FROM ff_projections WHERE Position = 'RB' ORDER BY Rush_Yds DESC;");
while($row = mysql_fetch_array($result))
{
echo "<tr style=\"text-align:center\"><td style=\"text-align:left\">{$row['Player']}</td>";
echo "<td>{$row['Team']}</td>";
echo "<td>{$row['Rush_Yds']}</td>";
echo "<td>{$row['Rush_TDs']}</td>";
echo "<td>{$row['Rec_Yds']}</td>";
echo "<td>{$row['Rec_TDs']}</td>";
echo "<td>{$row['Receptions']}</td>";
echo "<td>{$row['Fumbles']}</td>";
echo "<td>{$row['Total_Fantasy_Pts']}</td></tr>";
}
?>
</table>
</div>
</div>
<div class="tab_container">
<div id="tab3" class="tab_content">
<table cellspacing="0" cellpadding="5" border="1" width="560">
<tr style="text-align:center">
<td style="width:175px ; text-align:left">Player Name</td>
<td>Team</td>
<td>Rec Yds</td>
<td>Rec TDs</td>
<td>Rec</td>
<td>Fumbles</td>
<td>TFP</td>
</tr>
<?php
$result = mysql_query("SELECT Player, Team, Rec_Yds, Rec_TDs, Receptions, Fumbles, Total_Fantasy_Pts FROM ff_projections WHERE Position = 'WR' ORDER BY Rec_Yds DESC;");
while($row = mysql_fetch_array($result))
{
echo "<tr style=\"text-align:center\"><td style=\"text-align:left\">{$row['Player']}</td>";
echo "<td>{$row['Team']}</td>";
echo "<td>{$row['Rec_Yds']}</td>";
echo "<td>{$row['Rec_TDs']}</td>";
echo "<td>{$row['Receptions']}</td>";
echo "<td>{$row['Fumbles']}</td>";
echo "<td>{$row['Total_Fantasy_Pts']}</td></tr>";
}
?>
</table>
</div>
</div>
<div class="tab_container">
<div id="tab4" class="tab_content">
<table cellspacing="0" cellpadding="5" border="1" width="560">
<tr style="text-align:center">
<td style="width:175px ; text-align:left">Player Name</td>
<td>Team</td>
<td>Rec Yds</td>
<td>Rec TDs</td>
<td>Rec</td>
<td>Fumbles</td>
<td>TFP</td>
</tr>
<?php
$result = mysql_query("SELECT Player, Team, Rec_Yds, Rec_TDs, Receptions, Fumbles, Total_Fantasy_Pts FROM ff_projections WHERE Position = 'TE' ORDER BY Rec_Yds DESC;");
while($row = mysql_fetch_array($result))
{
echo "<tr style=\"text-align:center\"><td style=\"text-align:left\">{$row['Player']}</td>";
echo "<td>{$row['Team']}</td>";
echo "<td>{$row['Rec_Yds']}</td>";
echo "<td>{$row['Rec_TDs']}</td>";
echo "<td>{$row['Receptions']}</td>";
echo "<td>{$row['Fumbles']}</td>";
echo "<td>{$row['Total_Fantasy_Pts']}</td></tr>";
}
?>
</table>
</div>
</div>
<div class="tab_container">
<div id="tab5" class="tab_content">
<table cellspacing="0" cellpadding="5" border="1" width="560">
<tr style="text-align:center">
<td style="text-align:left ; width:175px">Player Name</td>
<td>Team</td>
<td>FG</td>
<td>Extra Pts</td>
<td>TFP</td>
</tr>
<?php
$result = mysql_query("SELECT Player, Team, FG, Extra_Pts, Overall_Pts, Total_Fantasy_Pts FROM ff_projections WHERE Position = 'K' ORDER BY FG DESC;");
while($row = mysql_fetch_array($result))
{
echo "<tr style=\"text-align:center\"><td style=\"text-align:left\">{$row['Player']}</td>";
echo "<td>{$row['Team']}</td>";
echo "<td>{$row['FG']}</td>";
echo "<td>{$row['Extra_Pts']}</td>";
echo "<td>{$row['Total_Fantasy_Pts']}</td></tr>";
}
?>
</table>
</div>
</div>
<div class="tab_container">
<div id="tab6" class="tab_content">
<table cellspacing="0" cellpadding="5" border="1" width="560">
<tr style="text-align:center">
<td>Team</td>
<td>Sacks</td>
<td>Int</td>
<td>Def TD</td>
<td>ST TD</td>
<td>Shutouts</td>
<td>TFP</td>
</tr>
<?php
$result = mysql_query("SELECT Team, Sacks, Int_Caught, Def_TD, ST_TD, Shutouts, Total_Fantasy_Pts FROM ff_projections WHERE Position = 'Def / ST' ORDER BY Def_TD DESC;");
while($row = mysql_fetch_array($result))
{
echo "<tr style=\"text-align:center\"><td>{$row['Team']}</td>";
echo "<td>{$row['Sacks']}</td>";
echo "<td>{$row['Int_Caught']}</td>";
echo "<td>{$row['Def_TD']}</td>";
echo "<td>{$row['ST_TD']}</td>";
echo "<td>{$row['Shutouts']}</td>";
echo "<td>{$row['Total_Fantasy_Pts']}</td></tr>";
}
?>
</table>
</div>
</div>
</body>
</html>
Never forget to properly sanitize any entry touching your database "mysql_real_escape_string();.
But it seems that its its complaining about the undefined index $_POST['ID'];, its because either nothing is being sent to it or perhaps there is a naming issue being posted to that page.
Try to set a static variable of one of your values as $posted_id:
$posted_id = 3262732 (or a known post id) to see if your form correctly returns.
If that does work then review what data is being posted to edit.php.

Getting the value from first cell in a row to submit using Javascript

I create a table using PHP from a database, meaning the size of the table and values vary.
The table creation:
<table>
<thead>
<tr>
<th>Ticket</th>
<th>Empresa</th>
<th>Requerente</th>
<th>Motivo</th>
<th>Data</th>
<th>Hora</th>
</tr>
</thead>
<tbody>
<form name="goTicket" action="resolver.php" method="POST" >
<?php
$sql = "QUERY";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<tr onmouseover=\"this.style.background='#EFF5FB';this.style.cursor='pointer'\"
onmouseout=\"this.style.background='white';\" onclick=\"javascript: submitform()\">";
echo "<td>$row[Ticket]</td>";
echo "<input type='hidden' name='_ticket' value='$row[Ticket]' />";
echo "<td>$row[Empresa]</td>";
echo "<td>$row[Requerente]</td>";
echo "<td>$row[Motivo]</td>";
echo "<td>$row[Data]</td>";
echo "<td>$row[Hora]</td>";
echo "</tr>";
}
echo "</form>";
echo "<tr><td colspan='6' style='text-align: center;'><form action='adminmenu.php' ><br /><input type='submit' name='woot' value='Main Menu' /></form></td></tr>";
echo "<tbody>";
echo "</table>";
?>
The Javacript function used to submit is this one:
<script type="text/javascript">
function submitform()
{
document.forms["goTicket"].submit();
}
</script>
Now whenever I click on a row it takes to the appropriate page "resolver.php" but always sends the most recent data, from last time the while was executed.
I need the value from the first cell, or the hidden input tag, that contains what I need, from the row I click.
Thank you.
Full suggestion
<script type="text/javascript">
function submitform(ticket) {
document.goTicket.elements["_ticket"].value=ticket;
document.goTicket.submit();
}
</script>
<table>
<thead>
<tr>
<th>Ticket</th>
<th>Empresa</th>
<th>Requerente</th>
<th>Motivo</th>
<th>Data</th>
<th>Hora</th>
</tr>
</thead>
<tbody>
<form name="goTicket" action="resolver.php" method="POST" >
<input type="hidden" name="_ticket" value="" />
<?php
$sql = "QUERY";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
?>
<tr onmouseover="this.style.background='#EFF5FB';this.style.cursor='pointer'"
onmouseout="this.style.background='white';"
onclick="submitform('<?php echo $row[Ticket]; ?>')">
<td><?php echo $row[Ticket]; ?></td>
<td><?php echo $row[Empresa]; ?></td>
<td><?php echo $row[Requerente]; ?></td>
<td><?php echo $row[Motivo]; ?></td>
<td><?php echo $row[Data]; ?></td>
<td><?php echo $row[Hora]; ?></td>
</tr>
<?php } ?>
</form><!-- not really nice -->
<tr><td colspan="6" style="text-align: center;"><form action="adminmenu.php" >
<br /><input type="submit" name="woot" value="Main Menu" /></form></td></tr>
</tbody>
</table>
Your problem is that you're just submitting the form without any reference to what was clicked. All the hidden inputs have the same name so the form just sends the last one.
Try the following:
echo "<tr onmouseover=\"this.style.background='#EFF5FB';this.style.cursor='pointer'\" onmouseout=\"this.style.background='white';\" onclick=\"javascript: submitform('$row[Ticket]')\">";
// delete this from while:
// echo "<input type='hidden' name='_ticket' value='$row[Ticket]'/>";
// and instead add before </form> with value=""
Then change your javascript to change the value before submitting:
function submitform(val) {
document.forms["goTicket"].elements["_ticket"].value = val;
document.forms["goTicket"].submit();
}
Kudos to mplungjan also got this as I was typing!
If I see good ... Yo set form with many of hidden inputs with same name ... what you want with this... of course it return last value ...
you have to changing names in loop and then call exact name of input

Categories