I have multiple books that people can order. All those books are stored in a MySQL database.
People can enter value's (INT) into a textfield. Example: Book One value = [5].
But when I enter submit it will only show the last entered value of that textfield.
How can I arrange, that if people only enter value's in some textfields and then hit submit they see what product they ordered and the value with it. Thanks :)
My code
<table width="990">
<form name="form" action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<tr>
<td width="93" class="main">Bestelnummer</td>
<td width="550" class="main">Titel</td>
<td width="100" class="main">Categorie</td>
<td width="150" class="main">Type Onderwijs</td>
<td width="80" class="main">Groep</td>
<td width="50" class="main">Prijs</td>
<td width="40" class="main">Aantal</td>
</tr>
<?php
// Laat Resultaten zien
$s = "SELECT * FROM producten ORDER BY id ASC";
$sql = (mysql_query($s))or die ("FOUT: " . mysql_error());
while($row = mysql_fetch_array($sql))
{
$id = $row['id'];
echo "<tr>";
echo "<td width='93'>" .$row['bestelnummer']. "</td>";
echo "<td width='550'><a href='".$row['link']."' target='_blank' title='".$row['titel']."' >" .$row['titel']. "</a></td>";
echo "<td width='100'>" .$row['categorie']. "</td>";
if ($row['onderwijs'] == "BO") { echo "<td width='150'>Basis Onderwijs</td>"; } elseif ($row['onderwijs'] == "VO") { echo "<td width='150'>Voortgezet Onderwijs</td>"; } else { }
echo "<td width='80'>" . $row['groep'] . "</td>";
if ($row['prijs'] == 0) { echo "<td width='50'><i>gratis</i></td>"; } else { echo "<td width='50'>€ " .$row['prijs']. "</td>"; }
echo "<td width='40'><input type='text' name='nummer".$id."' title='nummer".$id."' class='aantal' maxlength='4' /></td>";
echo "</tr>";
}
?>
<tr>
<td><input type="submit" name="submit" value="Plaats bestelling" class="verzend"/></td>
</tr>
</form>
</table>
<?php if (isset($_POST['submit']))
{
if (empty($_POST['aantal']))
{
echo "L33g";
}
else
{
$_POST['nummer".$id."'];
}
}?>
You would be better off using an array so use
<input type='text' name='nummer[".$id."]' title='nummer".$id."' class='aantal' maxlength='4' />
Then replace
$_POST['nummer".$id."'];
with
foreach($_POST['nummer'] as $value) {
echo $value;
}
Related
If I write something in search box and press search , it should only return matched rows and hide other rows.
Here is my code, it works perfects only issue is it gives me searched record + all record list of table.
What can I do to show only searched data in table.?
<div id="pageContent"><br />
<div class="search" align="right">
<form action="" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" value="Submit" />
</form>
</div>
<div class="container">
<table id="employee-grid" width="auto" cellpadding="1" cellspacing="1" border="1" class="table table-hover">
<?php
include_once '../storescripts/connect_to_mysql.php';
$num_rec_per_page=5;
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else
{
$page=1;
}
$start_from = ($page-1) * $num_rec_per_page;
$result= mysql_query("SELECT * FROM products LIMIT $start_from, $num_rec_per_page");
?>
<thead>
<tr class="success">
<th>Id</th>
<th>Product Name</th>
<th>Price</th>
<th>Status</th>
<th>Quantity</th>
<th>Details</th>
<th>Category</th>
<th>Subcategory</th>
<th>Date Added</th>
<th colspan="2">Functions</th>
</tr>
</thead>
<?php
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM products WHERE product_name LIKE '%".$term."%' or price LIKE '%".$term."' or details LIKE '%".$term."'";
$r_query = mysql_query($sql);
if($r_query>1)
{
while ($row = mysql_fetch_array($r_query)){
echo "<tr bgcolor='red'>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['product_name']."</td>";
echo "<td>".$row['price']."</td>";
echo "<td>".$row['status']."</td>";
echo "<td>".$row['quantity']."</td>";
echo "<td>".$row['details']."</td>";
echo "<td>".$row['category']."</td>";
echo "<td>".$row['subcategory']."</td>";
echo "<td>".$row['date_added']."</td>";
echo "<td><a href='product_listing_edit.php?id=".$row['id']."'>Edit</a></td>";
echo "<td><a name='delete' href='product_listing_delete.php?id=".$row['id']."'>Delete</a></td><tr>";
echo "</tr>";
}
}
else{
echo "Nothing should be displayed";
}
}
?>
<?php
while($row=mysql_fetch_array($result))
{
echo "<tr class='danger'>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['product_name']."</td>";
echo "<td>".$row['price']."</td>";
echo "<td>".$row['status']."</td>";
echo "<td>".$row['quantity']."</td>";
echo "<td>".$row['details']."</td>";
echo "<td>".$row['category']."</td>";
echo "<td>".$row['subcategory']."</td>";
echo "<td>".$row['date_added']."</td>";
echo "<td><a href='product_listing_edit.php?id=".$row['id']."'>Edit</a></td>";
echo "<td><a name='delete' href='product_listing_delete.php?id=".$row['id']."'>Delete</a></td><tr>";
echo "</tr>";
}
?>
</table>
Just keep single while loop and run the query and search query as shown below it will solve the issue:
<div id="pageContent"><br />
<div class="search" align="right">
<form action="" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" value="Submit" />
</form>
</div>
<div class="container">
<table id="employee-grid" width="auto" cellpadding="1" cellspacing="1" border="1" class="table table-hover">
<?php
include_once '../storescripts/connect_to_mysql.php';
$num_rec_per_page = 5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page = 1;
};
$start_from = ($page - 1) * $num_rec_per_page;
$sql = "SELECT * FROM products LIMIT $start_from, $num_rec_per_page";
?>
<thead>
<tr class="success">
<th>Id</th>
<th>Product Name</th>
<th>Price</th>
<th>Status</th>
<th>Quantity</th>
<th>Details</th>
<th>Category</th>
<th>Subcategory</th>
<th>Date Added</th>
<th colspan="2">Functions</th>
</tr>
</thead>
<?php
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM products WHERE product_name LIKE '%" . $term . "%' or price LIKE '%" . $term . "' or details LIKE '%" . $term . "'";
}
$r_query = mysql_query($sql);
if ($r_query > 1) {
while ($row = mysql_fetch_array($r_query)) {
echo "<tr bgcolor='red'>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['product_name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "<td>" . $row['details'] . "</td>";
echo "<td>" . $row['category'] . "</td>";
echo "<td>" . $row['subcategory'] . "</td>";
echo "<td>" . $row['date_added'] . "</td>";
echo "<td><a href='product_listing_edit.php?id=" . $row['id'] . "'>Edit</a></td>";
echo "<td><a name='delete' href='product_listing_delete.php?id=" . $row['id'] . "'>Delete</a></td><tr>";
echo "</tr>";
}
} else {
echo "Nothing should be displayed";
}
?>
</table>
Im having an issue with fetch_array. I keep getting the return of record not found. With every thing that I have looked at it seems to me that this code should work. Sorry im new to php web development.
$JobNumber = NULL;
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$JobID = $_POST['jobid'];
include('pmconnect.php');
$sql="SELECT * FROM tblJobMaster WHERE JobNumber=" . $JobID;
$result=$conn->query($sql);
if ($result->num_rows==0)
{
echo "Record not found.<br>";
die(0);
}
$row=$result->fetch_array();
echo '<table style="text-align: left; width: 100%;" border="1" cellpadding="2" cellspacing="2">';
echo "<tbody>";
echo "<tr>";
echo '<td style="vertical-align: top; text-align: right;">Job Number:<br>';
echo "</td>";
echo '<td style="vertical-align: top;">' .$row[0] . '<br>';
echo "</td>";
echo '<td style="vertical-align: top; text-align: right;">Engineer:<br>';
echo '</td>';
echo '<td style="vertical-align: top;">' . $row[3] .'<br>';
echo '</td>';
echo "</tr>";
echo "<form action=pmAssignEngineer2.php method=post id=usrform>";
echo "<input type=hidden name=JobID value=" . $JobID . ">";
echo "<input type=submit value=\"Update\" name=lookup>";
echo "</td>";
echo "</tr>";
echo "</form>";
echo "</tbody>";
}
else
{
echo "<form action=pmAssignEngineer.php method=post>";
echo "<table border=2>";
echo "<tr>";
echo "<td>Job Number:</td>";
echo "<td><input type=text name=JobID></td>";
echo "</tr>";
echo "</table>";
echo "<input type=submit value=\"Lookup\" name=lookupQ><br>";
echo "</form>";
}
HTML elements are posted to $_POST if form method is post.
The element name is key and value is array value.
And the keys are case-sensitive.
In your case, your element has name JobID and you are referring jobid.
Obviously, the keys of $_POST do not match as jobid and JobID are different.
<input type=text name=JobID>
So, update the following line:
$JobID = $_POST['jobid'];
To
$JobID = $_POST['JobID'];
I want to post value in one row while doing loop in specific array by id, but when click on bottom then will post it in "response" row in all arrays of table!!
<?php
require_once('config.php');
$con->set_charset('utf8');
$query = "select * from contact order by id";
$result = mysqli_query($con,$query);
while($name = mysqli_fetch_array($result)){
echo '<table width="200" border="1" align="right">';
echo "<tr>";
echo "<td align='center'> <div align='center'> <b> $name[id] </b> </td>";
echo "<td align='center'> <div align='center'> <b> The number </b> </td>";
echo "</tr>";
echo "<tr>";
echo "<td align='center'> <div align='center'> <b> $name[ticketnumber] </b> </td>";
echo "<td align='center'> <div align='center'> <b> ticket number </b> </td>";
echo "</tr>";
echo "<tr>";
echo "<td align='center'> <b> $name[name] </b> </td>";
echo "<td align='center'> <b> name </b> </div> </td>";
echo "</tr>";
echo "<tr>";
echo "<td align='center'> <b> $name[phone] </b> </td>";
echo "<td align='center'> <b> phone </b> </td>";
echo "</tr>";
echo "<tr>";
echo "<td align='center'> <b> $name[email] </b> </td>";
echo "<td align='center'> <b> email </b> </td>";
echo "</tr>";
echo "<tr>";
echo "<td align='center'> <b> $name[subject] </b> </td>";
echo "<td align='center'> <b> subject </b> </td>";
echo "</tr>";
echo "<tr>";
echo "<td align='center'> <b> $name[response] </b> </td>";
echo "<td align='center'> <b> response </b> </td>";
echo "</tr>";
echo "<tr>";
echo ' <td align="center"> <form method="POST" action="showcontact.php">
<input type="text" name="response" height="50pt"/> <br/>
<input type="submit" value="send" name="send"> </form> </td>';
echo "<td align='center'> <b> answer </b> </td>";
echo "</tr>";
if(isset($_POST['response'])) {
$response = $_POST['response'];
$sql = ("UPDATE contact SET response = '$response' WHERE id= $name[id]");
$rst = mysqli_query($con,$sql);
if($rst){
echo "<td align='center'> <b> sent </b> </td>" ;
echo " <td align='center'> </td>";
} else {
echo "<td align='center'> <b> did not send </b> </td>";
echo " <td align='center'> </td>";
echo "</table>";
}
}
}
?>
Having everything in a loop makes it hard to read your code.
You need a contact id to attach a response to.
Here's a brief overview of how I'd do it:
loop through contacts
row begins
display contact info
display response form
row ends
end loop
response form:
give it a post method
add a hidden contact id field <--
response input (text)
submit button
Form processing logic:
if this is a post request, and we have an id and response:
do your SQL update
I'd place the form processing out the loop at the top of your script, or on another page.
<table width="770px" cellpadding="2" cellspacing="0" style="float: right;">
<tr>
<th width="20%">A</th>
<th width="20%">B</th>
<th width="20%">C</th>
<th width="20%">D</th>
<th colspan=2 width="20%">Actions</th>
</tr>
<tbody>
<?php
$sql = mysql_query("SELECT * FROM answers ORDER BY question_id DESC");
if(!$sql){
die( "Database selection failed: " . mysql_error());
}
while ($row = mysql_fetch_assoc($sql)) {
echo "<form method=\"post\" action=\"editQuestions.php\">";
echo "<tr height=\"30px\"> ";
for($ctrCell=1;$ctrCell<=4;$ctrCell++){
echo "<td >";
echo $row['answer'];
echo "</td>";
}
echo "<td style=\"border-left: solid 1px #00478F;\" class=\"action\"><input type=\"submit\" name=\"btnAction\" value=\"EDIT\"></td>";
echo "<td style=\"border-right: solid 1px #00478F;\" class=\"action\"><input type=\"submit\" name=\"btnAction\" value=\"DELETE\"></td>";
echo "</tr>";
echo "</form>";
}
?>
</tbody>
</table>
I've tried using JOIN before but it was doing the same thing. I only had one table, now I'm trying to use two tables. But still, I can't seem to figure out how to display the rows correctly, A,B,C,D for each question.
The images attached are what it looks like on the admin page of the website, and the other one is the database itself.
Its because of this code for($ctrCell=1;$ctrCell<=4;$ctrCell++){
Its better to predefined your data, build an array after executing a query
$rows = mysql_fetch_assoc($sql);
$data = [];
foreach($rows as $row){
$data[$row['question_id']][] = $row['answer'];
}
And just do this
echo "<form method=\"post\" action=\"editQuestions.php\">";
foreach ($data as $row) {
echo "<tr>";
foreach ($row as $answer) {
echo "<td>" . $answer . "</td>";
}
echo "</tr>";
}
echo "</form>";
Hope that helps.
I have a quick question, I'm trying to use a function to delete a certain movie from a database using checkboxes. I can add stuff to the database just fine, but can't seem to delete using the checkboxes. Thanks in advance for your help!
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
</head>
<body>
<?
$db = new PDO("mysql:host=localhost;dbname=armstrongpz", "armstrongpz", "12345");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>
<?
function deleteFromDatabase($db)
{
$deleteQuery = $db->prepare("SELECT * FROM movie");
$deleteQuery->execute();
foreach($deleteQuery->fetchAll() as $row)
{
if(array_key_exists($row['id'], $_POST))
{
$deleteQuery = $db->prepare('DELETE FROM movie WHERE movie.id='. $row['id']);
$deleteQuery->excute();
}
}
}
?>
<h2 style='text-align: center';>
Movie Collection Database
</h2>
<?
if($_SERVER['REQUEST_METHOD'] === "POST")
{
deleteFromDatabase($db);
if(isset($_POST['add']))
{
$TitleOfMovie = $_POST['TitleMovie'];
$StudioOfMovie = $_POST['StudioMovie'];
$RatingOfMovie = $_POST['mRating'];
$PubYearOfMovie = $_POST['PubYear'];
$IMDBRating = floatval($_POST['imdbRating']);
$RunTimeOfMovie = $_POST['RunTime'];
$addQuery = "INSERT INTO movie (id,title,studio,rating,pub_year,imdb_rating,run_time) VALUES(".'NULL'.", '$TitleOfMovie','$StudioOfMovie','$RatingOfMovie','$PubYearOfMovie', '$IMDBRating', '$RunTimeOfMovie')";
$statement = $db->prepare($addQuery);
$statement->execute();
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'>
<table align='center' border='0' cellpadding='5'>
<tbody>
<tr>
<td style='border-style: none'>
<input name='TitleMovie' size='50' type='text' width='35' value= 'Title'/>
</td>
<td style='border-style: none'>
<input name='StudioMovie' size='25' type='text' width='35' value= 'Studio Name'/>
</td>
<td>
<?
$rating = array(1=>'G', 'PG', 'PG-13', 'R', 'NC-17', 'Not Rated');
echo "<select size='1' selected='movieRating' name='mRating'>";
foreach($rating as $key=>$value)
{
echo "<option value=\"$key\">$value</option>\n";
}
echo "</select>\n";
?>
</td>
<td style='border-style: none'>
<input name='PubYear' size='10' type='text' width='35' value='Pub Year'/>
</td>
<td style='border-style: none'>
<input name='imdbRating' size='10' type='text' width='35' value='IMDB rating'/>
</td>
<td>
<input name='RunTime' size='10' type='text' width='35' value='Run time'/>
</td>
<td>
<input type="checkbox" name="add" value="Add" align="top"/>Add
</td>
</tr>
<div style="text-align:center">
<input type='submit' name='submit' value='Update Database'>
</div>
</tbody>
</table>
</form>
<table border ='1' align='center'>
<?
$arrayOfFieldNames = array("Title", "Studio", "Rating", "Pub. Year", "IMDB Rating", "Run time(min)");
echo "<tr>";
echo "<td>". $arrayOfFieldNames[0] . "</td>";
echo "<td>". $arrayOfFieldNames[1] . "</td>";
echo "<td>". $arrayOfFieldNames[2] . "</td>";
echo "<td>". $arrayOfFieldNames[3] . "</td>";
echo "<td>". $arrayOfFieldNames[4] . "</td>";
echo "<td>". $arrayOfFieldNames[5] . "</td>";
echo "<td>Delete</td>";
echo "</tr>";
?>
<?
$query = "SELECT * FROM movie";
$stmt = $db->prepare($query);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo "<tr>";
echo "<td>" .$row['title']. "</td>";
echo "<td>" .$row['studio']. "</td>";
echo "<td>" .$row['rating']. "</td>";
echo "<td>" .$row['pub_year']. "</td>";
echo "<td>" .$row['imdb_rating']. "</td>";
echo "<td>" .$row['run_time']. "</td>";
echo "<td><input type='checkbox' name='". $row['id'] . "' value='" . $row['id'] . "'>Delete</td>";
echo "</tr>";
}
?>
</table>
<?
if(isset($db))
{
$db= null;
}
?>
</body>