I have a form that allows users to update their client's transactions. This form comes populated with the information already listed for the transaction. I want to have a drop down list for the customer names so that they have to choose an existing customer.
My problem is that the sql to populate the answers with the existing information conflicts with the sql to bring up the customer names in the database.
Is there a way to do this? Here is the code I have that brings up the populated answers:
<table>
<?php
if (!isset ($p_submitval))
{
$sql = "SELECT crid, c.custid,
firstname, lastname, cramount, crdate
FROM databau7_thomand.customer
c inner join databau7_thomand.cashrec r on
c.custid = r.custid WHERE crid= " .
$p_crid;
$result = mysqli_query($con, $sql);
while($row =
mysqli_fetch_array($result))
echo "<h2>Update Customer
Information</h2>\n";
echo "<p><i>Please update the customer information.</i><br>\n";
echo "<form action=\"web4_transactioninput.php\" method=post>\n";
echo "<input type=\"hidden\" name=\"crid\" value = \"$p_crid\">\n";
echo "<table border=\"3\"> \n";
echo "<td align=\"right\"><b>Customer Name:
</td>";
echo "<td><select name=\"custid\"
class=\"dropdown\">";
echo "<option value=\"0\" selected>
".$row['firstname']." ".$row['lastname']."
</option>\"";
echo "<tr>\n<td align=\"right\">\n";
echo "<b>Amount:</b>\n";
echo "</td>\n<td>\n";
echo "<input type=\"text\"
name=\"cramount\"size=\"30\"
value=\"".$row['cramount']."\">\n";
echo "</td>\n</tr>\n";echo "<tr>\n<td
align=\"right\">\n";
echo "<b>Date:</b>\n";
echo "</td>\n<td>\n";
echo "<input type=\"date\"
name=\"crdate\"size=\"30\"
value=\"".$row['crdate']."\">\n";
echo "</td>\n</tr>\n";echo "<tr>\n<td
align=\"right\">\n";
}
while($row =
mysqli_fetch_array($result))
echo "</select>\n</td>\n</tr>\n";
echo "</table>\n";
Thank you for the help!
Remove ALLLLLL the echo "..."; for html portion as you don't need it. Just a recommendation. Much cleaner view and unnecessary
You also should be using prepared statements as inserting a variable a parameter is unsafe.
This is a really rough example, but check it out this way:
<table>
<?php
if (!isset ($p_submitval)){
$sql = "
SELECT crid
, c.custid
, firstname
, lastname
, cramount
, crdate
FROM databau7_thomand.customer c
JOIN databau7_thomand.cashrec r
on c.custid = r.custid
WHERE crid= ?;
";
$stmt = $con->prepare($sql);
$stmt->bind_param("s",$p_crid);
$stmt->execute();
$stmt->bind_result($crid,$custid,$fname,$lname,$cramount,$crdate);
$stmt->store_result();
?>
<h2>Update Customer
Information</h2>";
<p><i>Please update the customer information.</i><br>
<form action="web4_transactioninput.php" method=post>
<input type="hidden" name="crid" value = "<?php echo $p_crid;?>">
<table border="3">
<td align="right"><b>Customer Name: </td>
<td><select name="custid" class="dropdown">";
<?php
echo "<option value=\"0\" selected> $fname $lname</option>";
?>'
<tr><td align="right">
<b>Amount:</b>
</td><td>
<input type="text" name="cramount"size="30" value="<?php echo $cramount;?>">;
<?php
$stmt->close();
}
Related
i have made a form in php and inside form there is a loop that extracts multiple values from database(img,name,availability,..) for multiple books i have made a table to display those values to user and after displaying these data in table i have made an issue button inside loop so that every book has its issue button.
My problem is that i have to only retrieve id of that book for which user click issue button. i tried storing it in cookie but it send the id of the first book displayed then i tried get method but that results in sending the last book that is displayed on screen id. but i want is that it should send the id of book which is selected by user
display books
echo "<form action='issue.php' method='get'>";
while ($row= mysqli_fetch_array($result)) {
echo "<div id='img_div' style='background-color:#fff;'> ";
echo "<img src='books/".$row['image']."'>";
// echo "</div>";<div id='text'>
$isbn=$row['isbn'];
echo "<input type='hidden' name='isb' value='$isbn' />";
echo " <table>";
echo "<tr><td> NAME</td><td> ".$row['name']."</td></tr>";
echo "<tr><td> AVAILABILITY</td><td> ".$row['availabilty']."</td></tr>";
echo "<tr><td> CATEGORY</td><td> ".$row['category']."</td></tr>";
echo "<tr><td colspan='2'>
<button type='submit' name='issue'>issue</button></td></tr>";
echo "</table>";
echo "</div><br/>";
if (isset($_GET['issue'])) {
# code...
$bookid=$isbn;
setcookie("bid",$bookid);
if(!isset($_COOKIE['bid'])){
echo "COOKIE NOT SET";
}
else{
echo "COOKIE SET SUCCESSFULLY";
}
}
issue.php(in which i want to send id)
if(isset($_GET['issue'])){
$bookid=$_GET['isb'];
$dbser="localhost";
$use="[redacted]";
$pasw="[redacted]";
$db="[redacted]";
$con=mysqli_connect($dbser,$use,$pasw,$db);
mysqli_select_db($con,$db)or die("db not connected");
$userid=$_COOKIE['id'];
$id=$_SESSION['user']['username'];
$query = "select id from user_account where username='$id'";
$result=mysqli_query($con,$query);
$row= mysqli_fetch_assoc($result);
$uid=$row['id'];
echo "$uid";
echo "<br/>";
echo "$bookid";
$query = "INSERT INTO issue (bookid, userid)
VALUES ('$bookid', '$uid')";
mysqli_query($con, $query)or die(mysqli_error($con));
What you need is a way to select a single row then submit that with the form. That can be accomplished by adding a Radio button to your table inside the form. The user will check the radio button for the item they want then click the submit button.
Here is an example of what that code could look like for your page.
display books
<form action='issue.php' method='get'>
while ($row= mysqli_fetch_array($result)) {
$isbn=$row['isbn'];
echo " <table>";
echo "<tr>";
echo "<td><input type=\"radio\" name=\"optradio\" value=\"".$isbn."\"></td>";
echo "<td> NAME</td><td> ".$row['name']."</td>";
echo "<td> AVAILABILITY</td><td> ".$row['availabilty']."</td>";
echo "<td> CATEGORY</td><td> ".$row['category']."</td>";
echo "</tr>";
echo "</table>";
}
echo "<button type='submit' name='issue'>issue</button>";
echo "</form>";
Here is a HTML snippet so you can see what that PHP code would output in HTML. Click "Run snippet code" below to see the preview.
<form action='issue.php' method='get'>
<table>
<tr>
<td><input type="radio" name="optradio" value="1"></td>
<td> NAME</td>
<td> name1</td>
<td> AVAILABILITY</td>
<td> availability1</td>
<td> CATEGORY</td>
<td> category1</td>
</tr>
<tr>
<td><input type="radio" name="optradio" value="2"></td>
<td> NAME</td>
<td> name2</td>
<td> AVAILABILITY</td>
<td> availability2</td>
<td> CATEGORY</td>
<td> category2</td>
</tr>
</table>
<button type='submit' name='issue'>issue</button>
</form>
Then in issue.php you would look for $_GET['optradio'] to get the selected value.
if(isset($_GET['issue'])){
$bookid=$_GET['optradio'];
$dbser="localhost";
...
...
...
Hello so i am doing this school assigment where i have make a comment system corresponding to the post ID and i know that it looping three times but i gave it the post id. And i know that the postID is changeing all the time. i just have no idea how to fix this bug any ideas?
<?php require_once("menu.php");
$connection = connectToMySQL();
$selectPostQuery = "SELECT * FROM (SELECT * FROM `tblposts` ORDER BY id DESC LIMIT 3) t ORDER BY id DESC";
$result = mysqli_query($connection,$selectPostQuery)
or die("Error in the query: ". mysqli_error($connection));
while ($row = mysqli_fetch_assoc($result))
{
$postid = $row['ID'];
if (!empty($_POST['comment']) ) #To insert new comments in the database
{
$comment = $_POST['comment'];
$userid = $_SESSION['userID'];
$insertCommentQuery = "INSERT INTO `tblcomments` (`Content`,`UserID`,`PostID`,`Timestamp`) VALUES ('$comment','$userid','$postid',CURRENT_TIMESTAMP)";
$resultComment = mysqli_query($connection, $insertCommentQuery)
or die("Error in the query: ". mysqli_error($connection));
}
echo "<div class=\"wrapper\">";
echo "<div class=\"titlecontainer\">";
echo "<h1>$row[Title]</h1>";
echo "</div>";
echo "<div class=\"textcontainer\">";
echo "<span>$row[Content]</span>";
echo "</div>";
if (!empty($row['ImagePath'])) #This will check if there is an path in the textfield
{
?>
<div class="imagecontainer">
<img src="images/<?php echo "$row[ImagePath]"; ?>">
</div>
<?php
}
echo "<div class=\"timestampcontainer\">";
echo "<b>Date posted :</b>$row[TimeStamp] ";
echo "<b>Author :</b> Admin";
echo "</div>";
#Selecting comments corresponding to the post
$selectCommentQuery = "SELECT * FROM `tblcomments` LEFT JOIN `tblusers` ON tblcomments.userID = tblusers.ID WHERE tblcomments.PostID ='$postid'";
$commentResult = mysqli_query($connection,$selectCommentQuery)
or die ("Error in the query: ". mysqli_error($connection));
while ($commentRow = mysqli_fetch_assoc($commentResult))
{
echo "<div class=\"commentcontainer\">";
echo "<div class=\"commentusername\"><h1>Username :$commentRow[Username]</h1></div>";
echo "<div class=\"commentcontent\">$commentRow[Content]</div>";
echo "<div class=\"commenttimestamp\">$commentRow[Timestamp]</div>";
echo "</div>";
}
if (!empty($_SESSION['userID']) )
{
echo "<form method=\"POST\" action=\"\" class=\"post-frm\">";
echo "<label>New Comment</label>";
echo "<textarea id=\"comment\" name=\"comment\"> </textarea>";
echo "<input id=\"submit\" type=\"submit\" name =\"submit\" class=\"button\"/>" ;
echo "</form>";
}
echo "</div>";
echo "<br /> <br /><br />";
}
require_once("footer.php") ?>
Well, that's exactly what your script does. It queries all posts, loops through them, and then performs an insert for all of them. To fix this, store the id of the post in the comment form. When you post the form, insert just a single comment and use the id in the form.
That could look something like this:
<?php
if (array_key_exists('postid', $_POST))
{
$postid = $_POST['postid'];
$comment = $_POST['comment'];
// Perform a single insert here, and use $postid and $comment.
}
// Then, start rendering the page:
require_once(menu.php);
$connection = connectToMySQL();
$selectPostQuery = SELECT * FROM (SELECT * FROM `tblposts` ORDER BY id DESC LIMIT 3) t ORDER BY id DESC;
$result = mysqli_query($connection,$selectPostQuery)
or die(Error in the query: . mysqli_error($connection));
while ($row = mysqli_fetch_assoc($result))
{
$postid = $row['ID'];
// Render the post itself here.
?>
<div class="wrapper">;
<div class="titlecontainer">;
<h1><?=$row['Title']?></h1>;
</div>;
<div class="textcontainer">;
<span><?=$row['Content']?></span>;
</div>;
<?php
// Render a comment form for each post (is that what you did?)
if (!empty($_SESSION['userID']) )
{?>
<form method=POST action= class=post-frm>
<label>New Comment</label>
<textarea id=comment name=comment></textarea>
<input type=hidden name=postid value=<?=$postid?>/>
<input id=submit type=submit name =submit class=button/>
</form>
<?}
}
Most of your code is the same, only the processing of the post data is now done before the loop.
Otherwise, I just fixed some small syntactic things (and maybe introduced new ones, I haven't tested it).
Also, I took the HTML out of echoes. It's a matter of taste, of course, but experience has taught me that big chunks of HTML in echo statements isn't very readable or maintainable. Rather just close the PHP tags, output the raw HTML and echo only the variables in it. You can use the short notation for that: <?= $value ?>, which basically means <?php echo $value ?>.
This form is a search form which allows the user to search for an event using the Venue and category fields which are scripted as dropdown boxes and the Price and event title as user input text boxes, as shown via the code if a keyword is entered which matches the fields on the database it should output all the related information for that event if any matches have been made on either search fields, the tickboxes allow the user to identify what criteria they would like to search with, if the tickbox field hasn't been checked then the SQL enquiry will not search for keywords with that corresponding field.
The issue is, it all seems to work fine except no results seem to show up for the Venue and Category fields if they was solely used to search for an event. But if I choose another field everything is outputting correctly including the venue and Category field.
DATABASE: http://i.imgur.com/d4uoXtE.jpg
HTML FORM
<form name="searchform" action ="PHP/searchfunction.php" method = "post" >
<h2>Event Search:</h2>
Use the Check Boxes to indicate which fields you watch to search with
<br /><br />
<h2>Search by Venue:</h2>
<?php
echo "<select name = 'venueName'>";
$queryresult2 = mysql_query($sql2) or die (mysql_error());
while ($row = mysql_fetch_assoc($queryresult2)) {
echo "\n";
$venueID = $row['venueID'];
$venueName = $row['venueName'];
echo "<option value ='$venueName'";
echo ">$venueName</option>";
}# when the option selected matches the queryresult it will echo this
echo "</select>";
echo" <input type='checkbox' name='S_venueName'>";
mysql_free_result($queryresult2);
mysql_close($conn);
?>
<br /><br />
<h2>Search by Category:</h2>
<?php
include 'PHP/database_conn.php';
$sql3 ="SELECT catID, catDesc
FROM te_category";
echo "<select name = 'catdesc'>";
$queryresult3 = mysql_query($sql3) or die (mysql_error());
while ($row = mysql_fetch_assoc($queryresult3)) {
echo "\n";
$catID = $row['catID'];
$catDesc = $row['catDesc'];
echo "<option value = '$catDesc'";
echo ">$catDesc </option>";
}
echo "</select>";
mysql_free_result($queryresult3);
mysql_close($conn);
?>
<input type="checkbox" name="S_catDes">
<br /><br />
<h2>Search By Price</h2>
<input type="text" name="S_price" />
<input type="checkbox" name="S_CheckPrice">
<br /><br />
<h2>Search By Event title</h2>
<input type="text" name="S_EventT" />
<input type="checkbox" name="S_EventTitle">
<br /><br />
<input name="update" type="submit" id="update" value="Search">
</form>
PHP CODE THAT DEALS WITH PROCESSING THE FORM DATA
<?php
include 'database_conn.php';
$venuename = $_POST['venueName']; //this is an integer
$catdesc = $_POST['catdesc']; //this is a string
$Price = $_POST['S_price'];
$EventT = $_POST['S_EventT'];
#the IF statements state if the tickbox is checked then search with these enquires
if (isset($_POST['S_VenueName'])) {
$sql = "SELECT * FROM te_venue WHERE venueName= '$venuename'";
}
if (isset($_POST['S_catDes'])) {
$sql = "SELECT * FROM te_category WHERE catID= '$catdesc'";
}
if (isset($_POST['S_CheckPrice'])) {
$sql = "SELECT * FROM te_events WHERE (eventPrice LIKE '%$Price%')";
}
if (isset($_POST['S_EventTitle'])) {
$sql = "SELECT * FROM te_events WHERE (eventTitle LIKE '%$EventT%')";
}
$queryresult = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_assoc($queryresult))
{
echo "Event Title: "; echo $row['eventTitle'];
echo "<br />";
echo "Event Description: "; echo $row['eventDescription'];
echo "<br />";
echo "Event Venue "; echo "$venuename";
echo "<br />";
echo "Event Category "; echo "$catdesc";
echo "<br />";
echo "Event Start Date "; echo $row['eventStartDate'];
echo "<br />";
echo "Event End Date "; echo $row['eventEndDate'];
echo "<br />";
echo "Event Price "; echo $row['eventPrice'];
echo "<br /><br />";
}
mysql_free_result($queryresult);
mysql_close($conn);
?>
Try using atleast MySQLi instead of deprecated MySQL. You can try this:
database_conn.php:
<?php
/* ESTABLISH YOUR CONNECTION. REPLACE THE NECESSARY DATA BELOW */
$con=mysqli_connect("YourHost","YourUsername","YourPassword","YourDatabase");
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}
?>
HTML Form:
<html>
<body>
<?php
include 'PHP/database_conn.php';
$sql2="SELECT venueID, venueName FROM te_venue"; /* PLEASE REPLACE THE NECESSARY DATA */
echo "<select name = 'venueName'>";
$queryresult2 = mysqli_query($con,$sql2);
while($row = mysqli_fetch_array($queryresult2)) {
echo "\n";
$venueID = mysqli_real_escape_string($con,$row['venueID']);
$venueName = mysqli_real_escape_string($con,$row['venueName']);
echo "<option value ='$venueName'>";
echo $venueName."</option>";
} /* when the option selected matches the queryresult it will echo this ?? */
echo "</select>";
echo "<input type='checkbox' name='S_venueName'>";
?>
<br><br>
<h2>Search by Category:</h2>
<?php
$sql3 ="SELECT catID, catDesc FROM te_category";
echo "<select name = 'catdesc'>";
$queryresult3 = mysqli_query($con,$sql3);
while($row = mysqli_fetch_array($queryresult3)) {
echo "\n";
$catID = mysqli_real_escape_string($con,$row['catID']);
$catDesc = mysqli_real_escape_string($con,$row['catDesc']);
echo "<option value = '$catDesc'>";
echo $catDesc."</option>";
}
echo "</select>";
?>
<input type="checkbox" name="S_catDes">
<br><br>
<h2>Search By Price</h2>
<input type="text" name="S_price" />
<input type="checkbox" name="S_CheckPrice">
<br><br>
<h2>Search By Event title</h2>
<input type="text" name="S_EventT" />
<input type="checkbox" name="S_EventTitle">
<br><br>
<input name="update" type="submit" id="update" value="Search">
</form>
</body>
</html>
PHP:
<?php
include 'database_conn.php';
$venuename = mysqli_real_escape_string($con,$_POST['venueName']); /* this is an integer */
$catdesc = mysqli_real_escape_string($con,$_POST['catdesc']); /* this is a string */
$Price = mysqli_real_escape_string($con,$_POST['S_price']);
$EventT = mysqli_real_escape_string($con,$_POST['S_EventT']);
/* SHOULD PRACTICE USING ESCAPE_STRING TO PREVENT SOME OF SQL INJECTIONS */
/* the IF statements state if the tickbox is checked then search with these enquires */
if (isset($_POST['S_VenueName'])) {
$sql = "SELECT * FROM te_venue WHERE venueName= '$venuename'";
}
if (isset($_POST['S_catDes'])) {
$sql = "SELECT * FROM te_category WHERE catID= '$catdesc'";
}
if (isset($_POST['S_CheckPrice'])) {
$sql = "SELECT * FROM te_events WHERE (eventPrice LIKE '%$Price%')";
}
if (isset($_POST['S_EventTitle'])) {
$sql = "SELECT * FROM te_events WHERE (eventTitle LIKE '%$EventT%')";
}
$queryresult = mysqli_query($con,$sql);
while ($row = mysqli_fetch_array($queryresult))
{
echo "Event Title: "; echo $row['eventTitle'];
echo "<br />";
echo "Event Description: "; echo $row['eventDescription'];
echo "<br />";
echo "Event Venue "; echo "$venuename";
echo "<br />";
echo "Event Category "; echo "$catdesc";
echo "<br />";
echo "Event Start Date "; echo $row['eventStartDate'];
echo "<br />";
echo "Event End Date "; echo $row['eventEndDate'];
echo "<br />";
echo "Event Price "; echo $row['eventPrice'];
echo "<br /><br />";
}
mysqli_close($conn);
?>
What if user checks all the check box? What would happen is, the last condition will be used. The first three conditions will be overwritten by the last condition.
If you use ELSE IF in those conditions, the first condition will be implemented.
My advice is to use radio button instead of check box and hope you gets the idea along the way.
Have you tried printing out your $sql query for debugging?
Try <input type="checkbox" name="S_catDes" value="checked">.
From memory checkboxes need a value field but I could be wrong. Hope this helps.
Hi im doing my final project this year. I need to input data into a database together with month and day. However, month and day is recalled from another database. Some of codes does not work. Btw im using php and mysql.
there are 3 pages.
This is the first page: pilih.php
<html>
<form action="pilih_process.php" method="POST">
<table>
<tr>
<td>Month:</td>
<td><select name="month"/>
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
</select>
</td>
</tr>
<tr>
<td>Day:</td>
<td><select name="day"/>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
</td>
</tr>
<tr>
<td>Group:</td>
<td><select name="group"/>
<option>1</option>
<option>2</option>
</select>
</td>
</tr>
<tr>
<td><input type="submit" name="submit" value="submit"/></td>
</tr>
</table>
</form>
</html>
This is the second page: pilih_process.php
<html>
<?php
include ('connect_database.php')
$query1 = mysql_query("SELECT Student_ID, Student_Name FROM student_details WHERE Group_ID=$group");
$month = $_POST['month'];
$day = $_POST['day'];
$group = $_POST['group'];
echo "<table>";
echo "<tr>";
echo "<td>Month: $month</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Day: $day</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Group: $group</td>";
echo "</tr>";
echo "</table>";
echo "<form action='pilih_process2.php' method='POST'>";
echo "<table>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Name</th>";
echo "</tr>";
while ($row = mysql_fetch_array($query1) )
{
echo "<tr>";
echo '<td>' . $row['Student_ID'] . '</td>';
echo '<td>' . $row['Student_Name']. '</td>';
echo "<td><input type='checkbox' name='status[]' value='Expert'/></td>";
echo "<td><input type='checkbox' name='status[]' value='Intermediate'/></td>";
echo "<td><input type='checkbox' name='status[]' value='Amature'/></td>";
echo "<td><input type='checkbox' name='status[]' value='Noob'/></td>";
echo "</tr>";
}
echo "<tr>";
echo "<td><input type='submit' name='submit' value='Submit'/></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
?>
</html>
This is the third page: pilih_process2.php
<html>
<?php
include ('connect_database.php');
$month = $_SESSION['month']; //From First page: pilih.php
$day = $_SESSION["day"]; //From First page: pilih.php
$student_id = $_SESSION["Student_ID"]; //From Second page: pilih_process.php
$student_name = $_SESSION["Student_Name"]; //From Second page: pilih_process.php
$group = $_POST['group']; //From First page: pilih.php
$status = $_POST['status']; //From Second page: pilih_process.php
mysql_query("INSERT into mencuba(Month_ID, Day_ID, Student_ID, Student_Name, Group_ID, Status) VALUES('$month', '$day', '$student_id', '$student_name', '$group', '$status')");
/*
$status_test = $_GET['status']; //I DONT KNOW WHETHER THIS ARRAY METHOD IS CORRECT
for($i=0; $i < count($checked); $i++){
echo "Selected " . $checked[$i] . "<br/>";
}
*/
?>
Here how it goes:
From the First page, the user have to choose which month, day and group and submit.
On the Second page, it displays the Student ID, Student Name, Group and Status which is based on what group.
For the status, the user has to pick one and submit.
On the third page. The data Month_ID, Day_ID, Student_ID, Student_Name, Group_ID and Status will be inserted into the database.
PROBLEMS:
When I insert the data, only Month_ID, Day_ID, Group_ID and Status is inserted into the database. I was unable to call the variable Student_ID and variable Student_Name.
it only insert one data. if there are 100 data to enter obviously ihave to use array which i do not really sure how to use.
We are taught simple PHP and MySql
As noted in other answers, there are a few things going on here.
You are trying to use the variable $group before you've set it
You are using raw POST data in a query - this is very very bad practice - even if it's a project you'll score better for doing it better. At the very least wrap that in mysql_real_escape_string so it looks like you tried.
$group = mysql_real_escape_string($_POST['group']);
As for the main issue:
You need to pass the first forms parameter to the second form. Just putting them in the table will not save them. Pass them as hidden form values
echo "<input type="hidden" name="group" value="'.$group.'" />";
Beyond that, if you expect to have multiple results for your query (ie: multiple people from one group) then you need to change your logic again because each one will just be overriding the form values from the one before.
You place $group in your query before you initiate it. you must initiate it first and then place it to query.
like this :
$month = $_POST['month'];
$day = $_POST['day'];
$group = $_POST['group'];
$query1 = mysql_query("SELECT
Student_ID, Student_Name
FROM student_details
WHERE Group_ID=$group");
About status thing
you have to use select instead of checkbox , if you want to give multiple select use another name property for each field in your form.
I'm still making the orderform, I've been working on the "save changes function".
By this I mean, if people make changes to the table, they can click on a button and it will update the database (where I get my data from).
Link to the image.
"Bestelling toevoegen" is the adding form, if I fill in the form, and click on "Toevoegen" it will display a new record to the table above.
The problem is, I can't seem to save the changes (Wijzigigen opslaan) of "Status".
Here's the full code:
$con=mysqli_connect("localhost","root","","bestelformulier");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM overzicht");
echo "<form method='post'>";
echo "<table align='center' width='700px' border='2'>
<tr>
<th>Ordernr</th>
<th>Klantnaam</th>
<th>Productnaam</th>
<th>ProductID</th>
<th>Status</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
$status = $row['status'];
$ordernr = $row['ordernr'];
echo "<tr>";
echo "<td>" . $row['ordernr'] . "</td>";
echo "<td width='150px'>" . $row['klantnaam'] . "</td>";
echo "<td width='200px'>" . $row['productnaam'] . "</td>";
echo "<td>" . $row['productid'] . "</td>";
echo "<td><select>
<option>" . $row['status'] . "</option>";
if($row['status'] != "Niet besteld")
echo "<option>Niet besteld</option>";
if($row['status'] != "Besteld")
echo "<option>Besteld</option>";
if($row['status'] != "Onderweg naar hoofdlocatie")
echo "<option>Onderweg naar hoofdlocatie</option>";
if($row['status'] != "Onderweg naar vestiging")
echo "<option>Onderweg naar vestiging</option>";
if($row['status'] != "Ontvangen")
echo "<option>Ontvangen</option>";
echo "</select></td>";
echo "</tr>";
}
echo "<tr>";
echo "<td></td><td></td><td></td><td></td>";
echo "<td><input type='submit' name='wijzigen' value='Wijzigingen Opslaan'/></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
if(isset($_POST['wijzigen'])) {
$query = mysqli_query("UPDATE overzicht SET status=$status WHERE ordernr=$ordernr");
}
mysqli_close($con);
//Table Toevoegen
$con=mysqli_connect("localhost","root","","bestelformulier");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo "<br/><br/><br/>";
echo "<h5>Bestelling Toevoegen</h5>";
echo "<form method='post'>";
echo "<table width='700px' border='1'>
<tr>
<th>klantnaam</th>
<td><input type='text' name='klantnaam'/></td>
</tr>
<tr>
<th>Productnaam</th>
<td><input type='text' name='productnaam'/></td>
</tr>
<tr>
<th>productid</th>
<td><input type='text' name='productid'/></td>
</tr>
<tr>
<th>Status</th>
<td>
<select name='status'>
<option value='Niet besteld'>Niet besteld</option>
<option value='Besteld'>Besteld</option>
<option value='Onderweg naar hoofdlocatie'>Onderweg naar hoofdlocatie</option>
<option value='Onderweg naar vestiging'>Onderweg naar vestiging</option>
<option value='Ontvangen'>Ontvangen</option>
<select>
</td>
</tr>
<tr>
<td></td>
<td><input type='submit' name='toevoegen' value='Toevoegen'/></td>
</tr>";
echo "</table>";
$klantnaam = $_POST['klantnaam'];
$productnaam = $_POST['productnaam'];
$productid = $_POST['productid'];
$status= $_POST['status'];
if(isset($_POST['toevoegen'])) {
$query = mysqli_query($con,"INSERT INTO overzicht (klantnaam, productnaam, productid, status)
VALUES ('$klantnaam', '$productnaam', '$productid', '$status')");
$current_url = (empty($_SERVER['HTTPS']) ? "http://" : "https://") . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
header ('Location: ' . $current_url);
exit ();
}
echo "</form>";
mysqli_close($con);
Hope I made it all clear for you, if not I will reply as soon as possible.
Thanks in advance!
You are not reading the status from the $_POST variable.
You are reading $status from the database:
$status = $row['status'];
and then you are setting it again
$query = mysqli_query("UPDATE overzicht SET status=$status WHERE ordernr=$ordernr");
So you set it to the same value as it already is.
Add a $status = $_POST['status']; before the query. Also, you are not reading $ordernr from $_POST, so you are always setting the status to the last item read from your database.
You are also missing single quotes in the query, it should be status='$status'. Last, but not least, take a look at prepared statements. Right now your code is vulnarable to SQL injections.
Edit:
You have to rethink the form of your list. Your select elements don't have a name, so it can't be transmitted to the server at all. Also, you have to provide the ID of the order number for each order ... either you make a form for each order (I'd recommend that), or you use arrays for your form element names (for example: <select name="status[$ordernr]">, simplified).
Why in the universe you have assigned $status = $row['status']; and not using $status in your while Loop.
Second you are using single quotes around variables in the insert query, you don't have to do this, else mysqli will take them as string instead of variables, put the variables in double quotes and query in single.
If this doesn't help, try printing the post data. Also you haven't given name to your forms. Please give forms some name and to your form elements as well.
This may help.