Im new to php and i have this code tested with XAMPP at backend
$name = addslashes ($_POST['name']);
$email = addslashes ($_POST['email']);
$paswd = addslashes ($_POST['paswd']);
$sql = "INSERT INTO webusers (username,email,paswd)VALUES('$name','$email','$paswd')";
and here's my code for displaying data
include_once('dbcon.php');
$db = mysql_select_db('test');
$sql = "SELECT * FROM webusers";
$result = mysql_query($sql);
$urow = mysql_num_rows($result);
echo"
<table border='1'>
<th>ID</th>
<th>USERNAME</th>
<th>EMAIL</th>
<th>PASSWORD</th>
";
if($result > 0){
while($urow = mysql_fetch_array($result)){
echo "<tr>";
echo" <td class='myinput'>'" .$urow['id']. "'</td>";
echo" <td class='myinput'>'" .$urow['username']. "'</td>";
echo" <td class='myinput'>'" .$urow['email']. "'</td>";
echo" <td class='myinput'>'" .$urow['paswd']. "'</td>";
echo "</tr>";
echo"</table>";
}
}else{
echo"No record";
}
?>
Those code works, except for the data which surprise me why it has a single quote when i show/display it on a table. though i input the data in html . and my magic_quote_gpc was off. is there something i missed or anything wrong with my code? or there is something with my database collation?
i also tried mysql_real_escape_string and mysql_escape_string, nothings change.
thanks for the help.
Otep
Because you put Single Quotes in the HTML ?
echo"
<table border='1'>
<tr>
<th>ID</th>
<th>USERNAME</th>
<th>EMAIL</th>
<th>PASSWORD</th>
</tr>
";
if($result > 0){
while($urow = mysql_fetch_array($result)){
echo "<tr>";
echo" <td class=\"myinput\">" .$urow['id']. "</td>";
echo" <td class=\"myinput\">" .$urow['username']. "</td>";
echo" <td class=\"myinput\">" .$urow['email']. "</td>";
echo" <td class=\"myinput\">" .$urow['paswd']. "</td>";
echo "</tr>";
echo"</table>";
}
}else{
echo"No record";
}
Try that, your code without Single quotes
You are printing a ' around your values in your HTML Markup
echo "<tr>";
echo" <td class='myinput'>" .$urow['id']. "</td>";
echo" <td class='myinput'>" .$urow['username']. "</td>";
echo" <td class='myinput'>" .$urow['email']. "</td>";
echo" <td class='myinput'>" .$urow['paswd']. "</td>";
echo "</tr>";
is what you really want to echo
Try this one
if($result > 0){
while($urow = mysql_fetch_array($result)){
echo "<tr>";
echo" <td class='myinput'>" .stripslashes($urow['id']). "</td>";
echo" <td class='myinput'>" .stripslashes($urow['username']). "</td>";
echo" <td class='myinput'>" .stripslashes($urow['email']). "</td>";
echo" <td class='myinput'>" .stripslashes($urow['paswd']). "</td>";
echo "</tr>";
echo"</table>";
}
}else{
echo"No record";
}
Related
What is the problem with my code?
My PHP is working, but delete button doesn't work!
if(isset($_POST['delete'])) {
$ID = $_POST['value'];
$delete = "DELETE FROM tbl_document WHERE ID = $ID";
$result = mysqli_query($con,$delete);
}
$query = "SELECT * FROM tbl_document LIMIT $start, $end";
$result = mysqli_query($con,$query);
echo "<table border='1' width='300' height='160' align = center id='result'>";
echo '<tr>
<th width="80">ID</th>
<th width="200">Title</th>
<th width="260">Presented To</th>
<th width="260">Presented By</th>
<th width="160">Date Submitted</th>
<th>Location</th>
<th width="17%">Option</th>
</tr>';
while($row = mysqli_fetch_array($result)){
echo "<tr align = center >";
<td width='20' height='60'>" .$row['ID']. "</td>";
<td width='120' height='60'>" .$row['Title']. "</td>";
<td>" .$row['Presented_To']. "</td>";
<td>" .$row['Presented_By']. "</td>";
<td>" .$row['Date_Submitted']. "</td>";
<td>" .$row['Location']. "</td>";
"<td width='17%'>";
?>
<?php if($_SESSION['user'] == "1")
?>
<button class="w3-btn w3-red w3-border-large w3-circle" value="<?php echo $row['ID'];?>" name="delete" style="width:40%"><i class="fa fa-trash-o"></i>Delete</button>
<?php } ?>
<?php
echo "</td>";
echo "</tr>";
}
echo"</table>";
A button is not enough to send data, you need to contain it within a form.
Here is how it should look like:
<form method="post">
<button name="delete" value="<?php echo $row['ID'];?>" class="w3-btn w3-red w3-border-large w3-circle" style="width:40%"><i class="fa fa-trash-o"></i>Delete</button>
</form>
<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.
After resolving an Undefined offset error, the error message is no more, but the results now displayed are only one record instead of the expected number of records, for instance 3.
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
$time = $row['vluchttijd'];
if (!function_exists('timeInWords')) {
function timeInWords($time) {
list($hours, $minutes) = explode(':', $time);
return (int)$hours . " hrs " . (int)$minutes . " min";
} $result = mysql_query($sql);
{
I can remove everything from $time down and then it returns all of the expected records.
As requested all of the code. I should preface this that I am still very very new to the world of PHP, so please go easy on me:
<?php
include('../datalogin.php'); // include your code to connect to DB.
mysql_query("SET CHARACTER SET 'utf8';");//GET and POST
mysql_query("SET NAMES 'utf8';");//POST
/* Get data. */
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
die("Invalid ID specified.");
}
$rID = (int)$_GET['id'];
$sql = "a bunch of SQL code removed to save space
WHERE vg.reisID = '$rID'
ORDER BY vg.vertrekdatum2 ASC";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
$time = $row['vluchttijd'];
if (!function_exists('timeInWords')) {
function timeInWords($time) {
list($hours, $minutes) = explode(':', $time);
return (int)$hours . " hrs " . (int)$minutes . " min";
} $result = mysql_query($sql);
{
echo "<table border='0' width='640'>";
echo "<tbody>";
echo "<tr>";
echo "<td colspan='3'><strong>" .date('d-M-Y H:i', strtotime($row['vertrekdatum2'])). "
</strong></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'><span class='c_vertrek'>(D)
".htmlspecialchars($row['luchthavennaam'])."</span></td>";
echo "</tr>";
echo "<tr>";
echo "<td width='18%'><strong>Duration:</strong></td>";
echo "<td width='41%'>".timeInWords($time)."</td>";
echo "<td rowspan='4' width='41%' align='center' valign='middle'><img
src='../logos/".$row['logo']."'</td>";
echo "</tr>";
echo "<tr>";
echo "<td><strong>Equipment:</strong></td>";
echo "<td>".$row['toestel']." ".$row['erlr']."</td>";
echo "</tr>";
echo "<tr>";
echo "<td><strong>Class:</strong></td>";
echo "<td>".$row['reisklass']."</td>";
echo "</tr>";
echo "<tr>";
echo "<td><strong>Miles:</strong></td>";
echo "<td>".$row['afstand']." miles</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3' height='12'></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'><strong>".date('d-M-Y H:i', strtotime($row['aankomstdatum2']))."
</strong></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'><span class='c_aankomst'>(A)
".htmlspecialchars($row['aankomstnaam'])."</span></td>";
echo "</tr>";
echo "<tr>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3' height='1' bgcolor='#585858'></td>";
echo "</tr>";
echo "<tr>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "</tr>";
echo "<tr>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'></td>";
echo "</tr>";
}
}
}
echo "</table>";
?>
This should do it, you called the mysql_query with the same query over again, so that would give you the same result all the time.
I cleaned the start of you while loop, so it only fetches the data from your result object and prints it. Your time function does not need to be inside the loop so I moved it outside.
I also cleaned the loop from some echo's
<?php
include('../datalogin.php'); // include your code to connect to DB.
function timeInWords($time) {
list($hours, $minutes) = explode(':', $time);
return (int)$hours . " hrs " . (int)$minutes . " min";
}
mysql_query("SET CHARACTER SET 'utf8';");//GET and POST
mysql_query("SET NAMES 'utf8';");//POST
/* Get data. */
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
die("Invalid ID specified.");
}
$rID = (int)$_GET['id'];
$sql = "a bunch of SQL code removed to save space
WHERE vg.reisID = '$rID'
ORDER BY vg.vertrekdatum2 ASC";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result))
{
$time = $row['vluchttijd'];
echo "<table border='0' width='640'>
<tbody>
<tr>
<td colspan='3'><strong>" .date('d-M-Y H:i', strtotime($row['vertrekdatum2']))."
</strong></td>
</tr>
<tr>
<td colspan='3'><span class='c_vertrek'>(D)
".htmlspecialchars($row['luchthavennaam'])."</span></td>
</tr>
<tr>
<td width='18%'><strong>Duration:</strong></td>
<td width='41%'>".timeInWords($time)."</td>
<td rowspan='4' width='41%' align='center' valign='middle'><img
src='../logos/".$row['logo']."'</td>
</tr>
<tr>
<td><strong>Equipment:</strong></td>
<td>".$row['toestel']." ".$row['erlr']."</td>
</tr>
<tr>
<td><strong>Class:</strong></td>
<td>".$row['reisklass']."</td>
</tr>
<tr>
<td><strong>Miles:</strong></td>
<td>".$row['afstand']." miles</td>
</tr>
<tr>
<td colspan='3'></td>
</tr>
<tr>
<td colspan='3' height='12'></td>
</tr>
<tr>
<td colspan='3'><strong>".date('d-M-Y H:i', strtotime($row['aankomstdatum2']))."
</strong></td>
</tr>
<tr>
<td colspan='3'><span class='c_aankomst'>(A)
".htmlspecialchars($row['aankomstnaam'])."</span></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td colspan='3' height='1' bgcolor='#585858'></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td colspan='3'></td>
</tr>
<tr>
<td colspan='3'></td>
</tr>";
}
echo "</table>";
?>
I have a problem with showing tables in PHP. I have installed shortcode exec php plugin.
This is the code I have done:
if(mysql_num_rows($raw_results) > 0){
while($results = mysql_fetch_array($raw_results)){
echo "<p><h3>".$results['id']."</h3>"."Student Name :".$results['name']." </p>" . "</br>";
echo "Age : ".$results['age']." Years old.</br>";
echo "Course : ".$results['course']."</br>";
echo "Gender : ".$results['gender']."</br></br>";
echo "<HR>";
}
}
May I know how I can sort the data out in tables? Any table codes I try doesn't seem to display at all..
<table>
<tr>
<td>Student name</td>
<td>Age</td>
<td>Course</td>
<td>Gender</td>
</tr>
<?php
if(mysql_num_rows($raw_results) > 0){
while($results = mysql_fetch_array($raw_results)){
echo "<tr><td>".$results['id']."</td>";
echo "<td>".$results['age']."</td>";
echo "<td>".$results['course']."</td>";
echo "<td>".$results['gender']."</td></tr>";
}
}
?>
</table>
Array sorting in php
http://us1.php.net/manual/en/function.sort.php
Simply echo the html table tags like so:
echo "<table border='1'>
<tr>
<th>Id</th>
<th>Age</th>
<th>Course</th>
<th>Gender</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['course'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "</tr>";
}
echo "</table>";
<table>
<tr>
<th>ID</th>
<th>Student Name</th>
<th>Age</th>
<th>Course</th>
<th>Gender</th>
</tr>
while($results = mysql_fetch_array($raw_results)){
echo "<tr>";
echo "<td>".$results['id']."</td>";
echo "<td>".$results['name']."</td>";
echo "<td>".$results['age']." Years old.</td>";
echo "<td>".$results['course']."</td>";
echo "<td>".$results['gender']."</td>";
echo "</tr>";
}
echo "</table>";
you need to connect databse and then fetch data to database
while($results = mysql_fetch_array($row)){
echo "<tr>";
echo "<td>".$results['id']."</td>";
echo "<td>".$results['name']."</td>";
echo "<td>".$results['age'].".</td>";
echo "<td>".$results['course']."</td>";
echo "<td>".$results['gender']."</td>";
echo "</tr>";
I'm using php to create a table from the data extracted from MySQL database. I'm using SELECT * From table_name and all data is appearing as I wish. But I want to create a Drop Down menu in the last column for that I'm using the function displayDMenu. But the drop down menu is still not appearing in the table. Can you please suggest what is causing the problem and solution? Or alternate solutions? Here's the code:
<div id="main">
<?php
include("config.php");
$result = mysql_query("SELECT * FROM schedule");
echo "<table border='1'>
<tr>
<th>Appoint No.</th>
<th>CR number</th>
<th>Time</th>
<th>Date</th>
<th>Month</th>
<th>Year</th>
<th>Department</th>
<th>Status</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td style='padding:3px'>" . $row['S_ID'] . "</td>";
echo "<td style='padding:3px'>" . $row['crnumber'] . "</td>";
echo "<td style='padding:3px'>" . $row['ScdTime'] . "</td>";
echo "<td style='padding:3px'>" . $row['ScdDate'] . "</td>";
echo "<td style='padding:3px'>" . $row['ScdMonth'] . "</td>";
echo "<td style='padding:3px'>" . $row['ScdYear'] . "</td>";
echo "<td style='padding:3px'>" . $row['DName'] . "</td>";
echo "<td 'style='padding:3px'>" . displayDMenu() . "</td>";
echo "</tr>";
}
echo "</table>";
function displayDMenu() {
$r = '';
$r .='<form method="post" action="ScdApproval.php">';
$r .='<select name="Status">';
$r .='<option value="approved" selected>Approve</option>';
$r .='<option value="disapproved">Disapprove</option>';
$r .='</select>';
$r .='</form>';
}
?>
</div> <!--main ends here -->
Not sure why you need the user function its not doing anything logical its just spitting back html which there is no reason not to put that html in the while loop.
<?php
include("config.php");
$result = mysql_query("SELECT * FROM schedule");
?>
<style>#main table tr td{padding:3px;}</style>
<div id="main">
<table border="1">
<tr>
<th>Appoint No.</th>
<th>CR number</th>
<th>Time</th>
<th>Date</th>
<th>Month</th>
<th>Year</th>
<th>Department</th>
<th>Status</th>
</tr>
<?php
if(mysql_num_rows($result) > 0):
while($row = mysql_fetch_array($result)): ?>
<tr>
<td><?php echo $row['S_ID']?></td>
<td><?php echo $row['crnumber']?></td>
<td><?php echo $row['ScdTime']?></td>
<td><?php echo $row['ScdDate']?></td>
<td><?php echo $row['ScdMonth']?></td>
<td><?php echo $row['ScdYear']?></td>
<td><?php echo $row['DName']?></td>
<td><form method="post" action="ScdApproval.php">
<select name="Status">
<option value="approved" selected>Approve</option>
<option value="disapproved">Disapprove</option>
</select>
</form>
</td>
</tr>
<?php
endwhile;
else: ?>
<tr>
<td rowspan="8">No results</td>
</tr>
<?php endif;?>
</table>
</div> <!--main ends here -->
Also:
Really you should also switch to using PDO or mysqli prepared querys for new code.
You need to return the value $r in the function displayDMenu()
ie
function displayDMenu()
{
.........
return $r;
}