I've got a html table I build from the database:
<tr onclick="DoNav('<?php echo $result_videos[$i]["video_url"]; ?>');">
<td>
<?php echo $result_videos[$i]["camera_name"]; ?>
</td>
<td>
Timezone: <?php echo queryTimezone(); ?> <br>
Video Size: <?php echo $result_videos[$i]["video_size"]; ?> bytes <br>
Video Length: <?php echo strTime($result_videos[$i]["video_length"]); ?>
</td>
<td>
<form name="myform" action="<?php echo htmlentities($_SERVER['REQUEST_URI']); ?>" method="POST">
<input type="submit" name="delete_video" value="Delete" title="Delete this video" onClick="return confirm('Are you sure you want to delete?')"/>
<input type="hidden" name="video_url" value="<?php echo $result_videos[$i]["video_url"]; ?>" />
</form>
</td>
</tr>
The problem is this function:
function queryTimezone()
{
$query = "SELECT timezone FROM #__camcloud_users WHERE user_id=".$user->id;
$db->setQuery($query);
$timezone = $db->loadResult();
return $timezone;
}
It messes the table up. I don't seem to get any php errors and it cuts off the table at the "Timezone:" part and throws it to the top right of the page. Basically where I call the function queryTimezone. If I change to put this inside the html table it works fine:
<tr onclick="DoNav('<?php echo $result_videos[$i]["video_url"]; ?>');">
<td>
<?php echo $result_videos[$i]["camera_name"]; ?>
</td>
<td>
Timezone: <?php
$query = "SELECT timezone FROM #__camcloud_users WHERE user_id=".$user->id;
$db->setQuery($query);
$timezone = $db->loadResult();
echo $timezone;
?> <br>
Video Size: <?php echo $result_videos[$i]["video_size"]; ?> bytes <br>
Video Length: <?php echo strTime($result_videos[$i]["video_length"]); ?>
</td>
<td>
<form name="myform" action="<?php echo htmlentities($_SERVER['REQUEST_URI']); ?>" method="POST">
<input type="submit" name="delete_video" value="Delete" title="Delete this video" onClick="return confirm('Are you sure you want to delete?')"/>
<input type="hidden" name="video_url" value="<?php echo $result_videos[$i]["video_url"]; ?>" />
</form>
</td>
</tr>
This works fine. Did I do something wrong with how I call the function?
The function cannot determine what $user->id, therefore doesnt return a valid result... Theres nothing in the function that defines what $user is
Use his function declaration instead:
function queryTimezone($user,$db)
{
$query = "SELECT timezone FROM #__camcloud_users WHERE user_id=".$user->id;
$db->setQuery($query);
$timezone = $db->loadResult();
return $timezone;
}
call this way:
Timezone: <?php echo queryTimezone($user,$db); ?> <br>
Related
I'm working on a webpage where I allow users to edit their car information. In the mainlining, there is an edit button (input - type text with a hidden key value) where it takes the user to this "edit car info" page. Initially, once the page is opened for the first time, this hidden value is used to query the database, retrieve original information and and set them as placeholders for the field. The user can write information in the input field then press the "submit edit" button which then updates the row in the database table. However, I get an error that the name of the hidden value is undefined. I don't understand how it can be undefined for the update query when it was working just fine for the select query. Can anyone shed a light on this? What should I do? This is a picture of the errors:
This is the mainlanding code: (hidden value is set here)
<?php
$mysqli= new mysqli("localhost", "root","","Car_registration");
if(empty($_SESSION)) // if the session not yet started
session_start();
if(isset($_SESSION['username'])) { // if user already logged in
header("location: mainlanding_user.php"); //send to homepage
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<title> Car Registration: User's Mainlanding </title>
<link href="css/style3.css" rel="stylesheet">
</head>
<body>
<header>
<h1>Account Information</h1>
<img id="img1" src= "image/car.jpg" alt ="car image">
</header>
<nav id='nav'>
<form action="logout.php">
<input type="submit" value=" Logout " id="button">
</form>
</nav>
<h2>Profile </h2>
<div class='container1'>
<?php
$username="root";
$password="";
$database="Car_registration";
$mysqli= new mysqli("localhost",$username,$password,$database);
$query= "select * from driver where username='".$_SESSION['logged_username']."'";
$result = $mysqli->query($query);
while( $row = $result->fetch_assoc() ){
echo "<div id='container'>" ;
echo "<dl> <dt>First Name</dt> <dd>".$row['Fname'];
echo "</dd> <br> <dt>Last name</dt><dd>".$row['Lname'];
echo "</dd> <br> <dt>License Number</dt><dd>".$row['license_no'];
echo "</dd> <br> <dt>Age</dt><dd>".$row['Age'];
echo "</dd> <br> <dt>Birthday</dt><dd>".$row['bdate'];
echo "</dd> <br> <dt>City</dt><dd>".$row['City'];
echo "</dd></dl>";
echo "</div>";
$license_no = $row['license_no']; //used for finding cars
}
?>
<div class="align-me">
<div class="form-wrapper" action="search_plate_no.php">
<form class="center">
<input class="input-fields" name="search" type="text" placeholder="Search a plate number">
<input class="input-fields submit" name="find" type="submit" value="Search">
</form>
</div>
</div>
<h3> Registered Cars </h3>
<div class='container2'>
<?php
$username="root";
$password="";
$database="Car_registration";
$mysqli= new mysqli("localhost",$username,$password,$database);
$query= "select * from cars where license_no='".$license_no."'";
$result = $mysqli->query($query);
echo "<table border=1>
<tr>
<th>Plate No.</th>
<th>License No.</th>
<th>Car Type</th>
<th>Fines</th>
<th>City</th>
<th>Edit</th>
<th>Delete</th>
</tr>";
while ($temp = $result->fetch_assoc()){
?>
<tr>
<td><?php echo $temp['Plate_no']; ?></td>
<td><?php echo $temp['license_no']; ?></td>
<td><?php echo $temp['Car_type']; ?></td>
<td><?php echo $temp['Fines']; ?></td>
<td><?php echo $temp['city']; ?></td>
<td>
<form action = "edit_car.php" method="post">
<input type="hidden" name="id" value="<?php echo $temp['Plate_no']; ?>">
<input type="submit" name="edit" value="Edit">
</form>
</td>
<td>
<form action = "delete_car.php" method="post">
<input type="hidden" name="id" value="<?php echo $temp['Plate_no']; ?>">
<input type="submit" name="delete" value="Delete">
</form>
</td>
</tr>
<?php
}
?>
</table>
</div>
<form action="register_car.php">
<input type="submit" value=" Register Car " id="button2">
</form>
<footer>
<h4> All rights belong to Car Registration Inc. </h4>
<img id="img3" src= "image/license.png" alt ="license plates image">
</footer>
</body>
</html>
Edit car page: (Error is generated here)
<!DOCTYPE html>
<html>
<head>
<title> Edit Car Information Page </title>
<link href="css/style2.css" rel="stylesheet">
</head>
<body>
<div class="container">
<header>
<h1>Edit Car Information </h1>
<img id="img1" src= "image/register.png" alt ="Registration image">
</header>
<?php
$username="root";
$password="";
$database="Car_registration";
$mysqli= new mysqli("localhost",$username,$password,$database);
$plate_no= $_POST["id"]; //This line causes an error
$_SESSION['plateNo'] = $plate_no;
$query= "select * from cars where Plate_no='".$plate_no."'";
$result = $mysqli->query($query);
while( $row = $result->fetch_assoc()){
$plate_no = $row['Plate_no'];
$car_type = $row['Car_type'];
}
?>
<main>
<h2> You can only edit the following information: </h2>
<form action="" method="post">
<label for="car_type_input">Car Type:</label>
<input type="text" placeholder="<?php echo $car_type?>" id="car_type_input" name="car_type_input"><br><br>
<div class="vertical-center">
<input type="submit" value=" Submit Edit " name="button1" id="button1">
</div>
</form>
<?php
$username="root";
$password="";
$database="Car_registration";
$mysqli= new mysqli("localhost",$username,$password,$database);
if( isset($_POST['button1']) ){ //If user changed field, take value. If not, keep old value.
if( !empty($_POST['car_type_input']) ){ //If there is user input
$car_type_2 = $_POST['car_type_input'];
$query= "update cars set Car_type='".$car_type_2."' WHERE Plate_no='".$_SESSION['plateNo']."'";
}
if ($mysqli->query($query))
echo "Fields updated successfuly!";
else
echo "Update Fields Failed!";
}
?>
</main>
<footer>
<h3> All rights belong to Car Registration Inc. </h3>
<img id="img3" src= "image/license.png" alt ="license plates image">
</footer>
</div>
</body>
</html>
Use $plate_no= $_POST['id']; instead of $plate_no= $_POST["id"];
Here why you close the while loop ??
while ($temp = $result->fetch_assoc()){
?>
and here too
<?php
}
Try this:
print"<h3> Registered Cars </h3>
<div class='container2'>";
$username="root";
$password="";
$database="Car_registration";
$mysqli= new mysqli("localhost",$username,$password,$database);
$query= "select * from cars where license_no='".$license_no."'";
$result = $mysqli->query($query);
echo "<table border=1>
<tr>
<th>Plate No.</th>
<th>License No.</th>
<th>Car Type</th>
<th>Fines</th>
<th>City</th>
<th>Edit</th>
<th>Delete</th>
</tr>";
while ($temp = $result->fetch_assoc())
{
print"
<tr>
<td><?php echo $temp['Plate_no']; ?></td>
<td><?php echo $temp['license_no']; ?></td>
<td><?php echo $temp['Car_type']; ?></td>
<td><?php echo $temp['Fines']; ?></td>
<td><?php echo $temp['city']; ?></td>
<td>
<form action = "edit_car.php" method="post">
<input type="hidden" name="id" value="<?php echo $temp['Plate_no']; ?>">
<input type="submit" name="edit" value="Edit">
</form>
</td>
<td>
<form action = "delete_car.php" method="post">
<input type="hidden" name="id" value="<?php echo $temp['Plate_no']; ?>">
<input type="submit" name="delete" value="Delete">
</form>
</td>
</tr> ";
}
print"</table>
</div>";
you are not sending id that's because error appears use this code to check if id exists first:
$plate_no='';
$car_type = '';
if(isset($_POST["id"])){
$plate_no= $_POST["id"]; //This line causes an error
$_SESSION['plateNo'] = $plate_no;
$query= "select * from cars where Plate_no='".$plate_no."'";
$result = $mysqli->query($query);
while( $row = $result->fetch_assoc()){
$plate_no = $row['Plate_no'];
$car_type = $row['Car_type'];
}
}
here is my index page.inserted all the data to the database and also show on the same page but the main problem is that on update.php page I can not retrieve the data
//that main problem is here and I can't be retrieved the data on this page and always sow that: Warning: mysql_fetch_array() expects parameter 1 to be resource, object given in C:\wamp\www\phonebook\update.php on line 12
index.php
<?php require_once('dbconnect.php'); ?>
<html>
<head>
<title> </title>
</head>
<body>
<h1> phone book </h1>
<form method="post">
<table>
<tr>
<td>fname </td><td> <input type="text" name="firstname" required /> </td>
</tr>
<tr>
<td>lname </td><td> <input type="text" name="lastname" required /> </td>
</tr>
<tr>
<td>mobile </td><td> <input type="text" name="mobile" required /> </td>
</tr>
</table>
<input type="submit" name="submit" value="submit" >
</form>
<!-- $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ show $$$$$$$$$$$$$$$$$$$$$$$$$$ -->
<br> data </br>
<table border="1">
<tr>
<th>id</th> <th>firstname</th> <th>lastname</th> <th>mobile</th><th>update</th><th>delete</th>
</tr>
<?php
$conn = mysqli_connect('localhost','root','','phonebook');
$show = mysqli_query($conn,"SELECT * FROM contacts");
while($row = mysqli_fetch_array($show))
{
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['firstname']; ?></td>
<td><?php echo $row['lastname']; ?></td>
<td><?php echo $row['mobile']; ?></td>
<td>update</td>
<td><a href="delete.php?id=<?php echo $row['id']; ?>" onclick="return confirm('sure want to delete')" >delete</a></td>
</tr>
<?php } ?>
</table>
</body>
</html>
<?php
//require_once("function.php");
//$obj = new data();
if(isset($_POST{"submit"}))
{
//echo "<pre>";print_r($_POST);die;
$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$mobile = $_POST['mobile'];
//$obj->insert($fname,$lname,$mobile);
$connect = mysqli_connect('localhost','root','','phonebook');
$insert = mysqli_query($connect,"insert into contacts(firstname,lastname,mobile) values('".$fname."','".$lname."','".$mobile."')");
if ($insert)
{ ?>
<script> alert('record inserted'); </script>
<?php
}
else
{ ?>
<script> alert('record not inserted'); </script>
<?php
}
header('Location:index.php');
}
?>
update.php
//check the code here
<?php require_once('dbconnect.php');
if(isset($_GET['id']) && is_numeric($_GET['id']) )
{
$id=$_GET['id'];
}
?>
<?php
$conn = mysqli_connect('localhost','root','','phonebook');
$result=mysqli_query($conn,"SELECT * FROM contacts WHERE id='$id'");
$fetch=mysql_fetch_array($result);
//$conn = mysqli_connect('localhost','root','','phonebook');
//$show = mysqli_query($conn,"SELECT * FROM contacts");
//while($row = mysqli_fetch_array($show))
?>
<html>
<head>
<title>update page</title>
</head>
<body>
<form method="post" name="update" action="update.php">
<table>
<tr>
<td>fname </td><td> <input type="text" name="firstname" value= "<?php echo $fetch['firstname']; ?>" required /> </td>
</tr>
<tr>
<td>lname </td><td> <input type="text" name="lastname" value="<?php echo $fetch['lastname']; ?>" required /> </td>
</tr>
<tr>
<td>mobile </td><td> <input type="text" name="mobile" value= "<?php echo $fetch['mobile']; ?>" required /> </td>
</tr>
</table>
<input type="submit" name="submit" value="submit" >
</form>
</body>
</html>
Switch to using mysqli_fetch_array() (note the i) instead of mysql_fetch_array
try this:
$conn = mysqli_connect('localhost','root','','phonebook');
$result=mysqli_query($conn,"SELECT * FROM contacts WHERE id='$id'");
$fetch=mysqli_fetch_array($result);
You must not use mysql_*, it's deprecated. Use PDO or MySQLi instead
You shouldn't mix mysql_* and mysqli_*
Just create ONE mysqli instance instead of creating it for every file you have.
Maximize the use of variables too. This way you only have to change something once.
Please sanitize/escape user input before passing it into your SQL query. Otherwise your application is vulnerable to SQL injection attacks.
I have a table where each row in the table has a button that allows you to delete that particular row from the database. Although somehow my form only submits the last row and no the one I selected. Please ignore the deprecated tags, well aware of the issues.
<?php
$result = mysql_query("SELECT * FROM booking");
while($row = mysql_fetch_array($result))
{
?>
<form class='table-form' id='form' method='post'>
<tr>
<input id="bookid" type="hidden" name="bookid" value="<?php echo ($row['booking_id']); ?>" />
<td>
<?php echo ($row['booking_id']);?>
</td>
<td>
<?php echo ($row['user_id']); ?>
</td>
<td>
<?php echo ($row['event_id']); ?>
</td>
<td>
<?php echo ($row['payment_type']); ?>
</td>
<td>
<?php echo ($row['booking_date']); ?>
</td>
<center><button type="submit" id="submit" name="submit">Cancel</button><center>
<td>
</td>
</tr>
<?php
}
?>
</form>
</table>
</div>
<?php
if (isset($_POST['bookid'])){
$id = ($_POST['bookid']);
$result = mysql_query("DELETE FROM booking
WHERE booking_id = '$id'");
}
?>
If you preffer programing this way, move </form> above
<?php
}
?>
(to cycle body).
Your code needs a lot of clean up, style cleaning and html validation. :-P
I have a script that counts how many records it sees on the page :
<script>
document.write(document.getElementById('moon').rows.length-1);
document.write (' ');
document.write (' of ');
document.write (' ');
document.write(document.getElementById('moon').rows.length-1);
document.write (' pages');
</script>
How would I use this to paginate 10 records at a time?
Perhaps using a styling such as display:none or something and then linking to these hidden divs on the "page" count.
Thanks
Edit:
I have tried to use a PHP Mysql Query to do this with num_rows, but it always throws out errors even when I am getting data!
2nd EDIT
Ok, I am pulling the information and displaying it with:
<table class="std" id="moon" border="0" cellpadding="0" cellspacing="0">
<tr>
<?php
$pcounter = 1;
$userID = LedDB::getInstance()->get_user_id_by_name($_SESSION['user']);
$presult = LedDB::getInstance()->get_page_by_campaign_id($campaignID);
$i=0;
while ($row = mysqli_fetch_array($presult)):
$style = "";
if($i%2==0)
{
$style = 'style="background-color: #EFEFEF"';
}
echo "<tr ".$style.">";
echo "<td class='camp' style='padding-left:10px;'><b><a href='editPage.php?pageID=" .htmlentities($row['pid']) ."&campaignID=" .htmlentities($row['campaignid']) ."' class='camp'> Page" . $pcounter . "</a></b></td>";
echo "<td style='padding-left:10px;'></td>";
echo "<td style='padding-left:10px;'></td>";
echo "<td></td>";
echo "<td></td>";
$pageID = $row['pid'];
//The loop is left open
?>
<td>
<div class="buttons">
<form name="editPage" action="editPage.php" method="GET">
<input type="hidden" name="campaignID" value="<?php echo $campaignID ?>"/>
<input type="hidden" name="pageID" value="<?php echo $pageID ?>"/>
<button type="submit" name="editPage" value="Edit" class="blue" >
<img src="images/edit.png" width="20" height="20"></button>
</form>
</div>
</td>
<td>
<form name="deletePage" action="deletePage.php" method="POST">
<input type="hidden" name="pageID" value="<?php echo $pageID; ?>"/>
<div class="buttons">
<button type="submit" name="deletePage" value="Delete" class="negative">
<img src="images/delete_x.png" width="20" height="20"></button>
</div>
</form>
</td>
<?php
echo "</tr>\n";
$pcounter++;
$i++;
endwhile;
mysqli_free_result($presult);
?>
</table>
Where $presult = LedDB::getInstance()->get_page_by_campaign_id($campaignID); is this:
public function get_page_by_campaign_id($campaignID) {
$campaignID = $this->real_escape_string($campaignID);
return $this->query("SELECT pid,campaignid,message_text,message FROM pages WHERE campaignid =" . $campaignID );
}
Whenever I try to wrap this in a pagination class, it either returns a 0 value or just bombs out my entire script with sql_num_rows() errors...
That is the reason I thought javascript pagination would be the best solution in this case.
There are several examples on the web to get you started, thats where I'd start:
http://www.phpclasses.org/search.html?words=pagination&x=0&y=0&go_search=1
http://www.phpsnaps.com/snaps/view/php-pagination-class/
http://www.phpbuilder.com/board/showthread.php?t=10283679
http://phpsense.com/2007/php-pagination-script/
<script type="text/javascript">
function removeLink()
{
document.getElementById("tab2").deleteRow(i);
}
</script>
</head>
<body>
<form action="forth.php" method="post">
<table width="600" border="1" id="tab2">
<?php
foreach($_POST as $post2)
{
?>
<tr>
<td>
<?php
echo $post2.'<br />';
?>
</td>
<td>Remove</td>
</tr>
<?php
}
?>
<tr>
<td><input type="submit" value="Next" /></td>
<td> </td>
</tr>
</table>
</form>
</body>
this is my page third.php it redirect user to new page forth.php on forth.php i have following code
<?php
print_r($_POST);
foreach($_POST as $key_post)
{
echo $key_post.'<br>';
}
?>
the issue is on page forth.php it doesn't print anything even when I did print_r($_POST); it returned me empty array like Array(), and help why data is not saved.
I assume it's empty because you don't actually have any <input name="foo" type="bar /> fields other than the submit button, so nothing is actually being posted.
You'll need to add some hidden fields:
foreach($_POST as $key => $post2)
{
?>
<tr>
<td>
<?php echo $post2.'<br />'; ?>
<input type="hidden" name="<?php echo $key;?>" value="<?php echo $post2;?>" />
</td>
</tr>
... etc