How to display NULL or N/A in empty table data - php

Empty spaces in phpMyAdmin seems to print out part of the closing table data tag </td> in my php file. I want to either leave the space blank or have something such as N/A to represent it being empty.
Here is my table display of the users in my php file.
Here is my users table in phpMyAdmin.
Here is my file causing the problem.
<?php
mysql_connect('localhost','root','');
mysql_select_db('project');
if (isset($_POST['update'])){
$UpdateQuery = "UPDATE users SET id='$_POST[id]', username='$_POST[username]', password='$_POST[password]', first_name='$_POST[first_name]', last_name='$_POST[last_name]', email='$_POST[email]'
WHERE id='$_POST[hidden]'";
mysql_query($UpdateQuery, mysql_connect('localhost','root',''));
};
if (isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM users WHERE id='$_POST[hidden]'";
mysql_query($DeleteQuery, mysql_connect('localhost','root',''));
};
if (isset($_POST['add'])){
$AddQuery = "INSERT INTO users (id, username) VALUES ('$_POST[uid]','$_POST[uusername]','$_POST[upassword]','$_POST[ufirst_name]','$_POST[ulast_name]','$_POST[uemail]')";
mysql_query($AddQuery, mysql_connect('localhost','root',''));
};
$sql = "SELECT * FROM users";
$records = mysql_query($sql);
//session_start();
//if(!isset($_SESSION["id"])){
// header("location:main.html");
//} else {
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Table Display (Allows instructor to add students, delete, and update id and names)
</title>
<link rel="stylesheet" href="main.css">
</head>
<body>
<table width="25%" border="1" cellpadding="1" cellspacing="1" align="center">
<tr>
<th>ID</th>
<th>Username</th>
<th>Password</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th></th>
<th></th>
<th></th>
</tr>
<?php
while ($users = mysql_fetch_assoc($records)) {
echo "<form action=displayusers.php method=post>";
echo "<tr>";
echo "<td>"."<input type=text name=id value=".$users['id']." </td>";
echo "<td>"."<input type=text name=username value=".$users['username']." </td>";
echo "<td>"."<input type=text name=password value=".$users['password']." </td>";
echo "<td>"."<input type=text name=first_name value=".$users['first_name']." </td>";
echo "<td>"."<input type=text name=last_name value=".$users['last_name']." </td>";
echo "<td>"."<input type=text name=email value=".$users['email']." </td>";
echo "<td>"."<input type=hidden name=hidden value=".$users['id']." </td>";
echo "<td>"."<input type=submit name=update value=Update"." </td>";
echo "<td>"."<input type=submit name=delete value=Delete"." </td>";
echo "</tr>";
echo "</form>";
}//end while
echo "<form action=displayusers.php method=post>";
echo "<tr>";
echo "<td><input type=text name=uid></td>";
echo "<td><input type=text name=uusername></td>";
echo "<td><input type=text name=upassword></td>";
echo "<td><input type=text name=ufirst_name></td>";
echo "<td><input type=text name=ulast_name></td>";
echo "<td><input type=text name=uemail></td>";
echo "<td>"."<input type=submit name=add value=Add"." </td>";
echo "<td>";
echo "<td>";
echo "</form>";
?>
</table>
</body>
</html>
<?php
//} // end else statement
?>
Another question: How can I exclude the ServerAdmin in the displayed table in my php file. I am logged in as a teacher and shouldn't be allowed to change the ServerAdmin's information.
Thanks!

Related

HTML: putting a table in a form

I'm coding an html table that displays information from a MySql table. Each row is a series of input's so the values of the table can be easily updated.
Here's my current code:
<form action=index.php/component/studentmanagement/?task=update method=post>
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Program</th>
<th>Class</th>
</tr>
<?php
$db = JFactory::getDBO();
$query = "SELECT * FROM student_management_module";
$db->setQuery($query);
$rows = $db->loadObjectList();
foreach ($rows as &$row) {
echo "<tr>";
echo "<td>" . "<input type=text name=fullName id=name_val value=" .$row->name. "> </td>";
echo "<td>" . "<input type=text name=email id=email_val value=" .$row->email. "> </td>";
echo "<td>" . "<input type=text name=prog id=prog_val value=" .$row->program. "> </td>";
echo "<td>" . "<input type=text name=class id=class_val value=" .$row->class. "> </td>";
echo "<td class = 'headcol'> <input type=submit name=update class='btnupdate' value=update>";
echo "<td>" . "<input type=hidden name=hidden value=" .$row->student_id. "> </td>";
echo "</tr>";
}
?>
</table> </form>
But whenever I try submitting the updated values, they don't get pass to my update functions. Am I putting the table in the form correctly?
Thanks in advance, and I'll appreciate any help.
This is my update function:
<?php
$db = JFactory::getDBO();
$query = "UPDATE student_management_module SET name = '$_POST[fullName]', email = '$_POST[email]', program='$_POST[prog]', class='$_POST[class]' WHERE student_id='$_POST[hidden]'";
$db->setQuery($query);
$db->query();
?>
Try having the value of the name and id in quotes or double quote. I.e., id="class_val". In your case, since you have already inserted the td in a double quote, use single quote. So it will be "<td class='foo' id='foo' name='foo'></td>".

How do I update each row individually?

So I've been trying for days to figure out how I can update each row separately and i can't figure it out. this is the first time I've used php so sorry for the ugly code.
I can seem to pull the information from each row but the update wont work, however it sends all the data from all the rows in the address bar
<html>
<head>
<title>EDIT Patient data</title>
</head>
<body>
<br>
Edit Patients<br>
Patient Request<br>
Booked Patients
<?php
include 'connect.php';
$sql = "SELECT * FROM patients;";
$result = mysql_query($sql);
$num_of_row = mysql_num_rows($result);
if (isset($_GET['submit'])) {
$id = $_GET['did'];
$fname = $_GET['dfname'];
$lname = $_GET['dlname'];
$email = $_GET['demail'];
$phone = $_GET['dphone'];
$address = $_GET['daddress'];
$query = mysql_query("update patients set fName='$fname', lName='$lname', email='email', phone='$phone', address='$address' where patientID ='$id'");
}
echo "<table border=1>";
echo " <tr> <td> Patient ID </td> ";
echo "<td> First name </td>";
echo "<td> last name </td>";
echo "<td> email </td>";
echo "<td> phone number</td>";
echo "<td> address </td>";
echo "<td> date of birth </td> ";
echo "<td> update </td></tr>";
while($row = mysql_fetch_assoc($result))
{
echo "<tr> <form class='form' method='get'>";
echo "<td> <input type='text' name='did' value='".$row['patientID']."' readonly /></td>";
echo "<td> <input type='text' name='dfname' value ='".$row['fName']."' /></td>";
echo "<td> <input type='text' name='dlname' value = '".$row['lName']."' /></td>";
echo "<td> <input type='text' name='demail' value = '".$row['email']."' /></td>";
echo "<td> <input type='text' name='dphone' value ='".$row['phone']."' /></td>";
echo "<td> <input type='text' name='daddress' value ='".$row['address']."' /></td>";
echo "<td> <input type='text' name='ddob' value ='".$row['dob']."' readonly /></td>";
echo "<td> <input class='submit' type='submit' name='submit' value='update' /> </td> </tr>";
}
?>
</body>
</html>
Does your user have update permissions?
If not you need to use GRANT UPDATE ON *.* TO 'User'#'Host';
But you should use MySQLi as MySQL is deprecated. You should also look into Parameterized Queries and MySQL injection, It's worth knowing about!
If you want to update an individual row, you'll have to put a <form> around your while-loop, like this:
echo "<form action='' method='GET'>";
while ($row = mysql_fetch_assoc($result)) {
echo "<tr> <form class='form' method='get'>";
echo "<td> <input type='text' name='did' value='".$row['patientID']."' readonly /></td>";
echo "<td> <input type='text' name='dfname' value ='".$row['fName']."' /></td>";
echo "<td> <input type='text' name='dlname' value = '".$row['lName']."' /></td>";
echo "<td> <input type='text' name='demail' value = '".$row['email']."' /></td>";
echo "<td> <input type='text' name='dphone' value = '".$row['phone']."' /></td>";
echo "<td> <input type='text' name='daddress' value ='".$row['address']."' /></td>";
echo "<td> <input type='text' name='ddob' value ='".$row['dob']."' readonly /></td>";
echo "<td> <input class='submit' type='submit' name='submit' value='update' /> </td> </tr>";
}
echo "</form>";
The form will allow input fields to be submit according to the form's method (GET and POST).

Adding Confirmation box

I'm trying to add an "confirmation box" that says "Are you sure you want to update/delete question?" when I click on my "update" and/or "delete" buttons.
I've already tried adding
onclick="return confirm('Are you sure you want to logout?');"
<input type="submit" value="Add Question" onclick="return confirm('Are you sure you want to update/delete question?');">
I provided my two php files. Might not need the whole files but I hope you can understand my issue and help the best that y'all can. thank you.
File: questions_menu.php
<?php
$username = "root";
$password = "";
$hostname = "localhost";
$database = "basketball_database";
$table = "question_bank";
$con = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MYsql");
//echo "Connected to mysql<br>";
mysql_select_db("$database")
or die("Could not select Basketball_database");
//echo "Connected to database";
//update when update button pressed
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE $table SET question_description='$_POST[description]', option_a='$_POST[option1]', option_b='$_POST[option2]', option_c='$_POST[option3]', answer='$_POST[dropdown]', question_id='$_POST[questionID]' WHERE question_id='$_POST[hidden]'";
mysql_query($UpdateQuery, $con);
};//end of if statement
//delete when delete button pressed
if(isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM $table WHERE question_id='$_POST[hidden]'";
mysql_query($DeleteQuery, $con);
};//end of if statement
$mysql = "SELECT * FROM $table";
$mydata = mysql_query($mysql,$con);
//create table
echo "<table border=1
<tr>
<th>Question ID</th>
<th>Question Description</th>
<th>Option 1</th>
<th>Option 2</th>
<th>Option 3</th>
<th>Answer</th>
<th>Picture</th>
<th>Video</th>
</tr>";
//insert data into rows
while($records = mysql_fetch_array($mydata)){
echo "<form action=questions_menu.php method=post>";
echo "<tr>";
echo "<td>"."<input type=text name=questionID size=10 value=".$records['question_id']." </td>";
echo "<td>"."<textarea name=description rows=2 cols=25>".$records['question_description']."</textarea>"."</td>";
echo "<td>"."<input type=text name=option1 size=18 value=".$records['option_a']." </td>";
echo "<td>"."<input type=text name=option2 size=15 value=".$records['option_b']." </td>";
echo "<td>"."<input type=text name=option3 size= 15 value=".$records['option_c']." </td>";
echo "<td>"."<input type=text name=answer size=15 value=".$records['answer']." </td>";
echo "<td>"."<input type=hidden name=hidden value=".$records['question_id']." </td>";
//update button
echo "<td>"."<input type=submit name=update value=Update onclick=return confirm(Are you sure you want to update/delete question?)"." </td>";
//delete button
echo "<td>"."<input type=submit name=delete value=Delete onclick=return confirm(Are you sure you want to update/delete question?)"." </td>";
echo "</tr>";
echo "</form>";//end form
} echo "</table>";
mysql_close();
?> <!-- End of php code-->
You need to close input tag at following line:
echo "<td>"."<input type=submit name=update value=Update onclick='return confirm(\"Are you sure you want to update/delete question?\")'>"." </td>";
---------------------------------------------------------------------------------------------------------------------------------------^
And you need to use put confirm code in single quotes as shown above. And also note the escape sequence used for message text.

Values not Posted in the Database

I want to take attendance of a particular class and store the values in database.
I have used INNER JOIN to get the data from two tables and used those tables values in a form name attendance.
Now once i take attendance using the form i want to store that values in my database so i created another file name insertattendance.php.
The problem is it shows undefined index variables. ex.undefined index classid ..etc
so i tried using it in *if(isset($_POST['submit'])* There is no erros but the values are not posted.
My doubt is since iam using the values of my old tables is it showing error?.
Tell me how can i do this?
attendance.php
<html>
<head>
<title>grade1</title>
</head>
<body>
<table border="1" cellspacing="1" cellpadding="1" width="200" height="200">
<tr>
<th>classid</th>
<th>studentid</th>
<th>teacherid</th>
<th>locid</th>
<th>date</th>
<th>flag</th>
<th>comments</th>
</tr>
<?php
include 'conn.php';
$query = "(SELECT a.classid, a.fname, b.teacherid, c.locid
FROM class_master c JOIN student_master a
ON c.classid = a.classid JOIN teacher_link b
ON c.classid = b.classid
WHERE c.classid = 'grade1' )";
$result = mysql_query($query);
$i=1;
while( $row = mysql_fetch_array($result))
{
echo "<form action=insertattend.php method=POST>";
echo "<tr>";
echo "<td>" . "<input name=classid[$i] type=text value=" .$row['classid']." </td>";
echo "<td>" . "<input name=fname[$i] type=text value=" .$row['fname']." </td>";
echo "<td>" . "<input name=teacherid[$i] type=number value="
.$row['teacherid']." </td>";
echo "<td>" . "<input type=number name=locid[$i] value=" .$row['locid']." </td>";
echo "<td>" . "<input name=date[$i] type=date value='date'></td>";
echo "<td>" . "<input type=radio id=attend name=attend[$i] value='present'>";?>P
<?php echo "<input type=radio id=attend name=attend[$i] value='absent'>";?>A
<?php
echo"</td>";
echo "<td><input name=comment type=comment[$i] row=3 column=5></td>";
echo "</tr>";
$i++;
}
?>
</table>
<input type="submit" value="submit">
</form>
</body>
</html>
Here is my Insertattendance.php code
<?php
if (isset($_POST['submit'])){
include 'conn.php';
$clnm = mysql_real_escape_string($_POST['classid']);
$stfn = mysql_real_escape_string($_POST['fname']);
$dt = mysql_real_escape_string($_POST['date']);
$fg = mysql_real_escape_string($_POST['attend']);
$tid = mysql_real_escape_string($_POST['teacherid']);
$lid = mysql_real_escape_string($_POST['locid']);
$cmt = mysql_real_escape_string($_POST['comment']);
$inquery =("INSERT INTO attendance(classid, studentid, dateid, flag, teacherid,
locid, comments) VALUES('$clnm', '$stfn', '$dt', '$fg', '$tid', '$lid', '$cmt')");
mysql_query($inquery, $dbconnection);
echo "<br>";
echo "values inserted successfully!!!!";
mysql_close($dbconnection);
};
?>
Fix your html code:
<html>
<head>
<title>grade1</title>
</head>
<body>
<table border="1" cellspacing="1" cellpadding="1" width="200" height="200">
<tr>
<th>classid</th>
<th>studentid</th>
<th>teacherid</th>
<th>locid</th>
<th>date</th>
<th>flag</th>
<th>comments</th>
</tr>
<?php
include 'conn.php';
$query = "(SELECT a.classid, a.fname, b.teacherid, c.locid
FROM class_master c JOIN student_master a
ON c.classid = a.classid JOIN teacher_link b
ON c.classid = b.classid
WHERE c.classid = 'grade1' )";
$result = mysql_query($query);
while( $row = mysql_fetch_array($result))
{
echo "<form action='insertattend.php' method='POST'>";
echo "<tr>";
echo "<td>" . "<input name=classid type=text value=" .$row['classid']." ></td>";
echo "<td>" . "<input name=fname type=text value=" .$row['fname']." ></td>";
echo "<td>" . "<input name=teacherid type=number value=" .$row['teacherid']." ></td>";
echo "<td>" . "<input type=number name=locid value=" .$row['locid']." ></td>";
echo "<td>" . "<input name=date type=date value='date'></td>";
echo "<td>" . "<input type=radio id=attend name=attend value='present'>";?>P
<?php echo "<input type=radio id=attend name=attend value='absent'>";?>A
<?php
echo"</td>";
echo "<td><input name=comment type=comment row=3 column=5></td>";
echo "</tr>";
//echo "</form>";
?>
<input type="submit" name="submit" value="submit">
</form>
<?php } ?>
<!--<form action="insertattend.php">-->
</table>
</body>
</html>
These changes ought to be done on your attendance.php code
First move this echo "<form action=insertattend.php method=POST>"; out of your while
Actually you are closing the <form> tag before the submit button.
echo "</tr>";
echo "</form>"; //<--- Comment or Remove this line
Also, remove this line too (because you already defined above the while loop)
<form action="insertattend.php"> <!-- Remove this line -->
<input type="submit" value="submit">
</form>
Another thing is.. echo "<td><input name=comment type=comment row=3 column=5></td>"; I don't think there is something called type=comment replace that to type=text or use a <textarea>
You missed name="submit" in your attendance.php form.
<input type="submit" name="submit" value="submit">
use form opening and closing tags like this:
<html>
<head>
<title>grade1</title>
</head>
<body>
<table border="1" cellspacing="1" cellpadding="1" width="200" height="200">
<tr>
<th>classid</th>
<th>studentid</th>
<th>teacherid</th>
<th>locid</th>
<th>date</th>
<th>flag</th>
<th>comments</th>
</tr>
<?php
include 'conn.php';
$query = "(SELECT a.classid, a.fname, b.teacherid, c.locid
FROM class_master c JOIN student_master a
ON c.classid = a.classid JOIN teacher_link b
ON c.classid = b.classid
WHERE c.classid = 'grade1' )";
$result = mysql_query($query);
?>
<form action=insertattend.php method=POST>
<?php
while ($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . "<input name='classid[]' type=text value=" . $row['classid'] . " </td>";
echo "<td>" . "<input name='fname[]' type=text value=" . $row['fname'] . " </td>";
echo "<td>" . "<input name='teacherid[]' type=number value=" . $row['teacherid'] . " </td>";
echo "<td>" . "<input type=number name='locid[]' value=" . $row['locid'] . " </td>";
echo "<td>" . "<input name='date[]' type=date value='date'></td>";
echo "<td>" . "<input type=radio id=attend name='attend[]' value='present'>";
?>P
<?php echo "<input type=radio id=attend name='attend[]' value='absent'>"; ?>A
<?php
echo"</td>";
echo "<td><input name='comment[]' type=comment row=3 column=5></td>";
echo "</tr>";
}
?>
</table>
<input type="submit" name= "submit" value="submit">
</form>
</body>
</html>
You have to post the values as array in form fields inside the loop. Then you have to get the array of post values in foreach to insert all the records to the database.
There are two forms.
Input types are in one form and submit button in another one.So avoid that and put together in one form.There is no need for another form with same action value. There are more than one student so write form outside while loop and then array name for input values. Then in php page values in array so use loop to get all values seperately.
<html>
<head>
<title>grade1</title>
</head>
<body>
<form action=insertattend.php method=POST>
<table border="1" cellspacing="1" cellpadding="1" width="200" height="200">
<tr>
<th>classid</th>
<th>studentid</th>
<th>teacherid</th>
<th>locid</th>
<th>date</th>
<th>flag</th>
<th>comments</th>
</tr>
<?php
include 'conn.php';
$query = "(SELECT a.classid, a.fname, b.teacherid, c.locid
FROM class_master c JOIN student_master a
ON c.classid = a.classid JOIN teacher_link b
ON c.classid = b.classid
WHERE c.classid = 'grade1' )";
$result = mysql_query($query);
$i=1;
while( $row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . "<input name=classid[] type=text value=" .$row['classid']." </td>";
echo "<td>" . "<input name=fname[] type=text value=" .$row['fname']." </td>";
echo "<td>" . "<input name=teacherid[] type=number value="
.$row['teacherid']." </td>";
echo "<td>" . "<input type=number name=locid[] value=" .$row['locid']." </td>";
echo "<td>" . "<input name=date[] type=date value='date'></td>";
echo "<td>" . "<input type=radio id=attend name=attend[] value='present'>";?>P
<?php echo "<input type=radio id=attend name=attend[] value='absent'>";?>A
<?php
echo"</td>";
echo "<td><input name=comment[] type=text row=3 column=5></td>";
echo "</tr>";
}
?>
</table>
<input type="submit" value="submit">
</form>
</body>
</html>
And in php page you take size of any of the coming array
For eg:sizeof(classid);using that write for loop to accept values

Create dropdown list from another table into my update/delete/add table

Im a newbie and working on a project for school
I have a website that lists foods.
I have an update table that allows me to change and add data.
For the food group field I have it cross reference another table called food_group which has the food_group(name) and an id.
When you view the food data you can see the name that it pulls instead of the ID. On the update page I would like a drop down to be in the place of the ID. So you can see the "friendly" name instead of the ID number, but it has to store the ID not the friendly name in the food table.
Website can be found at http://web.nmsu.edu/~jrortiz/ICT458/FINAL/
The code I have is:
<html>
<head>
</head>
<body>
<?php
$con = mysqli_connect("localhost","user","pw","db");
if (!$con){
die("Can not connect: " . mysql_error());
}
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE food SET food_group='$_POST[Food_group]', food='$_POST[Food]', ph='$_POST[PH]' WHERE food='$_POST[hidden]'";
mysql_query($UpdateQuery, $con);
};
if(isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM food WHERE Food='$_POST[hidden]'";
mysql_query($DeleteQuery, $con);
};
if(isset($_POST['add'])){
$AddQuery = "INSERT INTO food (Food_group, Food, PH) VALUES ('$_POST[addGroup]','$_POST[addFood]','$_POST[addPH]')";
mysql_query($AddQuery, $con);
};
$sql = "SELECT * FROM food";
$myData = mysqli_query($con,$sql);
echo "<table border=1>
<tr>
<th>Food Group</th>
<th>Food</th>
<th>PH</th>
<th>Update/Add</th>
<th>Delete</th>
</tr>";
while($record = mysqli_fetch_array($myData)){
echo "<form action=updateFood.php method=post>";
echo "<tr>";
echo "<td><input type='text' name='Food_group' value='$record[food_group]'/></td>";
echo "<td><input type='text' name='Food' value='$record[food]'/></td>";
echo "<td><input type='text' name='PH' value='$record[ph]'/></td>";
echo "<td><input type='submit' name='update' value='update'/></td>";
echo "<td><input type='submit' name='delete' value='delete'/></td>";
echo "<td><input type='hidden' name='hidden' value='$record[food]'/></td>";
echo "</tr>";
echo "</form>";
}
echo "<form action=updateFood.php method=post>";
echo "<tr>";
echo "<td><input type='text' name='addGroup'></td>";
echo "<td><input type='text' name='addFood'></td>";
echo "<td><input type='text' name='addPH'></td>";
echo "<td><input type='submit' name='add' value='add'/></td>";
echo "</tr>";
echo "</form>";
echo "</table>";
mysql_close($con);
?>
</body>
</html>
____________ Update 12/2/13 10:30pm ___________________
Ok so if I create a new php page like the following it will work. However, I have no idea how to combine it into the original above... Can anyone help?
<html>
<head>
</head>
<body>
<?php
// Connect to the database server
$con = mysql_connect("localhost","user","pw");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("db",$con);
$sql2="SELECT id, food_group FROM food_group";
$result = mysql_query($sql2,$con) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$type=$row["food_group"];
$options.= '<option value="'.$row['id'].'">'.$row['food_group'].'</option>';
};?>
<SELECT NAME=Food_group>
<OPTION VALUE=0>Choose</OPTION>
<?php echo $options; ?>
</SELECT>
</body>
</html>
Thank you for all your help!
Jason
Your script is nice but I just want to point the following:
There's no need to concatenate this
"<td>" . "<input type=text name=Food_group value=" . $record['food_group'] . " </td>";
you can type it like this:
echo "<td><input type=text name=Food_group value='$record[food_group]'</td>";
also you missed to close your input tag
echo "<td><input type=text name=Food_group value='$record[food_group]' /></td>";
and another is you need to quote your attribute values , see below
echo "<td><input type='text' name='Food_group' value='$record[food_group]'</td>";
Last thing is that you're open to SQL injection, so you should start learning mysqli and prepared statement

Categories