I am stuck on a script and need another pair of eyes to see if I am missing something. The script is for a bookshop. When a student number is in-putted and searched for the student is displayed with the books that he is suppose to get for each subject. The student, course and book data comes from a MySQL database.
This is all done with this script:
<?php
if (isset($_POST['submit'])){
$btnClick = $_POST['submit'];
switch($btnClick){
case "Logout" :
session_destroy();
header("location:index.php");
break;
case "Search" :
$Validate = $_POST['txtStud'] ;
$StudNr = ValidateTxt($Validate);
$showStud = findStud($StudNr);
$cid = $showStud[4];
$showBooks = findBooks($cid);
echo "<form action='issue_book.php' method='post'>";;
echo "<table class='table3'>";
echo "<tr>";
echo "<td>" . $showStud[0] . " " . $showStud[1] . " " . $showStud[2] ."</td>";
echo "</tr>";
echo "<tr><td></td><td>" . $showStud[3] . "</td></tr>";
$array_count = count($showBooks);
$num = 0;
while ($num != $array_count) {
$bookNum = $showBooks[$num]['bid'];
echo $bookNum . "<br>";
echo "<tr><td>" . $showBooks[$num]['bid'] . "</td>" . "<td>" . $showBooks[$num]['bname'] . "</td>" ;
echo "<td><input type='checkbox' name='booknum[]' value='<?php echo $bookNum; ?>'></td></tr>";
$num++;
}
echo "</table>";
echo "<br>";
echo "<table class = 'table3'>";
echo "<tr><td></td><td><input type='submit' name='submit' value='Issue'></td>
<td><input type='submit' name='submit' value='Clear'></td></tr>";
echo "</form>";
break;
case "Issue":
$mybooks = $_POST['booknum'];
$h = count($mybooks);
echo $h . "<br>";
print_r ($mybooks);
break;
}
}
?>
At the bottom of the dynamic created data there is 2 buttons. When I click on the Issue button I am presented with this data.
This comes from the code as it is in the script at this moment. I want to send the data from here to the database.
Array ( [0] => [1] => [2] => )
An empty array?? Not sure what happened to the names that I assigned each check box??
I tried to adapt my script according to this forum post Check box link
I am not sure where I am missing something.
This is because you have a syntax error here
echo "<td><input type='checkbox' name='booknum[]' value='<?php echo $bookNum; ?>'></td></tr>";
^php tags are opened ^
You are already printing your table inside php tags, you cannot open other tags
value='<?php echo $bookNum; ?>
This is why your array's values are empty but keys exists. You just need to concatenate
echo "<td><input type='checkbox' name='booknum[]' value='".$bookNum."'></td></tr>";
Related
I have an interesting challenge that I haven't been able to get my head around, I have a html table that is generated from php while loop and a mysql database, each generated row of this table has a button that allows someone to modify a row in a database, but whenever the button is pressed the action is performed across the whole generated table thanks to the while loop, and I don't know how to only get that row to perform an action.
Here is the code that I have so far in this section.
while ($submissionrow = mysqli_fetch_array($currentsubmissions)) {
if ($submissionrow[approved] === '1') {
$approvedtick = 'Approved';
}else {
$approvedtick = '';
}
if ($submissionrow[rejected] === '1') {
$rejectedsymbol = 'Rejected';
}else {
$rejectedsymbol = '';
}
if ($approvedtick == $blanksymbol && $rejectedsymbol == $blanksymbol) {
$inreviewmessage = 'In Review';
}else {
$inreviewmessage = '';
}
echo "<tr>";
echo "<td>" . $submissionrow[submission_ID] . "</td>";
echo "<td>" . $submissionrow[Username] . "</td>";
echo "<td>" . $currentyear . "</td>";
echo "<td>" . $submissionrow[CSEvent] . "</td>";
echo "<td>" . "";?>
<form method='post' action='<?php $_PHP_SELF ?>'>
<?php
echo "<button type='submit' name='$submissionrow[submission_ID]' id='accept_cs'>Approve</button>";
echo "<button type='submit' name='$submissionrow[submission_ID]' id='reject_cs'>Reject</button>";
?>
</form>
<?php echo "
" . "</td>";
echo "<td>" . $approvedtick . $rejectedsymbol . $inreviewmessage . "</td>";
if(isset($_POST['accept_cs'])) {
$accept_cs_sql = 'UPDATE CommunityService SET approved =1 WHERE submission_ID =' . $submissionrow[submission_ID];
printf($accept_cs_sql);
$retval = $dbcon->query("$accept_cs_sql");
echo "Updated Accepted Status successfully\n";
}
if(isset($_POST['reject_cs'])) {
$reject_cs_sql = 'UPDATE CommunityService SET rejected =1 WHERE submission_ID =' . $submissionrow[submission_ID];
printf($reject_cs_sql);
$retval = $dbcon->query("$reject_cs_sql");
echo "Updated Rejected Status successfully\n";
}
}
I can make the code submit the performed action, but it enacts this action on the ID of every row in the table, not just the row with that button that was pressed.
Thank you in advance
-jx
I am a beginner to web development and am just wondering how I can store the values of a table row and use them on another page.
At the moment I am retrieving values from a database and putting them into a table. Each row has a submit button which should take them to a pay.php page where it should list the details of the particular row that they selected to pay.
while($row = mysql_fetch_array($result))
{
echo "<td>" . $row['P_name'] . "</td>";
echo "<td>" . $row['P_amount'] . "</td>";
echo "<td>" . $row['P_dateStart'] . "</td>";
if(is_null($row['P_datePaid'])){
echo "<td>Not Paid</td>";
}
else{
echo "<td>" . $row['P_datePaid'] . "</td>";
}
if($row['P_status'] == 0){
echo "<td bgcolor='#FF0000'>" . $row['P_status'] . "</td>";
}
else{
echo "<td bgcolor='#00FF00'>" . $row['P_status'] . "</td>";
}
echo "<td><form name = 'viewMore' action = 'viewMore.php'><input type = 'submit' value = 'View More'></form></td>";
echo "<td><form name = 'pay' action = 'pay.php'><input type = 'submit' value = 'Pay'></form></td>";
echo "</tr>";
}
echo "</table>";
?>
You can add the record ID (assuming in my example that is P_id) in the form like this. I also added method='post' to avoid someone to try pay.php?id=... :
echo "<td><form name = 'pay' action = 'pay.php' method='post'>
<input type='hidden' name='id' value='" . $row['P_id'] . "'>
<input type = 'submit' value = 'Pay'></form></td>";
And then in pay.php using $_POST["id"] to query the table and retrieve the data.
You can google PHP Session
Here is an example.
session_start();
// store session data
$_SESSION['views']=1;
on another page you can use
echo "Pageviews=". $_SESSION['views'];
and I think your requirement is not releted to session.
Do you know how to use the form?
You should post the data of the certain line to the page you want.
And you can get the in _POST['XXXX'];
I'm a new member of StackOverflow, and although I've been using the website for a long time, it's my first time posting a question, in a hope that someone will be able to help me. I'll start by saying that my knowledge of PHP and MySQL is basic, but what I'm trying to do isn't too complex in my opinion, so hopefully I won't be asking for much. I've done a lot of prior research, but I just couldn't find the right answer.
In short, this is what I'm trying to do:
I've got an html form, which upon submission writes data to a database, and then publishes a table on a separate html page. With each successful submission a new table gets generated and published, while the old one gets pushed underneath. This all works fine, and I've also implemented pagination so that only 5 tables are visible per page.
What I'd like to be able to do is allow people to ONLY view/display results (tables) based on a specific criteria, in this case "rating", by selecting a rating from a drop-down on the page where tables are published. Rating is one of the fields in my form which gets submitted to a database and then published in one of the rows in a table.
Below is the code which publishes tables. Thanks in advance for your help!
<?php
include('dbconnect.php');
mysql_select_db("vtracker", $con);
$result = mysql_query("SELECT * FROM userdata");
$age = "Age:";
$rating = "Rating:";
$country = "From:";
$name = "Name:";
while($row = mysql_fetch_array($result))
{
echo "<table id='mft_table' cellspacing='0'>";
echo "<tbody>";
echo "<tr>";
echo "<td class='row1'>" .$name . " " . $row['personsname'] . "</td>";
echo "<td rowspan='4'>";
echo "<div class='mft_column'>" . $row['mft'] . "</div>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='row2'>" . $country . " " . $row['nationality'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='row3'>" . $age . " " . $row['personsage'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='row4'>" . $rating . " " . $row['rating'] . "</td>";
echo "</tr>";
echo "</tbody>";
echo "<br>";
echo "</table>";
}
?>
for both true and false use can add thid in your code:
if($_POST['rating_dropdown']!='')
{
$temp_rating = $_POST['rating_dropdown'];
$query=mysql_query("SELECT * FROM userdata WHERE rating = '$temp_rating'");
}
else
{
$query=mysql_query("SELECT * FROM userdata");
}
Dunno if this works, it's just a hinch. haha.
It will see if the rating is true(not null), if it's true it will echo the results.
while($row = mysql_fetch_array($result))
{
if ($rating)
echo "<table id='mft_table' cellspacing='0'>";
echo "<tbody>";
echo "<tr>";
echo "<td class='row1'>" .$name . " " . $row['personsname'] . "</td>";
echo "<td rowspan='4'>";
echo "<div class='mft_column'>" . $row['mft'] . "</div>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='row2'>" . $country . " " . $row['nationality'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='row3'>" . $age . " " . $row['personsage'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='row4'>" . $rating . " " . $row['rating'] . "</td>";
echo "</tr>";
echo "</tbody>";
echo "<br>";
echo "</table>";
}
}
Once the dropdown gets selected and posted to your display page, use this code:
$temp_rating = $_POST['rating_dropdown'];
mysql_query("SELECT * FROM userdata WHERE rating = '$temp_rating'");
Keep in mind, however, that you should be using PDO or mysqli extension, not the mysql extension. According to PHP's website:
This extension is deprecated as of PHP 5.5.0, and will be removed in
the future. Instead, the MySQLi or PDO_MySQL extension should be used.
See also MySQL: choosing an API guide and related FAQ for more
information.
I currently have 2 different sections for this program, the first half takes the users input from a web page and then transfers it over onto a PHP side which will access MySQL and display the requested information.
Example: If I enter AX12 for the ID it will display information for that ID which does infact exist, but if I enter AX13 (which doesn't) it will display blank information, so I'm wondering if someone can show me how I can validate this once the information has been transferred over onto the PHP side. So if it detects that the information you've submitted does not exist simply display a message saying "ID DOES NOT EXIST" or something along those lines.
Here's the code for the PHP side if you need it for more information.
<?php
$part_number = $_GET['txtInput'];
$part_description;
$units_on_hand;
$item_class;
$warehouse_number;
$unit_price;
$query;
$result_set;
$connection;
$record;
echo "<html>";
echo "<head>";
echo "<title>SQL Application</title>";
echo "<style type = 'text/css'>body{text-align: center; background-color: #CC3333; color: #660000; font-size: 30;}</style>";
echo "</head>";
echo "<body>";
echo "<center><h1>SQL Application</h1></center>";
echo "<br />";
echo "<br />";
echo "<br />";
$connection = #mysql_connect("localhost","m_stanicic","")
or die ("\n\n PROBLEM CONNECTING TO DATABASE! \n" . mysql_error() . "\n\n");
mysql_select_db("m_stanicicdb");
$query = "select * from part where part_number = '" . $part_number . "'";
$result_set = mysql_query($query)
or die ("\n\n PROBLEM WITH QUERY! . \n" . mysql_error() . "\n\n");
$record = mysql_fetch_assoc($result_set);
if($part_number == "")
{
//
}
else
{
$part_description = $record['part_description'];
$units_on_hand = $record['units_on_hand'];
$item_class = $record['item_class'];
$warehouse_number = $record['warehouse_number'];
$unit_price = $record['unit_price'];
echo "<center>";
echo "<table border='1' width=400 style ='table-layout:fixed' cellpadding='5' cellspacing='0'>";
echo "<col width = 200>";
echo "<col width = 200>";
echo "<tr>";
echo "<th colspan='2'>DETAILS OF THE PART YOU REQUESTED</th>";
echo "</tr>";
echo "<tr>";
echo "<td>part_description</td>";
echo "<td>" . $part_description . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>units_on_hand</td>";
echo "<td>" . $units_on_hand . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>item_class</td>";
echo "<td>" . $item_class . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>warehouse_number</td>";
echo "<td>" . $warehouse_number . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>unit_price</td>";
echo "<td>$" . $unit_price . "</td>";
echo "</tr>";
echo "</table>";
echo "</center>";
mysql_close($connection);
}
echo "<br />";
echo "<br />";
echo "<br />";
echo "<input type = 'button' value = 'RETURN' style = 'width: 75px; height: 75px;' onclick = \"javascript:window.location.href = 'jdpset1_4.html'\">";
echo "</body>";
echo "</html>";
You aren't validating anywhere that the result did return any data at all. Right after your call to mysql_query(), you should use mysql_num_rows() to see how many rows were returned by your query -- if mysql_num_rows($result_set) is zero, your query returned no data.
Notice how $part_number is never modified by mysql_query(), mysql_fetch_array() or any of those functions; so it will never be empty unless it started as such (rendering your current if almost useless).
You can check the output of your query $record...
if (count($record)==0) {
echo "the ID you entered does not exist! Try again...";
} else {
// code to output the part's details...
}
put the if (count... part instead of ...
if($part_number == "")
from your code i notice 2 things
$query = "select * from part where part_number = '" . $part_number . "'";
as your part number is a string, i recommend you to use LIKE not =
$query = "select * from part where part_number LIKE '" . $part_number . "'";
another is inspect your record is returning in multidimensional array like
$record = Array([0]=>array('part_description'=>A123...)).
then you must assign like so
$part_description = $record[0]['part_description'];
i hope it helps you
I have a list of user for a site, mainly using a unique ID for each members.
I need to have a button after each name to delete or modify but i can't find how to do it properly, here's the code !
$result = mysql_query("SELECT * FROM users ORDER BY ID_Usager");
while($row = mysql_fetch_array($result))
{
echo "<form method='post'>";
echo "<tr>";
echo "<td>" . $row['Pseudo'] . "</td>";
echo "<td>" . $tempID . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "<td>" . $row['Status'] . "<input type='submit' name='delete[" . $tempID . "]' value='Delete'> <input type='submit' name='modify[" . $tempID . "]' value='Modify'> </td>";
echo "</tr>";
echo "</form>";
}
(I have skipped some table code..)
So this show as :
Vincent 25625 [DELETE] [MODIFY]
So what I was think, when i click submit, it reload the page, i look if the Post Delete[] is empty, or modify is empty, ...
if(!empty($_POST['delete']))
{
$test[] = $_GET['delete[]']; ---- LINE 163 ERROR...
//Delete from blablabla userID = $test;
}
else
{
... check for modify then blablabla.
}
But its not working, i get an error.
Notice: Undefined index: delete[] in C:\wamp\www\VMAD\admin_usagers.php on line 163
The [] is the problem. It should be
$test = $_POST['delete'];