I've created in PHP a html table with a combobox that retrieves the values of a mysql database table.
This is my code:
<?php
include_once 'indeling/header.php';
print 'Overzicht nota\'s <br /><br />';
$sql = "
SELECT
notas.id AS notaid
, notas.klantid AS klantid
, notas.bedrag AS bedrag
, notas.datum AS datum
, contacten.bedrijf AS bedrijf
, contacten.adres AS adres
, contacten.woonplaats AS woonplaats
FROM
notas
LEFT JOIN contacten
ON (notas.klantid = contacten.id);
";
function bedrijven($mysqli) {
$sqlbedrijven = "
SELECT
id
, bedrijf
, adres
, woonplaats
FROM
contacten
ORDER BY bedrijf ASC
";
$resultbedrijven = $mysqli->query($sqlbedrijven);
if (!$resultbedrijven) {
echo "something went wrong: (" . $mysqli->error .")";
}
echo "<select name = klantid>\n";
while ($row = $resultbedrijven->fetch_assoc()) {
echo <<<opt
<option value="{$row['id']}"> {$row['bedrijf']} - {$row['adres']} - {$row['woonplaats']} </option>
opt;
}
echo "</select>\n";
}
$result = $mysqli->query($sql);
if (!$result) {
echo "Oeps hier gaat iets fout: (" . $mysqli->error .")";
}
else {
printf("Er zijn momenteel %d nota's.<br />", $result->num_rows);
echo "
<table>
<tr>
<th>notanummer.</th>
<th>bedrijf</th>
<th>bedrag</th>
<th>datum</th>
<th>bewerken</th>
</tr>
";
while ($row = $result->fetch_assoc()) {
echo '<tr> <form action="overzicht_relaties_bewerken.php" method="post">
<td> <input type="text" class="short" name="notaid" value="' . $row['notaid'] . '"></td>
<td> ' . bedrijven($mysqli) . ' </td>
<td> <input type="text" name="bedrag" value="' . $row['bedrag'] . '"></td>
<td> <input type="date" name="datum" value="' . $row['datum'] . '"></td>
<td> <input type="submit" name="update" value="aanpassen" class="button">' . '<br />
<input type="submit" name="delete" value="verwijderen" class="button"' . '"></td>
</tr></form>
';
}
echo "</table>";
}
?>
it all works but I see the comboboxes above the table and not in the second TD bedrijven($mysqli)
here is a dump of mij html source code of the webpage:
Overzicht nota's <br /><br />Er zijn momenteel 2 nota's.<br />
<table>
<tr>
<th>notanummer.</th>
<th>bedrijf</th>
<th>bedrag</th>
<th>datum</th>
<th>bewerken</th>
</tr>
<select name = klantid>
<option value="37"> afsdf - fasdf12 - Klarenbeek - (Gelderland) </option>
<option value="36"> afsdf - fasdf12 - Klarenbeek - (Gelderland) </option>
<option value="38"> afsdf2 - fdas - Klarenbeek </option>
now we have a bunch of more option values
and then:
</select>
<tr> <form action="overzicht_relaties_bewerken.php" method="post">
<td> <input type="text" class="short" name="notaid" value="2"></td>
<td> </td>
<td> <input type="text" name="bedrag" value="125.50"></td>
<td> <input type="date" name="datum" value="2013-06-04"></td>
the table data under tabledata id is blank...?
So I call the function at the right place (2nd TD) but is shows the comboboxes somewhere else.
Any idea how this is possible?
I am not good at php but I think your select tag is not in the table! See how i do this in html:
<table>
<tr>
<th>notanummer.</th>
<th>bedrijf</th>
<th>bedrag</th>
<th>datum</th>
<th>bewerken</th>
</tr>
<tr>
<td colspan='5'>
<select name = klantid>
<option value="37"> afsdf - fasdf12 - Klarenbeek - (Gelderland) </option>
<option value="36"> afsdf - fasdf12 - Klarenbeek - (Gelderland) </option>
<option value="38"> afsdf2 - fdas - Klarenbeek </option>
</select>
</td>
</tr>
<tr> <form action="overzicht_relaties_bewerken.php" method="post">
<td> <input type="text" class="short" name="notaid" value="2"></td>
<td> </td>
<td> <input type="text" name="bedrag" value="125.50"></td>
<td> <input type="date" name="datum" value="2013-06-04"></td>
so change your php code from:
echo "<select name = klantid>\n";
while ($row = $resultbedrijven->fetch_assoc()) {
echo <<<opt
<option value="{$row['id']}"> {$row['bedrijf']} - {$row['adres']} - {$row['woonplaats']} </option>
opt;
}
echo "</select>\n";
to:
echo "<tr><td colspan='5'><select name = klantid>\n";
while ($row = $resultbedrijven->fetch_assoc()) {
echo <<<opt
<option value="{$row['id']}"> {$row['bedrijf']} - {$row['adres']} - {$row['woonplaats']} </option>
opt;
}
echo "</select></td></tr>\n";
Related
I have a table named students of students that I display in a form to be able to update their data.
I also have a table named Groepen (groups), and some students are part of a group. The group they are part of is also displayed. And there is a select option to make a student part of a group.
Looks like this:
If a student is part of a group and I update for instance, his phone number, he isn't in the group anymore after the update.
How can I prevent this?
My code is
<?php
$q = "SELECT * FROM students LEFT JOIN Groepen ON Groepen.groep_id=students.groep WHERE students.uid = '$user[uid]' ORDER BY st_last ASC";
$r = mysqli_query($dbc, $q);
if (mysqli_num_rows($r) > 0) {
echo "
<p>Om gegevens te wijzigen of aan te vullen kunt u gewoon in het desbetreffende veld typen en vervolgens op 'update' klikken.</p></br></br>
<table class='table'>
<thead>
<tr>
<th>Naam</th>
<th>Voornaam</th>
<th>Graad</th>
<th>Telefoon</th>
<th>Gsm</th>
<th>Email</th>
<th>Lid van groep</th>
<th>Toevoegen aan/verwijderen uit groep</th>
</tr>
</thead>";
while($student_list = mysqli_fetch_assoc($r)) { ?>
<tbody>
<form action="" method="POST" role="form">
<tr>
<input type="hidden" value="<?php echo $student_list['sid']?>" name="sid" />
<td width="8%"><input type="text" class="form-control" name="st_last" value="<?php echo $student_list['st_last']; ?>" /></td>
<td width="6%"><input type="text" class="form-control" name="st_first" value="<?php echo $student_list['st_first']; ?>"/></td>
<td width="2%"><input type="text" class="form-control" name="graad" value="<?php echo $student_list['graad']; ?>"/></td>
<td width="7%"><input type="text" class="form-control" name="vaste_telefoon" value="<?php echo $student_list['vaste_telefoon']; ?>"/></td>
<td width="7%"><input type="text" class="form-control" name="gsm" value="<?php echo $student_list['gsm']; ?>"/></td>
<td width="10%"><input type="text" class="form-control" name="email" value="<?php echo $student_list['email']; ?>"/></td>
<td width="8%" style="padding-top: 15px;"><?php echo $student_list['groepsnaam']; ?></td>
<td width="7%"><div class="form-group">
<select class="form-control" name="groep_id" id="groep">
<option value="><?$student_list['groepid']; ?>"><?php echo $student_list['groepsnaam']; ?></option>
<?php
$q2 = "SELECT * FROM Groepen ORDER BY groepsnaam ASC";
$r2 = mysqli_query($dbc, $q2);
while($groep_list = mysqli_fetch_assoc($r2)) {
?>
<option value="<?php echo $groep_list['groep_id']; ?>"><?php echo $groep_list['groepsnaam']; ?></option>
<?php } ?>
</select>
</div></td>
<td width="12%"> <div class="btn-group" role="group" aria-label="...">
<button type="submit" name="updatell" class="btn btn-warning">Update</button>
<button type="submit" name="deletell" class="btn btn-danger">Delete</button></td>
</tr>
</form>
</tbody>
<?php } }
else {
echo "U hebt nog geen leerlingen toegevoegd.";
}
?>
</table>
And the update query:
<?php
if(isset($_POST['updatell'])) {
$qupdatestudent = "UPDATE students SET st_last='$_POST[st_last]', st_first='$_POST[st_first]', groep='$_POST[groep_id]', graad='$_POST[graad]', vaste_telefoon='$_POST[vaste_telefoon]', gsm='$_POST[gsm]', email='$_POST[email]' WHERE sid='$_POST[sid]'";
$r2 = mysqli_query($dbc, $qupdatestudent);
}
if(isset($_POST['deletell'])) {
$deletestudent = "DELETE FROM students WHERE sid='$_POST[sid]'";
$r3 = mysqli_query($dbc, $deletestudent);
}
?>
See this line here,
<option value="><?$student_list['groepid']; ?>"><?php echo $student_list['groepsnaam']; ?></option>
^ see this closing bracket
Instead of putting an extra <option> in the <select> element, in each iteration of while loop check if the current group id matches with the student's group id or not, and make it selected accordingly, like this:
<select class="form-control" name="groep_id" id="groep">
<?php
$q2 = "SELECT * FROM Groepen ORDER BY groepsnaam ASC";
$r2 = mysqli_query($dbc, $q2);
while($groep_list = mysqli_fetch_assoc($r2)) {
?>
<option value="<?php echo $groep_list['groep_id']; ?>"<?php if($groep_list['groep_id'] == $student_list['groepid']){ echo " selected='selected'"; } ?>><?php echo $groep_list['groepsnaam']; ?></option>
<?php
}
?>
</select>
I have a list proxy.Once I select and save it I want to display the selected data. How can I do this?.I want to display it in selected list and once I reload or get back to that page `
<?php
include_once("../noaccess.php");
include_once(CLASS_PATH."fetch_service.php");
$objfetch = new fetchService();
include_once(CLASS_PATH."proxy.php");
$objproxy = new Proxy();
include_once(CLASS_PATH."log.php");
$objlog= new changelog();
$account_id=$_SESSION['account_id'];
global $mysqli;
if($_POST['submit']=="Save")
{
$pname=$_POST['proxy'];
$query="UPDATE `proxy` SET `proxy_default`='1' where `proxyname`='$pname'";
$mysqli->query($query) or die($mysqli->error);
$_SESSION['check_update'] = "1";
setcookie("msg","Proxy Seleted",time()+5,"/");
header("location:".SITE_URL."index.php?view=default_proxy");
}
?>
<form name="frmcr" id="frmcr" action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="mode" id="mode" value="insert" />
<table align="left" id="tblworking_hours" class="tbl_altcolor shadow" style="width:25%;margin-left:30px">
<thead>
<tr>
<th align="center"><b>Proxy</b></th>
<td><select name="proxy" id="proxy" class="required input">
<option value="">Select</option>
<!-- <option value="<?php echo $i ;?>"<?php echo $i==$ring21 ? "selected":"";?> ><?php echo $i;?></option> -->
<?php
$result = $objfetch->fetch_proxy("*","where account_id='".$_SESSION['account_id']."' ");
foreach($result as $key=>$resrproxy)
{
?>
<option value="<?php echo $resrproxy['proxyname'];?>"<?php echo $resrproxy['proxyname']== $resrproxy['proxyname'] ? "selected":"";?>><?php echo $resrproxy['proxyname'];?></option>
<?php } ?>
</select></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" id="submit" value="Save" class="btn" style="margin-left:35px;"/></td>
</tr>
</thead>
</table>
</div>
You've to compare $resrproxy['proxyname'] value with $pname. Your code should be like this:
// your code
<select name="proxy" id="proxy" class="required input">
<option value="">Select</option>
<?php
$result = $objfetch->fetch_proxy("*","where account_id='".$_SESSION['account_id']."' ");
foreach($result as $key=>$resrproxy){
$option = "<option value=\"{$resrproxy['proxyname']}\"";
if(isset($pname)){
if($pname == $resrproxy['proxyname']){
$option .= " selected=\"selected\"";
}
}
$option .= ">{$resrproxy['proxyname']}</option>";
echo $option;
}
?>
</select>
// your code `
<td>
<select name="proxy" id="proxy" class="required input">
<option value="">Select</option>
<?php
$result = $objfetch->fetch_proxy("*","where account_id='".$_SESSION['account_id']."' and `proxy_default`='1'");
$pname=$result[0]['proxyname'];
$result = $objfetch->fetch_proxy("*","where account_id='".$_SESSION['account_id']."' ");
foreach($result as $key=>$resrproxy)
{
?>
<option value="<?php echo $resrproxy['proxyname'];?>"<?php echo $resrproxy['proxyname']==$pname ? "selected":"";?>><?php echo $resrproxy['proxyname'];?></option>
<?php } ?>
</select>
</td>
I have some search fields that look across a few different tables.
"display_name" form field and "last" form field show different results then what it does directly into phpmyadmin.
If i echo out the mysql query in my php script and paste it into phpmyadmin. It lists of correct results. However on the php/html page it is not listing the same.
For example. If someones name full name is nathan spencer and i put spencer into the "last" form field it will only show 1 result or 2 results. HOWEVER there are actually 5 results found by pasting it directly into phpmyadmin and running it.
I have been battling for ages with this and its driving me nuts.
Here is the PHP up the top of the page:
<?php
// SEARCH
if(isset($_POST['submit'])) {
// define the list of fields
$fields = array('display_name', 'last', 'suburb', 'state', 'user_type', 'active');
$conditions = array();
// loop through the defined fields
foreach($fields as $field){
// if the field is set and not empty
if(isset($_POST[$field]) && $_POST[$field] != '') {
// create a new condition while escaping the value inputed by the user (SQL Injection)
$conditions[] = "`$field` LIKE '%".mysql_real_escape_string($_POST[$field])."%'";
}
}
// builds the query
$query = "SELECT display_name, first, last, suburb, state, user_type, active FROM nfw_users ";
// if there are conditions defined
if(count($conditions) > 0) {
// append the conditions
$query .= "WHERE " . implode (' AND ', $conditions) .""; // you can change to 'OR', but I suggest to apply the filters cumulative
}
else {
echo "No records found";
}
$result = mysql_query($query);
$score = mysql_fetch_assoc($result);
}
?>
and here is the html form
<form method="post" action="index.php">
<tr>
<td>Name:</td>
<td><input type="text" name="display_name" /></td>
</tr>
<tr>
<td>Street:</td>
<td><input type="text" name="last" /></td>
</tr>
<tr>
<td>Suburb:</td>
<td><input type="text" name="suburb" /></td>
</tr>
<tr>
<td>State:</td>
<td>
<select name="state">
<option>
<option value="qld">QLD</option>
<option value="sa">SA</option>
<option value="nt">NT</option>
<option value="wa">WA</option>
<option value="vic">VIC</option>
<option value="tas">TAS</option>
<option value="act">ACT</option>
</select>
</td>
</tr>
<tr>
<td>Type:</td>
<td>
<select name="user_type">
<option>
<option value="franchise">Franchisee</option>
<option value="regional">Regional</option>
<option value="state">State</option>
<option value="national">National</option>
<option value="office">Headoffice Staff</option>
</select>
</td>
</tr>
<tr>
<td>Active:</td>
<td>
<select name="active">
<option></option>
<option value="1">Active</option>
<option value="0">Not Active</option>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Search" /></td>
</tr>
</form>
<form method="post" action="index.php">
<tr>
<td>Name:</td>
<td><input type="text" name="display_name" /></td>
</tr>
<tr>
<td>Street:</td>
<td><input type="text" name="last" /></td>
</tr>
<tr>
<td>Suburb:</td>
<td><input type="text" name="suburb" /></td>
</tr>
<tr>
<td>State:</td>
<td>
<select name="state">
<option>
<option value="qld">QLD</option>
<option value="sa">SA</option>
<option value="nt">NT</option>
<option value="wa">WA</option>
<option value="vic">VIC</option>
<option value="tas">TAS</option>
<option value="act">ACT</option>
</select>
</td>
</tr>
<tr>
<td>Type:</td>
<td>
<select name="user_type">
<option>
<option value="franchise">Franchisee</option>
<option value="regional">Regional</option>
<option value="state">State</option>
<option value="national">National</option>
<option value="office">Headoffice Staff</option>
</select>
</td>
</tr>
<tr>
<td>Active:</td>
<td>
<select name="active">
<option></option>
<option value="1">Active</option>
<option value="0">Not Active</option>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Search" /></td>
</tr>
</form>
and here is the results that get printed into a table
<?php if(isset($score)){
while($score=mysql_fetch_assoc($result)){
$display_name = $score['display_name'];
$lastname = $score['last'];
$state = $score['state'];
$active = $score['active'];
if ($active=='1') {
$activeother = "<i class='fa fa-check' style='color:green;'></i>";
}
else {
$activeother = "<i class='fa fa-times' style='color:red;'></i>";
}
?>
<?php
$content = "<tr><td>" . $score['display_name'] . "</td><td>" . $score['first'] . "</td><td>" . $score['last'] . " </td><td>" . $score['email'] . " </td><td> " . $score['mobile'] . " </td><td> " . $score['landline'] . "</td><td>$activeother</td><td> " . $score['user_type'] . "</td><td> " . date('d-m-Y', strtotime($score['date_join'])) . "</td><td class='invoicing-columns'><a class='btn btn-yellow' href='view-invoices.php?id=" . $score['id_num'] . "'><i class='fa fa-eye'></i></a></td><td class='invoicing-columns'><a class='btn btn-red' href='del-customers.php?id=" . $score['id_num'] . "' onclick='return check();' class='delete'><i class='fa fa-minus-circle'></i></a></td></tr>";
echo $content;
}}
?>
If I got your code right there might be issue:
Here you actually fetching first row.
$result = mysql_query($query);
$score = mysql_fetch_assoc($result);
Then it seems that you simply skip it and go with loop:
if(isset($score)){
while($score=mysql_fetch_assoc($result)){
What you really want to do is:
$result = mysql_query($query);
if (!$result) {
//TODO: Query error handling
}
// This is how you check for results count
if (mysql_num_rows($result) == 0) {
while ($score = mysql_fetch_assoc($result)) {
//Here you go with result
}
}
You can also check this manual link as reference.
Next thing to check, get exact string from query and try it from phpMyAdmin. Make sure that you use same user as your code does, when doing query.
And one more thing, extension you are using is long time deprecated, you should consider to switching to MySQLi or PDO_MySQL
I have a simple form on my page and I want to make sure every field is filled before inserting the values into the database. The problem is the condition never met, even if every field is filled I still get "Something is missing"...
Search:
<form method="post" action="<?php echo $_SERVER[" PHP_SELF "]?>">
<input placeholder="e-mail address" type="text" name="email_search">
<input type="submit" name="search" value="Go">
<?php if (isset($_POST[ "search"])) { $email_search=m ysql_real_escape_string($_POST[ "email_search"]); $check=m ysql_query( "SELECT * FROM torzsvendegek WHERE email = '$email_search'"); $s=m ysql_fetch_array($check); }?>
<form method="post" action="<?php echo $_SERVER[" PHP_SELF "]?>">
<table width="440" border="0" style="text-align:right;">
<tr>
<td>E-mail:</td>
<td>
<input type="text" name="email" value="<?php echo $email_search;?>" disabled>
</td>
</tr>
<tr>
<td>Név:</td>
<td>
<input type="text" name="nev" value="<?php echo $s['nev'];?>">
</td>
</tr>
<tr>
<td>Mikor:</td>
<td>
<input type="text" name="mikor">
</td>
</tr>
<tr>
<td>Éjszakák száma:</td>
<td>
<input type="text" name="ejszakak">
</td>
</tr>
<tr>
<td>Nemzetisége:</td>
<td align="left">
<select name="nyelv">
<option value="magyar" <?php if($s[ 'nyelv']=="magyar" ) echo "selected=\"selected\ ""; ?>>Magyar</option>
<option value="nemet" <?php if($s[ 'nyelv']=="nemet" ) echo "selected=\"selected\ ""; ?>>Német</option>
<option value="lengyel" <?php if($s[ 'nyelv']=="lengyel" ) echo "selected=\"selected\ ""; ?>>Lengyel</option>
<option value="roman" <?php if($s[ 'nyelv']=="roman" ) echo "selected=\"selected\ ""; ?>>Román</option>
<option value="szlovak" <?php if($s[ 'nyelv']=="szlovak" ) echo "selected=\"selected\ ""; ?>>Szlovák</option>
<option value="egyeb" <?php if($s[ 'nyelv']=="egyeb" ) echo "selected=\"selected\ ""; ?>>Egyéb</option>
</select>
</td>
</tr>
<tr>
<td>Megjegyzés:</td>
<td>
<textarea name="megjegyzes">
<?php echo htmlspecialchars($s[ 'megjegyzes']);?>
</textarea>
</td>
</tr>
</table>
<br>
<table width="440">
<tr>
<td>
<input type="submit" name="submit_add" value="Hozzáad">
</td>
</tr>
</table>
</form>
<?php if (isset($_POST[ "submit_add"]) && !empty($_POST[ "nev"]) && !empty($_POST[ "email"]) && !empty($_POST[ "mikor"]) && !empty($_POST[ "ejszakak"])){ $nev=m ysql_real_escape_string($_POST[ "nev"]); $email=m ysql_real_escape_string($_POST[ "email"]); $mikor=m ysql_real_escape_string($_POST[ "mikor"]); $ejszakak=m ysql_real_escape_string($_POST[ "ejszakak"]); $nyelv=m ysql_real_escape_string($_POST[ "nyelv"]); $megjegyzes=m ysql_real_escape_string($_POST[ "megjegyzes"]); $check2=m ysql_query( "SELECT * FROM torzsvendegek WHERE email = '$email'"); $br="<br>" ; if (mysql_num_rows($check2)> 0) { $adatok = mysql_fetch_array($check2); $osszesen = ($adatok['ejszakak'] + $ejszakak); mysql_query("UPDATE torzsvendegek SET nev = '".$nev."', mikor = '".$adatok['mikor']."".$mikor."".$br."', ejszakak = '".$osszesen."', nyelv = '".$nyelv."', megjegyzes = '".$adatok['megjegyzes']."".$megjegyzes."".$br."' WHERE email = '".$email."'"); echo "
<br>".$email." Updated"; } else { mysql_query("INSERT INTO torzsvendegek (id, nev, email, mikor, ejszakak, nyelv, megjegyzes) VALUES (NULL, '$nev', '$email', '".$mikor."".$br."', '$ejszakak', '$nyelv', '".$megjegyzes."')"); echo "
<br>".$email." Added"; } } else { echo "Something is missing"; } ?>
You made 2 mistakes in your code:
1) You didn't close the first form (missing </form>)
2) You disabled the E-mail input field which resulted in always empty
Here's the updated (though deprecated and insecure!!!) code:
<?php
if(isset($_POST["search"])){
$email_search = mysql_real_escape_string($_POST["email_search"]);
$check = mysql_query("SELECT * FROM torzsvendegek WHERE email = '$email_search'");
$s = mysql_fetch_array($check);
}
if(isset($_POST["submit_add"]) && !empty($_POST["nev"]) && !empty($_POST["email"]) && !empty($_POST["mikor"]) && !empty($_POST["ejszakak"])){
$nev = mysql_real_escape_string($_POST["nev"]);
$email = mysql_real_escape_string($_POST["email"]);
$mikor = mysql_real_escape_string($_POST["mikor"]);
$ejszakak = mysql_real_escape_string($_POST["ejszakak"]);
$nyelv = mysql_real_escape_string($_POST["nyelv"]);
$megjegyzes = mysql_real_escape_string($_POST["megjegyzes"]);
$check2 = mysql_query("SELECT * FROM torzsvendegek WHERE email = '$email'");
$br = "<br>";
if (mysql_num_rows($check2) > 0){
$adatok = mysql_fetch_array($check2);
$osszesen = ($adatok['ejszakak'] + $ejszakak);
mysql_query("UPDATE torzsvendegek SET nev = '".$nev."', mikor = '".$adatok['mikor']."".$mikor."".$br."', ejszakak = '".$osszesen."', nyelv = '".$nyelv."', megjegyzes = '".$adatok['megjegyzes']."".$megjegyzes."".$br."' WHERE email = '".$email."'");
echo "<br>".$email." Updated";
} else {
mysql_query("INSERT INTO torzsvendegek (id, nev, email, mikor, ejszakak, nyelv, megjegyzes) VALUES (NULL, '$nev', '$email', '".$mikor."".$br."', '$ejszakak', '$nyelv', '".$megjegyzes."')");
echo "<br>".$email." Added";
}
} else {
echo "Something is missing";
}
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]?>">
Search: <input placeholder="e-mail address" type="text" name="email_search">
<input type="submit" name="search" value="Go">
</form>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]?>">
<table width="440" border="0" style="text-align:right;">
<tr>
<td>E-mail:</td>
<td><input type="text" name="email" value="<?php echo $email_search;?>"></td>
</tr>
<tr>
<td>Név:</td>
<td><input type="text" name="nev" value="<?php echo $s['nev'];?>"></td>
</tr>
<tr>
<td>Mikor:</td>
<td><input type="text" name="mikor"></td>
</tr>
<tr>
<td>Éjszakák száma:</td>
<td><input type="text" name="ejszakak"></td>
</tr>
<tr>
<td>Nemzetisége:</td>
<td align="left">
<select name="nyelv">
<option value="magyar" <?php if($s['nyelv']=="magyar") echo "selected=\"selected\""; ?>>Magyar</option>
<option value="nemet" <?php if($s['nyelv']=="nemet") echo "selected=\"selected\""; ?>>Német</option>
<option value="lengyel" <?php if($s['nyelv']=="lengyel") echo "selected=\"selected\""; ?>>Lengyel</option>
<option value="roman" <?php if($s['nyelv']=="roman") echo "selected=\"selected\""; ?>>Román</option>
<option value="szlovak" <?php if($s['nyelv']=="szlovak") echo "selected=\"selected\""; ?>>Szlovák</option>
<option value="egyeb" <?php if($s['nyelv']=="egyeb") echo "selected=\"selected\""; ?>>Egyéb</option>
</select>
</td>
</tr>
<tr>
<td>Megjegyzés:</td>
<td><textarea name="megjegyzes"><?php echo htmlspecialchars($s['megjegyzes']);?></textarea></td>
</tr>
</table>
<br>
<table width="440">
<tr>
<td><input type="submit" name="submit_add" value="Hozzáad"></td>
</tr>
</table>
</form>
You need to close your search form tag to keep the two forms separated
Search:<form method="post" action="<?php echo $_SERVER["PHP_SELF"]?>"><input placeholder="e-mail address" type="text" name="email_search"><input type="submit" name="search" value="Go"></form>
and
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]?>">
<table width="440" border="0" style="text-align:right;">
<tr><td>E-mail:</td><td><input type="text" name="email" value="<?php echo $email_search;?>" disabled></td></tr>
<tr><td>Név:</td><td><input type="text" name="nev" value="<?php echo $s['nev'];?>"></td></tr>
<tr><td>Mikor:</td><td><input type="text" name="mikor"></td></tr>
<tr><td>Éjszakák száma:</td><td><input type="text" name="ejszakak"></td></tr>
<tr><td>Nemzetisége:</td><td align="left"> <select name="nyelv">
<option value="magyar" <?php if($s['nyelv']=="magyar") echo "selected=\"selected\""; ?>>Magyar</option>
<option value="nemet" <?php if($s['nyelv']=="nemet") echo "selected=\"selected\""; ?>>Német</option>
<option value="lengyel" <?php if($s['nyelv']=="lengyel") echo "selected=\"selected\""; ?>>Lengyel</option>
<option value="roman" <?php if($s['nyelv']=="roman") echo "selected=\"selected\""; ?>>Román</option>
<option value="szlovak" <?php if($s['nyelv']=="szlovak") echo "selected=\"selected\""; ?>>Szlovák</option>
<option value="egyeb" <?php if($s['nyelv']=="egyeb") echo "selected=\"selected\""; ?>>Egyéb</option>
</select></td></tr>
<tr><td>Megjegyzés:</td><td><textarea name="megjegyzes"><?php echo htmlspecialchars($s['megjegyzes']);?></textarea></td></tr>
</table><br>
<table width="440"><tr><td><input type="submit" name="submit_add" value="Hozzáad"></td></tr></table>
</form>
You did not close your search form and you need to remove the disabled attribute from your email input field.
I re-wrote your code to help you will debugging. I commented out all the stuff related to the database so you can focus on the form fields only. Here is the code I re-wrote. I left comments so you can see what I did.
<?php
/******JUST TO MAKE DEBBUGGING EASIER***/
echo "<pre>"; // Start of the pre> tags
/**ANYTHING TO DO WITH THE DATABASE I HAVE COMMENTED OUT**/
if(isset($_POST["search"])){
$email_search = mysql_real_escape_string($_POST["email_search"]);
/**PRINT_R FOR DEBUGGING PURPOSES, REMOVE!!*/
print_r($email_search);
//$check = mysql_query("SELECT * FROM torzsvendegek WHERE email = '$email_search'");
//$s = mysql_fetch_array($check);
}
/***CHECK THE POST DATA, REMOVE FROM APPLICATION ONCE YOU HAVE DEBUGGED THE DATA**/
print_r($_POST);
/***I WILL STORE THE POST DATA IN VARIABLES BEFORE CHECKING**/
$nev = isset($_POST["nev"]) ? mysql_real_escape_string($_POST["nev"]) : null;
$email = isset($_POST["email"]) ? mysql_real_escape_string($_POST["email"]) : null;
$mikor = isset($_POST["mikor"]) ? mysql_real_escape_string($_POST["mikor"]) : null;
$ejszakak = isset($_POST["ejszakak"]) ? mysql_real_escape_string($_POST["ejszakak"]) : null;
$nyelv = isset($_POST["nyelv"]) ? mysql_real_escape_string($_POST["nyelv"]) : null;
$megjegyzes = isset($_POST["megjegyzes"]) ? mysql_real_escape_string($_POST["megjegyzes"]) : null;
if(isset($_POST["submit_add"]) && !is_null($nev) && !is_null($email) && !is_null($mikor) && !is_null($ejszakak)){
/*******
SINCE I ALREADY HAVE THEM, YOU NEED TO REMOVE THEM FROM THE CODE
$nev = mysql_real_escape_string($_POST["nev"]);
$email = mysql_real_escape_string($_POST["email"]);
$mikor = mysql_real_escape_string($_POST["mikor"]);
$ejszakak = mysql_real_escape_string($_POST["ejszakak"]);
$nyelv = mysql_real_escape_string($_POST["nyelv"]);
$megjegyzes = mysql_real_escape_string($_POST["megjegyzes"]);
******/
//$check2 = mysql_query("SELECT * FROM torzsvendegek WHERE email = '$email'");
$br = "<br>";
/*********
if (mysql_num_rows($check2) > 0) {
$adatok = mysql_fetch_array($check2);
$osszesen = ($adatok['ejszakak'] + $ejszakak);
mysql_query("UPDATE torzsvendegek SET nev = '".$nev."', mikor = '".$adatok['mikor']."".$mikor."".$br."', ejszakak = '".$osszesen."', nyelv = '".$nyelv."', megjegyzes = '".$adatok['megjegyzes']."".$megjegyzes."".$br."' WHERE email = '".$email."'");
echo "<br>".$email." Updated";
}else {
mysql_query("INSERT INTO torzsvendegek (id, nev, email, mikor, ejszakak, nyelv, megjegyzes) VALUES (NULL, '$nev', '$email', '".$mikor."".$br."', '$ejszakak', '$nyelv', '".$megjegyzes."')");
echo "<br>".$email." Added";
}
****/
}elseif (isset($_POST["submit_add"])) {
echo "Something is missing";
}
echo '</pre>';//end of pre
?>
Search:
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]?>">
<input placeholder="e-mail address" type="text" name="email_search">
<input type="submit" name="search" value="Go">
</form>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]?>">
<table width="440" border="0" style="text-align:right;">
<tr>
<td>E-mail:</td>
<td><input type="text" name="email" value="<?php echo $email;?>" ></td>
</tr>
<tr>
<td>Név:</td>
<td><input type="text" name="nev" value="<?php echo $nev;?>"></td>
</tr>
<tr>
<td>Mikor:</td>
<td><input type="text" name="mikor" value="<?php echo $mikor;?>"></td>
</tr>
<tr>
<td>Éjszakák száma:</td>
<td><input type="text" name="ejszakak" value="<?php echo $ejszakak;?>"></td>
</tr>
<tr>
<td>Nemzetisége:</td>
<td align="left">
<select name="nyelv">
<option value="magyar" <?php if($nyelv=="magyar") echo "selected=\"selected\""; ?>>Magyar</option>
<option value="nemet" <?php if($nyelv=="nemet") echo "selected=\"selected\""; ?>>Német</option>
<option value="lengyel" <?php if($nyelv=="lengyel") echo "selected=\"selected\""; ?>>Lengyel</option>
<option value="roman" <?php if($nyelv=="roman") echo "selected=\"selected\""; ?>>Román</option>
<option value="szlovak" <?php if($nyelv=="szlovak") echo "selected=\"selected\""; ?>>Szlovák</option>
<option value="egyeb" <?php if($nyelv=="egyeb") echo "selected=\"selected\""; ?>>Egyéb</option>
</select>
</td>
</tr>
<tr>
<td>Megjegyzés:</td>
<td><textarea name="megjegyzes"><?php echo htmlspecialchars($megjegyzes);?></textarea>
</td>
</tr>
</table><br>
<table width="440">
<tr><td><input type="submit" name="submit_add" value="Hozzáad"></td></tr>
</table>
</form>
I need to fetch the rows based on form submission values.
Here is my form
<form name="choose" method "post" t" action="search.php">
<table>
<tr>
<tr>
<td height="3">
</td>
</tr>
<td width="60">
<font1>Prof</font1>
</td>
<td>
<select name proffession on>
<option value=""></option>
<option value="doctor"><font4>Doctor</font></option>
<option value="designer">Designer</option>
</select>
</td>
</tr>
<tr>
<tr>
<td height="3">
</td>
</tr>
<td width="60">
<font1>Source</font1>
</td>
<td>
<select name source>
<option value=""></option>
<option value="x"><font4>X</font></option>
<option value="y">Y</option>
<option value="z">Z</option>
</select>
</td>
</tr>
<tr>
<tr>
<td height="3">
</td>
</tr>
<td width="60">
<font1>Location</font1>
</td>
<td>
<select name location on>
<option value=""></option>
<option value="bangalore">Bangalore</option>
<option value="delhi">Delhi</option>
</select>
</td>
</tr>
<tr>
<td>
<input name=look type=submit value=submit>
</td>
</tr>
</form>
If there is any empty field submission i need to fetch the rows excluding that column.
Here is my search.php
<?php
mysql_connect("localhost","root","");//database connection
mysql_select_db("alldata");
$qry = "SELECT * FROM data WHERE location LIKE '" . mysql_escape_string($_POST['location']) . "' And proffession LIKE '" . mysql_escape_string($_POST['proffession']) . "' And source LIKE '" . mysql_escape_string($_POST['source']) . "'";
$res = mysql_query($qry);
function mysql_fetch_all($res) {
while($row=mysql_fetch_array($res)) {
$return[] = $row;
}
return $return;
}
function create_table($dataArr) {
echo "<tr>
"; for($j = 0; $j < count($dataarr); $j++) { echo "<td>".$dataArr[$j]."
</td>
"; } echo "
</tr>
"; } $all = mysql_fetch_all($res); echo "
<table class='data_table'>
"; for($i = 0; $i < count($all); $i++) { create_table($all[$i]); } echo "</table>";
?>
But this script is not able to get me a solution.
Please help
1.Correct your function mysql_fetch_all($res). There is no query
inside the function.
2. Deprecated: mysql_escape_string(): This
function is deprecated; use mysql_real_escape_string()
3. Correct: <select name source> to: <select name="source"> and <select name location on> to <select name="location"> and <input name=look type=submit value=submit> to <input name="look" type="submit" value="submit"> and delete t" from choose form AND <form name="choose" method="post" action="search.php">
Firstly, you should not be using mysql_* functions, see the big red box here. Consider using PDO or MySQLi instead.
Second, you appear to be missing some = such as method="post" You may want to check you're actually receiving your POST values correctly with var_dump($_POST)
Thirdly to exclude non-submitted values, you could construct the query string based on these. Something like:
$qry = "SELECT * FROM data WHERE ";
if($_POST['location']) {
$qry .= 'LIKE "%'. mysql_real_escape_string($_POST['location']) .'%"';
}
// etc...
$qry = "SELECT * FROM data WHERE location LIKE '%" . mysql_escape_string($_POST['location']) . "%' And proffession LIKE '%" . mysql_escape_string($_POST['proffession']) . "%' And source LIKE '%" . mysql_escape_string($_POST['source']) . "%'";