PHP struggles. Error 404 - php

Trying to make a crud function/contact list. The user is brought to this page after they log in:
With a click of the edit function, I hope to take the user to a page that allows them to edit the contact details. Currently struggling to have the link function properly.
The link photo is my /view file. Which looks like this:
<?php
session_start();
if(isset($_SESSION['u_uid'])){
$uid = $_SESSION['u_id'];
require_once('connect.php');
$ReadSql = "SELECT * FROM `contact` WHERE users_id=$uid ORDER BY Name";
$res = mysqli_query($connection, $ReadSql);
?>
<head>
<title>Motoko</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">
</script>
</head>
<body>
<div class="container">
<div class="row">
<table class="table">
<tr>
<th><strong>Name</strong></th>
<th><strong>Company</strong></th>
<th><strong>Title</strong></th>
<th><strong>Phone</strong></th>
<th><strong>Email</strong></th>
<th><strong>Address</strong></th>
<th><strong>Extras</strong></th>
</tr>
<?php
while($r = mysqli_fetch_assoc($res)){
?>
<tr>
<td><?php echo $r['Name']; ?></td>
<td><?php echo $r['Company']; ?></td>
<td><?php echo $r['Title']; ?></td>
<td><?php echo $r['Phone']; ?></td>
<td><?php echo $r['Email']; ?></td>
<td><?php echo $r['Address']; ?></td>
<td>Edit</td>
<td><input type="button" onClick="deleteme(<?php echo $r['u_uid']; ?>)" name="Delete" value="Delete"></td>
</tr>
<!-- Javascript function for deleting data -->
<script language="Javascript">
function deleteme(delid)
{
if(confirm("Are you sure you want to Delete?")){
window.location.href='crud/delete.php';
}
}
</script>
<?php } ?>
</table>
</div>
</body>
</html>
<?php
}else{
header("Location: http://motoko.sorainsurance.com.au");
}
?>
This would then take them to the /update file. Which starts like:
<?php
error_reporting();
require_once('connect.php');
$id = $_GET['id'];
$SelSql = "SELECT * FROM `contact` WHERE id=$id";
$res = mysqli_query($connection, $SelSql);
$r = mysqli_fetch_assoc($res);
if(isset($_POST) & !empty($_POST)){
$name = mysqli_real_escape_string($connection,$_POST['name']);
$comp = mysqli_real_escape_string($connection,$_POST['comp']);
$title = mysqli_real_escape_string($connection,$_POST['title']);
$phone = mysqli_real_escape_string($connection,$_POST['urstel']);
$email = mysqli_real_escape_string($connection,$_POST['email']);
$location = mysqli_real_escape_string($connection,$_POST['location']);
$UpdateSql = "UPDATE `contact` SET Name='$name', Company='$comp',
Title='$title', Phone='$phone', Email='$email', Address='$location' WHERE id=$id";
$res = mysqli_query($connection, $UpdateSql);
if($res){
echo $smsg = "Successfully updated data.";
echo header('location: view.php');
}else{
echo $fmsg = "Failed to update data.";
}
}
Any idea why the error keeps popping up? So confused and struggling to progress :(
Many thanks!

404 error code indicate that, you are not request correct URL.
This would then take them to the /update file
You mentioned, the request will go to update file, But your request URL is crud/update.php.
Now, request URL depends on relativity of the file. So, whether you view file exists at root folder and update.php under crud folder ?
==Edited==
(referring your latest comment)It seems like, your update.php and view.php exists in same folder, then you should simply change the request
crud/update.php
to
update.php
You don't need to mention the folder path in your request.

Related

edit table with php and jquery

I'm trying to modify the contents of a cell in a table with php and ajax.
the problem that the content has been changed in the page but it is not registered in the database.
this is the page index.php:
<?php
include 'connexion.php';
$sql = 'SELECT * FROM liste_user_tbl';
$result = mysql_query($sql) or die(__LINE__.mysql_error().$sql);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<title>Modification "inline" de données</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script>
$(function(){
var message_status = $("#status");
$("td[contenteditable=true]").blur(function(){
var field_userid = $(this).attr("id") ;
var value = $(this).text() ;
$.post('test1.php' , field_userid + "=" + value, function(data){
if(data != '')
{
message_status.show();
message_status.text(data);
setTimeout(function(){message_status.hide()},3000);
}
});
});
</script>
</head>
<body>
<h1>Utilisateurs</h1>
<table id="table-utilisateurs">
<tr>
<th>Nom</th>
<th>Prénom</th>
</tr>
<?php
while($user = mysql_fetch_assoc($result))
{
?>
<tr>
<td id="<?php echo $user['id']; ?>" contenteditable="true">
<?php echo $user['nom']; ?>
</td>
<td id="<?php echo $user['id']; ?>" contenteditable="true">
<?php echo $user['prenom']; ?>
</td>
</tr>
<?php
}
?>
</table>
</body>
</html>
<?php
mysql_close();
?>
this is test1.php:
<?php
if(!empty($_POST))
{
include "connexion.php";
foreach($_POST as $field_name => $val)
{
//clean post values
$field_userid = strip_tags(trim($field_name));
$val = strip_tags(trim(mysql_real_escape_string($val)));
//from the fieldname:user_id we need to get user_id
$split_data = explode(':', $field_userid);
$user_id = $split_data[1];
$field_name = $split_data[0];
if(!empty($user_id) && !empty($field_name) && !empty($val))
{
//update the values
mysql_query("UPDATE liste_user_tbl SET $field_name = '$val' WHERE id = $user_id") or mysql_error();
echo "Updated";
} else {
echo "Invalid Requests";
}
}
} else {
echo "Invalid Requests";
}
?>
this is my table :
First of all, $field_name is not defined in your code, that's why the query will produce an error if you enable debugging
Second of all, your code is very vulnerable to SQL injections. Your are using user's posted data as it is, without any filtering, and this can lead to loosing your entire database data.
Third of all, you are still using procedural php and "old schoool" database connection. Instead, you can use PDO and POO
use
print_r($_POST);
to receive post data and display
check if the post data has a problem

Why i can see login form if im already login

Hi guys can i ask what is the problem on my code.
all i want is if there is someone already login then if i click back i don`t want to see the login form what is the problem in my code thank you for you help
//php for check if i login
<link rel="stylesheet" href="bootstrap.min.css" />
<?php
if (!isset($_SESSION["member_id"])) {
header("Location:index.php");
}
?>
//html
<?php
session_start();
require_once("check_login.php");
?>
<!DOCTYPE html>
<html>
<head>
<title>All Members</title>
<link rel="stylesheet" href="bootstrap.min.css" />
<script>
function confirmDelete() {
if (!confirm("Are you sure you want to delete this member?")) {
return false;
}
}
</script>
</head>
<body>
<?php require_once("top_nav.php"); ?>
<div class="container">
<h1>All Members</h1>
<table class="table table-striped">
<th>ID</th>
<th>Email</th>
<th>Name</th>
<th></th>
<?php
require_once("db_open.php");
$sql = "SELECT * FROM members";
$result = $conn->query($sql) or die($conn->error);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$delete_link = "delete_member_db.php?member_id=".$row["member_id"];
echo "<tr>";
echo "<td>".$row["member_id"]."</td>";
echo "<td>".$row["member_email"]."</td>";
echo "<td>".$row["member_full_name"]."</td>";
echo "<td><a href='".$delete_link."' onClick='return confirmDelete();' class='btn btn-danger btn-xs'>Delete</a></td>";
echo "</tr>";
}
} else {
echo "<p>No members to show...</p>";
}
require_once("db_close.php");
?>
</table>
</div>
</body>
</html>
My apologies, I didn't get your question exactly but as per my understanding, in order to avoid login page view if user is logged in, you have to check with another condition on login page that,
if (isset($_SESSION["member_id"])) {
header("Location:some_page_name.php");
}
I think, You have checked condition
if (!isset($_SESSION["member_id"])) {
header("Location:index.php");
}
which is useful for pages visible after logging in...

How to change database values from front end in PHP or Jquery?

I have php code which displays a table, the contents of which are displayed from a table in the database. One of the fields is a status field. There is a "Change" button which should change an "Inactive" status to "Active" and vice versa.
I need to know how to go about writing the code for the "change" button. Whether it should be written in jquery or it can be written in php also.
The code is as follows:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="indexStyle.css">
<title>User List</title>
</head>
<body>
<table border="2" bgcolor="#50D5CE">
<tr>
<td>SL. No</td>
<td>Name</td>
<td>Mobile Number</td>
<td>Status</td>
</tr>
<?php include ('header.php');
include ('includes/connect.php');
$dbconn = new connect();
$fields = "first_name,last_name,mobile_number,status";
$user_details = $dbconn->select($fields,"user_details","1");
$c=1;
for($i = 0; $i < count($user_details);$i++){
?>
<tr>
<td><?php echo $c ?></td>
<td><?php echo $user_details[$i][0]." ".$user_details[$i][1]; ?></td>
<td><?php echo $user_details[$i][2] ?></td>
<td><?php if($user_details[$i][3] == 0) {
echo "Inactive"; }
else echo "Active"; ?>
<input type="button" name="change_status" id ="change_status" value="Change"/></td>
</tr>
<?php $c++;
} ?>
</table>
</body>
</html>
jQuery is Javascript and runs on the user's computer, while PHP runs on the server. The thing is that the user has to trigger the database update and the server has to execute it. As a result, you need to send an AJAX request using jQuery and your server-side has to execute the update.
change input: ↓
<input type="button" name="change_status" id ="change_status" value="Change" onclick="update_status(<?php echo $user_details[$i][2]; ?>,<?php echo $user_details[$i][3]; ?>)"/>
<script>
function update_status(mob_no,sta)
{
$.ajax({
type: "POST",
url: "update_status.php",
data: {mobno:mob_no,status:sta},
success: function(data){
// whatever you do here after success
}
}
</script>
UPDATE_STATUS.PHP
<?php
if(isset($_POST['mobno']) && isset($_POST['status']))
{
$mono= $_POST['mobno'];
$temp_status = 0;
if($_POST['status']==0)
$temp_status=1;
mysql_query("update user_details set status='$temp_status' where mobile_number ='$mono'");
}
?>
You can change it with PHP code or with jQuery AND PHP code.
This because you'll need a server-side langage (in your case PHP) to access your DB. So the server-side script is needed.
To run the server-side script, you can call a php page directly, or you can call it from an AJAX request (in your case jQuery).

How to use jquery simple modal plugin to edit form data in zend framework?

I am developing a simple students information application. I have student list in my index.phtml and I can edit student information form there. It's working nice but I want to use modal for edition student info. But I don't know how to do this.Below I have posted my current codes :
/// indexController.php
public function editAction()
{
$modelStudents = new Application_Model_DbTable_Students();
$id = (int) $this->_getParam('id');
$student = $modelStudents->fetch($id);
$form = new Application_Form_Student($student->email);
$form->submit->setLabel('Save');
$this->view->form = $form;
if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData)) {
$id = (int)$form->getValue('id');
$name = $form->getValue('name');
$email = $form->getValue('email');
$phone = $form->getValue('phone');
$students = new Application_Model_DbTable_Students();
$students->updateStudent($id, $name, $email, $phone);
$this->_helper->redirector('index');
} else {
$form->populate($formData);
}
} else {
$id = $this->_getParam('id', 0);
if ($id > 0) {
$form->populate($modelStudents->getStudent($id));
}
}
}
/// index.phtml
<html>
<head>
<style type="text/css" rel="stylesheet">
</style>
<script type="text/javascript" src="jQuery.js"> </script>
<script type ="text/javascript" src="jquery.simplemodal-1.4.2.js" ></script>
<script>
<?php
echo "Student List";
?>
<p><a href="<?php echo $this->url(array('controller'=>'index',
'action'=>'add'));?>">Add new student</a></p>
<table border="1">
<tr>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Action</th>
</tr>
<?php foreach($this->students as $student) : ?>
<tr>
<td><?php echo $student->name;?></td>
<td><?php echo $student->email;?></td>
<td><?php echo $student->phone;?></td>
<td>
<a href="<?php echo $this->url(array('controller'=>'index',
'action'=>'edit', 'id'=>$student->id));?>">Edit</a>
<a href="<?php echo $this->url(array('controller'=>'index',
'action'=>'delete', 'id'=>$student->id));?>">Delete</a>
<p id="delete" > Delete </p>
</td>
</tr>
<?php endforeach; ?>
</table>
</center>
</html>
////edit.phtml
<center>
<?php
echo "Edit Student List ";
echo $this->form ;
?>
</center>
Please let me know how to use jQuery simple modal plugin to preform this task.
Thanks in advance
Start by editing your index.phtml file to add a class and the student id to your edit link:
<a href="<?php echo $this->url(array('controller'=>'index',
'action'=>'edit', 'id'=>$student->id));?>" class="editBtn" studentId="<?=$student->id?>">Edit</a>
Then, in an included js file:
$(document).ready(function() {
$(function() {
//Event triggered when clicking on the edit btn
$('.editBtn').click(function(event) {
event.preventDefault ? event.preventDefault() : event.returnValue = false;
//Get the page and add it to the modal
studentId = $(this).attr('studentId');
var data={};
data['id'] = studentId;
url = "/index/edit/";
$.get(url, data, function(resp) {
$.modal(resp);
});
});
});
});
This should help you get started...

form hidden value not working?

I have a simple table that prints our the list of videos and plays them when you click on the row. I have a delete button to delete the corresponding video in the row. But for some reason when you click on any video it will only delete the last one in the list. I can look at the source on the page and the video_url seems to be correct. Any ideas?
Here is the majority of the code:
<?php
// Dont allow direct linking
defined('_JEXEC') or die('Direct Access to this location is not allowed.');
//get current user
$user =& JFactory::getUser();
// get a reference to the database
$db = &JFactory::getDBO();
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
echo $problem;
}
return $data;
}
if (isset($_POST['delete_video'])) {
$video_url = check_input($_POST['video_url']); //value here is always just the last row in the table!!!
echo $video_url;
$query_delete_video = "DELETE FROM `#__videos` WHERE `video_url`='$video_url'";
$db->setQuery($query_delete_video);
$db->query();
}
//list all details of the camera including camera_name
$query_videos = "SELECT #__videos.video_url, #__videos.video_size, #__videos.video_datetime, #__videos.video_length, #__cameras.camera_name
FROM #__videos INNER JOIN #__cameras using (user_id, camera_id)
WHERE #__videos.user_id=".$user->id." ORDER BY #__videos.video_datetime DESC";
$db->setQuery($query_videos);
//get number of cameras so we can build the table accordingly
$db->query();
$num_videos = $db->getNumRows();
// We can use array names with loadAssocList.
$result_videos = $db->loadAssocList();
echo "<html>";
echo "<head>";
?>
<link href="recordings/recordings.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="flowplayer/example/flowplayer-3.2.6.min.js"> </script>
<?php
echo "</head>";
echo "<body>";
?>
<?php
if (!isset($result_videos))
{
//TODO check if query failed
}
else
{
if ($num_videos == 0)
{
?>
<div id="cc_table">
<table id="webcam-table">
<thead>
<tr>
<th>Camera Name</th>
<th>Camera Details</th>
<th>Video Options</th>
</tr>
</thead>
<td colspan=3><b><i><center>You currently have no videos created. Start monitoring today!</center></i></b></td>
</table>
</div>
<?php
}
else
{
?>
<div id="cc_table">
<form name="myform" action="<?php echo htmlentities($_SERVER['REQUEST_URI']); ?>" method="POST">
<table id="webcam-table">
<thead>
<tr>
<th>Camera Name</th>
<th>Camera Details</th>
<th>Video Options</th>
</tr>
</thead>
<tbody>
<?php
for($i=0;$i<$num_videos;$i++)
{
?>
<tr onclick="DoNav('<?php echo $result_videos[$i]["video_url"]; ?>');">
<td>
<?php echo $result_videos[$i]["camera_name"]; ?>
</td>
<td>
Date Created: <?php echo $result_videos[$i]["video_datetime"]; ?> <br>
Video Size: <?php echo $result_videos[$i]["video_size"]; ?> bytes <br>
Video Length: <?php echo $result_videos[$i]["video_length"]; ?> secs
</td>
<td>
<input type="submit" name="delete_video" value="Delete" onClick="return confirm('Are you sure you want to delete?')"/>
</td>
</tr>
<input type="hidden" name="video_url" value="<?php echo $result_videos[$i]["video_url"]; ?>" />
<?php
}
echo "</tbody>";
echo "</table>";
echo "</form>";
echo "</div>";
}
}
?>
<div id="player" style="display:block;width:320px;height:240px;background-image:url(recordings/landscape.jpg)"></div>
<script type="text/javascript">
function DoNav(theUrl)
{
//document.write(document.location.href = theUrl);
flowplayer("player", "flowplayer/flowplayer-3.2.7.swf", theUrl);
}
</script>
<?php
echo "</body>";
echo "</html>";
?>
Can you post the HTML? I suspect you've got multiple hidden input fields on the page, all with the same name. If that's the case, only the last one that loads on the page will register as a valid hidden input. A better way to do it would be to set a data attribute on the delete button, and then add a JS click event to that delete button. Something like this:
<button class="delete" data-video_url="youtube.com/abcd">Delete ABCD</button>
<button class="delete" data-video_url="vimeo.com/xyz">Delete XYZ</button>
and then when someone clicks on a .delete button, you send the data-video_url value to the PHP script via POST. A jQuery example might look like this:
$('.delete').click(function() {
var url = $(this).data('video_url');
$.post('/delete.php', { video_url: video_url }, function() {
// Do something on success
});
});
Another way to do it is to simply make each button its own form:
<form method="post" action="delete.php">
<input type="hidden" name="video_url" value="url_goes_here">
<button>Delete</button>
</form>
Hope that helps. Good luck.
The problem is that the name of the hidden field is not unique (for every iteration in the loop a new hidden field with the same name is created). How should the script know which row you want if all of them has the same name?
Your code does not seem to name the hidden values properly.
You should use $i to name the variable.
input type="hidden" name="video_url<?php echo $i; ?>"
then in handler you can do
<?php
if ($_POST) {
$kv = array();
foreach ($_POST as $key => $value) {
// check here which one begins with video_url
}
}
?>

Categories