I'm making a page to register damage mobile phones. I made a search query to search data in database which matched datepicker's date. Here is my source code. (Only relevant to this question, )
<?php
$con = mysql_connect("localhost", "root", "");
if ($con) {
$db = mysql_select_db('mobile', $con);
} else {
die('Could not connect: ' . mysql_error());
}
if (array_key_exists('myday', $_POST)) {
$mydate = $_POST['mydate'];
$myday = "SELECT * FROM mobile_rep WHERE sdate='{$mydate}' ";
}
mysql_close($con)
?>
<html>
<head> </head>
<body>
<form action="index.php" method="POST" class="form-inline">
<div class="span12" style="border-radius: 5px; border-style: solid; border-width: 1px; margin-left: 20px;" >
<h5 style="margin-left: 10px; "> <b> Current User List </b></h5>
Select By Date
<input type="date" name="mydate" id="mydate" class="input-medium" />
<input type="submit" class="btn-primary " name="myday" value="Search by date" id='myday' />
<br/> <br/>
<table border="1" width="300" cellspacing="1" cellpadding="1" class="table table-striped" style=" border-radius: 5px; border-style: solid; border-width: 1px; ">
<thead>
<tr>
<th>sdate</th>
<th>model_no</th>
<th>fault</th>
<th>rp_fee</th>
<th>sp_fee</th>
<th>total</th>
</tr>
</thead>
<tbody>
<?php
while ($row1 = mysql_fetch_array($myday)) {
echo "<tr>";
echo "<td>" . $row1['sdate'] . "</td>";
echo "<td>" . $row1['model_no'] . "</td>";
echo "<td>" . $row1['fault'] . "</td>";
echo "<td>" . $row1['rp_fee'] . "</td>";
echo "<td>" . $row1['sp_fee'] . "</td>";
echo "<td>" . $row1['total'] . "</td>";
echo "</tr>";
}
?>
</tbody>
</table>
</div>
</form>
</body>
</html>
And I'm having
Undefined variable: myday in C:\wamp\www\mobile\index.php on line 227
Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\wamp\www\mobile\index.php on line 227
Line 227 means while ($row1 = mysql_fetch_array($myday)) {
I also uploaded an image of my page too.
Please help me.
When you enter your page first time, $myday variable is not set. If you send your POST code then if (array_key_exists('myday', $_POST)) { } is executed and $myday is created.
Just check before while loop if your variable exists, otherwise it pass null value to mysql_fetch_data function
if(isset($myday)) { while ($row1 = mysql_fetch_array($myday)) ... }
Related
i need to get the counting of the table rows in order as a column in column "N"
also i need to make the column"N" width looks smaller than others columns
i think i have done everything but this is the only point i couldn't achieved
the output should be as the picture :
i hope you could help me guys, thank you
here is my codes :
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
// DataBase connection
$host = "localhost";
$user = "root";
$password = "";
$db = "worksheet9";
$con = mysqli_connect($host, $user, $password, $db);
//verfiy Connection
if( mysqli_connect_errno()) {
echo " Connection Error:" . mysqli_connect_error();
}
if(isset($_POST['submit'])) {
$task=$_POST['task'];
// insert sql query
$sql = "INSERT INTO dolist (task) VALUES ('$task')";
$result=mysqli_query($con, $sql);
// verify query
if ($result) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
}
?>
<form method='POST'>
<input type="text" name="task" id="task" required
placeholder="add task" oninvalid="this.setCustomValidity('you must fill the task')"
oninput="this.setCustomValidity('')">
<input type="submit"name="submit"value="add task" ><!-- comment -->
</form>
<table border="1">
<tr>
<th> N </th>
<th>ID </th>
<th>task</th>
<th>delete</th><!-- comment -->
<th>update</th>
</tr>
<?php
// Select sql query
$sql = "SELECT ID,task FROM dolist";
$result = mysqli_query($con, $sql);
while ($rows = mysqli_fetch_array($result)) {
echo"<form method ='POST'>";
echo "<tr>";
echo "<td>" . "<input type='number' readonly name='number' value='" . $rows[''] . "'></td>";
echo "<td>" . "<input type='text' readonly name='id' value='" . $rows['ID'] . "'></td>";
echo "<td>" . "<input type='text' name='task' value= '" . $rows['task'] . "'></td>";
echo "<td>" . "<input type='submit' name='delete' value='x'>" . "</td>";
echo "<td>" . "<input type='submit' name='update' value='update'>" . "</td>";
echo "</tr>";
echo "</form>";
}
if(isset($_POST['update'])) {
$sql = "UPDATE dolist SET task='$_POST[task]'WHERE ID=$_POST[id]";
$result = mysqli_query($con,$sql) or die(mysqli_connect_errno());
if($result){
echo "updated";
} else {
echo "update faild";
}
}
//perform only when the user click on Delete
if(isset($_POST['delete'])) {
$sql = "DELETE FROM dolist WHERE ID=$_POST[id]";
$result = mysqli_query($con,$sql) or die(mysqli_connect_errno());
if($result){
echo "DELETED";
}else{
echo "delete faild";
}
}
?>
</table>
</body>
</html>
For row numbering add a counter to your PHP code:
$rowNumber = 1;
while ($rows = mysqli_fetch_array($result)){
echo"<form method ='POST'>";
echo "<tr>";
echo "<td>" . $rowNumber++. "</td>";
// echo other fields...
echo"</tr>";
For formatting, give your table an ID (not essential but it makes the CSS styling more specific)
This should then generate HTML like this:
<table id="results">
<tr><th>N</th><th>ID</th><th>task</th><th>Delete</th><th>Update</th></tr>
<tr><td>1</td><td>11</td><td>task</td><td>X</td><td>Update</td></tr>
<tr><td>2</td><td>12</td><td>task</td><td>X</td><td>Update</td></tr>
</table>
Now you can style it with CSS:
/* Border around the table */
#results {
border: 1px solid grey;
}
/* Border around the individual cells */
#results th, #results td {
border: 1px solid gray;
}
/* Set the width of the first TH. Also sets the width of the res of the column */
#results th:first-of-type {
width:1em;
}
/* Set the width of the 3rd column (the 'task' column */
#results th:nth-of-type(3) {
width:10em;
}
Output:
Note: you could do this by adding class attributes to the <th> elements and styling those. It makes for easier maintenance later.
Use of formatting attributes in HTML tags (like BORDER="1") is considered bad practice and should be avoided.
Don't use JS or PHP to count something pure CSS can help you with
Use CSS Counters
Use <thead> and <tbody> table elements
Use width: 0%; for the td you want really condensed in width
.tbodyCount {
counter-reset: rowIndex;
}
.tbodyCount tr {
counter-increment: rowIndex;
}
.tbodyCount td:nth-child(1)::before {
width: 0%; /* Make as smaller as possible */
content: counter(rowIndex); /* Learn about counters*/
}
<table>
<thead>
<tr>
<th>N</th>
<th>ID</th>
</tr>
</thead>
<tbody class="tbodyCount">
<tr>
<td></td>
<td>Foo</td>
</tr>
<tr>
<td></td>
<td>Bar</td>
</tr>
<tr>
<td></td>
<td>Baz</td>
</tr>
</tbody>
</table>
Here's the thing, I need to update a selected row from a table, so I'm putting a form for each row of it (Every single row has an update button) and when I click update, it doesn't submit, actually, doesn't do anything.
Here's my code, I'll be grateful with the solution.
<div class="table-responsive">
<table class="table table-condensed">
<thead class="">
<tr>
<th>ID</th>
<th>Project</th>
<th>Type</th>
<th>Kick-Off Date</th>
<th>Deadline Date</th>
<th>Current Ninja</th>
<th>Status</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
$q = $_GET['q'];
$sql="SELECT pr.project_name, pr.project_type, pr.project_start_date, pr.project_end_date, us.nombre, st.id_status, pr.id_project FROM NT_Projects as pr LEFT JOIN Project_Status as st on st.id_status = pr.id_status LEFT JOIN NT_Clients as cl on cl.id_client = pr.id_client LEFT JOIN usuarios as us on us.username = pr.username WHERE cl.id_client = $q";
$result = mysql_query($sql) or die(mysql_error());
$upt = 1;
while($row = mysql_fetch_array($result)) {
echo '
<div id="update-project">
<form method="post" action="sistema/_actions/updateProject.php" id="res-update-proj-'.$upt.'">';
$kickoff = date('m/d/Y', strtotime($row[2]));
$deadline = date('m/d/Y', strtotime($row[3]));
echo '<tr>';
echo '<td width="95px">
<input class="form-control" name="id_Proj" type="text" value="'.$row[6].'" readonly>
</td>';
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $kickoff . "</td>";
echo "<td>" . $deadline . "</td>";
echo "<td>" . $row[4] . "</td>";
echo '<td width="225px">';
echo '<select class="form-control" name="proj_Status">';
$qStatus = "SELECT * FROM Project_Status;";
$exStatus = mysql_query($qStatus);
while($rStatus = mysql_fetch_array($exStatus))
{
if($row[5] == $rStatus[0])
echo '<option value="'.$rStatus[0].'" selected>'.$rStatus[1].'</option>';
else
echo '<option value="'.$rStatus[0].'">'.$rStatus[1].'</option>';
}
echo '</select>
</td>
<td class="text-center">
<button type="submit" class="btn btn-sm btn-primary btn-UProj" value="res-update-proj-'.$upt.'">Update</button>
<div id="res-update-proj-'.$upt.'" style="width: 100%; margin:0px; padding:0px;"></div>
</td>
</tr>
</form>
</div>';
$upt = $upt + 1;
}
?>
</tbody>
</table>
</div>
That code is being called from another HTML with ajax
You cannot mix table tags with divs and forms according to the web standards. If you do, browser's HTML-parser will mix your tags in unpredictable way.
The only solution I know is to utilize CSS:
<div style="display: table">
<form style="display: table-row">
<div> style="display: table-cell"></div>
</form>
</div>
I want to delete record from checkbox. I have a form to display data from database like this
$connect = mysqli_connect('Ian', 'root', '', 'penduduk');
$call_db = mysqli_query($koneksi, "select * from tabel_data_penduduk");
<form action="deleteData.php" method="post">
<table style="border:black solid; width: 100%">
<tr style="font-weight: bold">
<th style="width: 2%"><input type="checkbox"/></th>
<th style="width: 3%">No. Identity</th>
<th style="width: 4%">No. Driving License</th>
<th style="width: 10%">Name</th>
<th style="width: 12%">Birth Date</th>
<th style="width: 9%">City Code</th>
</tr>
<?php
$counter=0;
while($db = mysqli_fetch_array($call_db, MYSQLI_NUM)){
echo "<tr>"
. "<td style='text-align:center; width:2%'>"
. "<input type='checkbox' name='deleteNetizen[$counter]' value='". $db[0] ."'/>"
. "</td>"
. "<td style='width: 3%'>". $db[0]. "</td>"
. "<td style='width: 4%'>". $db[1]. "</td>"
. "<td style='width: 10%'>". $db[2]. "</td>"
. "<td style='width: 12%'>". $db[3]. "</td>"
. "<td style='width: 9%'>". $db[4]. "</td></tr>";
++$counter;
}
?>
</table>
<button type="submit" name="btnNetizen" value='ButtonNetizen'>Delete Data Netizen Table</button>
</form>
`
and then the deleteData.php (which is same file as the form) is:
<?php
$netizenData= $_POST['deleteNetizen'];
echo "$netizenData";
$hapusPenduduk = $_POST['btnNetizen'];
$db = mysqli_connect('Ian', 'root', '', 'netizen');
if($hapusPenduduk){
$deleteData_Netizen = mysqli_query($db, "delete from table_data_netizen where No_Identity = $netizenData");
}
?>
The $netizenData return a value when i debug it, but somehow it can't be deleted from database. I wonder where did i go wrong?
Thanks for help
It's hard to debug completely from this code snippet, but here are some tips for improving / debugging:
Use bind variables instead of strings in your queries. So change your last code snippet to something like:
<?php
$netizenData= $_POST['deleteNetizen'];
$db = mysqli_connect('Ian', 'root', '', 'netizen');
$delete_stmt = mysqli_prepare($db, "delete from table_data_netizen where No_Identity = ?"); // note that you can / should re-use this prepared statement
if($hapusPenduduk){
mysqli_stmt_bind_param($delete_stmt, $netizenData);
mysqli_stmt_execute($delete_stmt);
// some debug info:
printf("%d rows deleted.", mysqli_affected_rows($delete_stmt));
}
// if necessary
mysqli_stmt_close($delete_stmt);
?>
This change will make it clearer what's happening in your code. For example, if someone put a quote in $netizenData, you'd get weird results. (This is, in fact, where SQL injection attacks come from and why your code is unsafe.) And then you should start to be able to debug things like: did you make it into your if block? Did any rows get deleted? Etc.
Another thing to look at is whether you are committing / need to commit your data. For example, see http://php.net/manual/en/mysqli.autocommit.php for info on autocommit. If it's off (and you're using transactional tables), then you may need to manually commit (or set autocommit to true).
Hope that helps.
It works now. I change the code to:
Form:
<?php
$connect = mysqli_connect('Ian', 'root', '', 'netizen');
$call_db = mysqli_query($connect, "select * from tabel_data_penduduk");
?>
<form action="deleteData.php" method="post">
<table style="border:black solid; width: 100%">
<tr style="font-weight: bold">
<th style="width: 2%"><input type="checkbox"/></th>
<th style="width: 3%">No. Identity</th>
<th style="width: 4%">No. Driving License</th>
<th style="width: 10%">Name</th>
<th style="width: 12%">Birth Date</th>
<th style="width: 9%">City Code</th>
</tr>
<?php
$counter=0;
while($db = mysqli_fetch_array($call_db, MYSQLI_ASSOC)){
echo "<tr>"
. "<td style='text-align:center; width:2%'>"
. "<input type='checkbox' name='deleteNetizen[$counter]' value='". $db['No_Identity'] ."'/>"
. "</td>"
. "<td style='width: 3%'>". $db['No_Identity']. "</td>"
. "<td style='width: 4%'>". $db['No_Driving_License']. "</td>"
. "<td style='width: 10%'>". $db['Name']. "</td>"
. "<td style='width: 12%'>". $db['BOD']. "</td>"
. "<td style='width: 9%'>". $db['City_Code']. "</td></tr>";
++$counter;
}
?>
</table>
<input type="submit" name="btnNetizen" value='Delete Data Netizen Table'/>
</form>
deleteData.php:
<?php
$netizenData= $_POST['deleteNetizen'];
$hapusPenduduk = $_POST['btnNetizen'];
$db = mysqli_connect('Ian', 'root', '', 'netizen');
if($hapusPenduduk){
$delete_stmt = mysqli_prepare($db, "delete from table_data_netizen where No_Identity = ?");
foreach ($netizenData as $del) {//array processed using foreach
mysqli_stmt_bind_param($delete_stmt, 's', $del);
//'s' is type of parameter $del which is string
mysqli_stmt_execute($delete_stmt);
}
}
?>
I'm having trouble setting up some css code for a table I'm generating using php from a database. I want every even row to have a different background-color, so I tried using nth-child(even) for this, but it doesn't seem to work for some reason. Here's my code:
style.css:
#usertable {
border-collapse: collapse;
font-family: "Trebuchet MS", Arial;
}
#usertable td, #usertable th {
border: 1px solid black;
background-color: rgb(228,227,227);
padding: 8px;
}
#usertable tr:nth-child(odd){background-color: rgb(115,115,115);}
admin.php:
<table id="usertable">
<tr>
<th> Id: </th>
<th> Username: </th>
<th> Rights: </th>
</tr>
<?php
$userquery = "SELECT id, username, strength FROM users";
$result = $conn->query($userquery) or die($conn->error);
while ($row = $result->fetch_assoc()) { ?>
<tr>
<td> <?php echo $row['id']; ?> </td>
<td> <?php echo $row['username']; ?> </td>
<td>
<form method="post" action="">
<input type="number" min="0" max="255" name="newrights" value=" <?php echo $row['strength']; ?> ">
<input type="text" name="idnumber" hidden="true" value=" <?php echo $row['id']; ?> ">
<input type="text" name="usertochange" hidden="true" value=" <?php echo $row['username']; ?> ">
<input type="submit" value="Update">
</form>
</td>
</tr>
<?php
}
?>
</table>
There is some markup error with your code
<table id="usertable">
<tr> <!--Add this-->
<th> Id: </th>
<th> Username: </th>
<th> Rights: </th>
</tr> <!--Add this-->
<?php
$userquery = "SELECT id, username, strength FROM users";
$result = $conn->query($userquery) or die($conn->error);
while ($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row['id'] . "</td><td>" . $row['username'] . "</td><td>";
echo "<form method=\"post\" action=\"\">
<input type=\"number\" min=\"0\" max=\"255\" name=\"newrights\" value=\"" . $row['strength'] . "\">
<input type=\"text\" name=\"idnumber\" hidden=\"true\" value=\"" . $row['id'] . "\">
<input type=\"text\" name=\"usertochange\" hidden=\"true\" value=\"" . $row['username'] . "\">
<input type=\"submit\" value=\"Update\">
</form></td></tr>";
}
?>
Also looks like you have used many slaces so i suggest you to check table markup on browser
Updated: Add
#usertable td:nth-child(odd) { background-color: #efefef;}
Do not use tr as you have given background color to td and th initially
Why don't you sue some CSS class in each alternate table row?
Ex:
HTML
<tr class="bg-red">
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr class="bg-blue">
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
CSS
<style type="text/css">
.bg-blue{
background-color: blue;
}
.bg-red{
background-color: red;
}
</style>
I think this will simplify your code & make it easy to read.
Example for Dynamic Data
<?php
$users[] = array('fname'=>'Jill', 'lname'=>'Smith', 'age'=>50);
$users[] = array('fname'=>'Eve', 'lname'=>'Jackson', 'age'=>94);
$rowColClass = 'bg-blue';
foreach ($users as $key => $value)
{
echo "<tr class='$rowColClass'>";
echo "<td>$value[fname]</td>";
echo "<td>$value[lname]</td>";
echo "<td>$value[age]</td>";
echo "</tr>";
if($rowColClass == 'bg-blue')
$rowColClass = 'bg-red';
else
$rowColClass = 'bg-blue';
}
?>
I am trying to get information from mysql including a blob image which i shall echo with php and will have an onclick event within the php, redirecting it to another page. The onlick event will contain a mysql result which it will carry with it as seen in the code below.
My main issue is with the syntax of the code or if there is another way to do it all together. please keep in mind the output when the script is run is similiar to that of google images, bing images etc. Thank you.
<?php
$con=mysqli_connect("localhost","root","*******","media");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM movies ORDER BY `movies`.`title` ASC");
echo "<table border='3' style='margin: auto; text-align: left;background: white; padding: 3em;'>
<tr>
<th><b>Movie Title</b></th>
<th><b>Language</b></th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td style='padding-right: 2em;'><img src="data:image/jpeg;base64,' . base64_encode( $row['image'] ) . '" width="160px" height="200px";" onclick="window.location='lookup.php?pattern=" . $row['title'] . "';>";
</td>
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Untested but here is one way you could clean up your code move style and javascript (and use some jquery) into the head:
<?php
$con=mysqli_connect("localhost","root","*******","media") or die("Failed to connect to MySQL: " . mysqli_connect_error());
$result = mysqli_query($con,"SELECT * FROM movies ORDER BY `movies`.`title` ASC");
?>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('img').each(function() {
var img = $(this);
img.click(function() {
window.location="lookup.php?pattern=" + img.attr('title');
});
});
});
</script>
<style>
table {
margin: auto;
text-align: left;
background: white;
padding: 3em;
border: 2px solid #000000;
}
table tr td {
padding-right: 2em;
}
table tr td img {
width: 160px;
height: 200px;
}
</style>
</head>
<body>
<table>
<tr>
<th>Movie Title</th>
<th>Language</th>
</tr>
<?php
while($row = mysqli_fetch_array($result)) {
echo "
<tr>
<td>
<img src=\"data:image/jpeg;base64," . base64_encode( $row['image'] ) . "\" title=\"" . $row['title'] . "\">
</td>
</tr>
";
}
?>
</table>
</body>
</html>
<?php mysqli_close($con); ?>
Or if you don't want to use javascript, you could always wrap the image around the anchor tag instead:
<td>
<a href='lookup.php?pattern={$row['title']}'>
<img src=\"data:image/jpeg;base64," . base64_encode( $row['image'] ) . "\">
</a>
</td>
You could further separate PHP and HTML code:
<?php
$con=mysqli_connect("localhost","root","*******","media");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
$result = mysqli_query($con,"SELECT * FROM movies ORDER BY `movies`.`title` ASC");
?>
<table border='3' style='margin: auto; text-align: left;background: white; padding: 3em;'>
<tr>
<th><b>Movie Title</b></th>
<th><b>Language</b></th>
</tr>
<?php
while($row = mysqli_fetch_array($result)) {
$img = 'data:image/jpeg;base64,'
. base64_encode( $row['image'] );
?>
<tr>
<td style='padding-right: 2em;'>
<img src="<?php echo $img; ?>"
style="width: 160px; height: 200px;"
onclick="window.location='lookup.php?pattern=<?php echo $row['title']?>;'"
/>
</td>
</tr>
<?php } ?>
</table>
<?php
mysqli_close($con);
?>
You could also use some sort of templating engine to do this, but the results would be pretty much the same - I don't see much point in writing, say,
<strong>{{ title }}</strong>
instead of
<strong><?php echo $title; ?></strong>