My PHP Form will not refresh properly - php

So when I hit submit on the form that is displayed, the page (if working properly) should refresh, and the ELSE statement should be displayed instead, but I have 2 problems
The else statement is not displayed until I manually refresh the page
The Pub Score is not updated until the page is manually refreshed either, I think my code placement might be what's causing it, but I tried to put my form as far down as I could, I'm out of ideas, any help would be great thanks.
<?php
require_once('header.php');
require_once('connectdb.php');
require_once('sessioncheck.php');
if (isset($_SESSION['user_id'])) {
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_DATA);
$user_name = mysqli_real_escape_string($dbc, trim($_GET['username']));
$query = "SELECT * FROM blah WHERE username = '$user_name'";
$data = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($data);
if (mysqli_num_rows($data) != 0) {
if ($row['havemic'] == 1) {
$micstatus = "Yes";
} else {
$micstatus = "No";
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title><?php echo $user_name . ' profile' ?></title>
</head>
<body>
<?php
$commenduser = $user_name;
$query = "SELECT * FROM blah where commenduser = '$commenduser'";
$data = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($data);
$lowerusername = strtolower($username);
$loweruser_name = strtolower($user_name);
if (mysqli_num_rows($data) == 0) {
if (isset($_POST['submit'])) {
$commendplayer = mysqli_real_escape_string($dbc, trim($_POST['commendplayer']));
$commend = mysqli_real_escape_string($dbc, trim($_POST['commend']));
$comment = mysqli_real_escape_string($dbc, trim($_POST['comment']));
if (empty($comment)) {
echo '<p class="error">Please fillout a comment before submitting</p>';
} else {
$query = "INSERT INTO commend (commendby, commenduser, comment) VALUES ('$username', '$user_name', '$comment')";
mysqli_query($dbc, $query);
if ($commend == true) {
$query = "UPDATE blah SET points=points+1 WHERE username='$user_name'";
mysqli_query($dbc, $query);
echo '<p class="success">Your commendation has been submitted with + 1 account points.</p>';
} else {
echo '<p class="success">Your commendation has been submitted with no affect on the users account points.</p>';
}
}
}
} else {
echo '<p class="success">You have already submitted a commendation for this player.</p>';
}
?>
<div id="accsettings">
<table cellpadding="5">
<tr><td><label for="username" class="reglabel">Username: </label></td>
<td><label for="username" class="reglabel"><?php echo $row['username']; ?></label></td></tr>
<tr><td><label class="reglabel">Pub Score: </label></td><td><label class="reglabel">
/*This value 'points' should be updated to the new value after form submit */
/*As well the ELSE statement near the bottom should be displayed*/
<?php echo $row['points'] ?></label></td></tr>
<tr><td><label for="steamname" class="reglabel">Steam Name: </label></td>
<td><label for="steamname" id="acclink"><?php echo '' . $row['steamname'] . ''; ?></label>
<tr><td><label for="favchar" class="reglabel">Prefered Hero: </label></td>
<td><label for="favchar" class="reglabel"><?php echo $row['favchar']; ?></label></td></tr>
<tr><td><label for="language" class="reglabel">Spoken Language: </label></td>
<td><label for="language" class="reglabel"><?php echo $row['language']; ?></label></td></tr>
<tr><td><label for="playernote" class="reglabel">Player Note: </label></td>
<td><label for="playernote" class="reglabel"><?php echo $row['note']; ?></label></td></tr>
<tr><td><label for="micstatus" class="reglabel">Has a Mic and VO-IP?</label></td>
<td><label for="micstatus" class="reglabel"><?php echo $micstatus; ?></label></td></tr>
<tr><td colspan="2">Players Comments</td></tr>
<?php
if ($row['commendby'] != $username && $lowerusername != $loweruser_name) {
?>
<tr><td><br></td></tr>
<tr><td colspan="2"><p class="success">Player Commendations/Comments</p></td></tr>
<tr><td><br></td></tr>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] . '?username=' . $user_name; ?>">
<tr><td><label for="comment">Leave a comment</label></td>
<td><input type="text" name="comment" class="regtext" /></td></td>
<tr><td colspan="2"><label for="commend" class="right">Commend Player?</label><input type="checkbox" class="right" name="commend" value="yes" /></td></tr>
<tr><td colspan="2"><input id ="submit" type="submit" class="button1" name="submit" value="Submit" /></td></tr>
</form>
<?php
} else {
/*This is what should be being displayed after the form is submitted. But it is not.*/
$query = "SELECT * FROM blah where commenduser = '$commenduser'";
$data = mysqli_query($dbc, $query);
while($row = mysqli_fetch_array($data)) {
echo '<tr><td><br></td></tr>';
echo '<tr><td><br></td></tr>';
echo '<tr><td><label class="reglabel" for="commendedbyy">Comment From: ' . $row['commendby'] . '</label></td>';
echo '<td><label class="reglabel">' . $row['comment'];
echo '<input type="hidden" name="submit" />';
echo '</form>';
}
}
?>
</table>
<?php
} else {
echo '<p class="error">' . $user_name . ' is not a registered account.</p>';
}
}
else {
echo '<p class="error">You must Log In to view this profile.</p>';
}
?>
</div>
</body>
</html>
<?php
require_once('footer.php');
?>

The $row is first grabbed from the database and then the database is updated.
You can do one of three things, the last being the simplest:
You can refactor the code to change the order
Reload the data using another query after the update
Once you update the points then update the array (i.e. do $row['points'] = $row['points'] + 1;)

Related

PHP | Object not found! Simple CRUD

I am still trying out PHP and with database capability. I'm new to stack overflow as well.
I'm having a problem with updating and deleting a row in my database but for now updating is the concern and surely deletion will follow if I manage to get assisted for this.
I have database "dbSample" and it has 3 columns "id, name, address, email".
I am having problems redirecting correctly to my update classes and it shows an error every time.
Below is the said connection file:(sample/class/dbconnection.php):
<?php
/*
==============================SQL Connection=================================
*/
$connections = mysqli_connect("localhost","root","","dbSample"); //checks database connection
if(mysqli_connect_errno()){
echo '<script type="text/javascript">alert("' . "Database Status: " . mysqli_connect_error() . '")</script>';
}
?>
Below serves as my index php file(sample/dbsample.php):
<?php require 'class/dbconnection.php';?>
<html>
<head>
</head>
<body>
<div>
<?php include 'class/dbupdate.php'; ?>
</div>
</body>
</html>
Below is the said dbupdate file:(sample/class/dbupdate.php):
<?php
/*
==============================SQL Update=================================
*/
$view_query = mysqli_query($connections, "SELECT * FROM tblSample");
echo "<table border = '1'>";
echo "<tr>
<td>Name</td>
<td>Address</td>
<td>Email</td>
<td>Option</td>
</tr>";
while($row = mysqli_fetch_assoc($view_query)){ //make variables to hold the values from the table
$user_id = $row["id"];
$db_name = $row["name"];
$db_address = $row["address"];
$db_email = $row["email"];
//get the value id and pass it
echo "<tr>
<td>$db_name</td>
<td>$db_address</td>
<td>$db_email</td>
<td><a href='class/updatepass.php?id=$user_id'>Update</a></td>
</tr>";
}
echo "</table>";
?>
Below is the said updatepass file:(sample/class/updatepass.php):
<?php
require_once(__DIR__."\dbconnection.php");
$user_id = $_REQUEST["id"];
$get_record = mysqli_query($connections, "SELECT * FROM tblSample WHERE id='$user_id'");
while($row_edit = mysqli_fetch_assoc($get_record)){
$db_name = $row_edit["name"];
$db_address = $row_edit["address"];
$db_email = $row_edit["email"];
}
?>
<form method="POST" action="class/updatenow.php">
<input type="hidden" name="user_id" value="<?php echo $user_id; ?>">
Name: <input type="text" name="new_name" value="<?php echo $db_name; ?>">
<hr />
Address: <input type="text" name="new_address" value="<?php echo $db_address; ?>">
<hr />
Email: <input type="text" name="new_email" value="<?php echo $db_email; ?>">
<hr />
<input type="submit" value="Update">
</form>
Below is the said updatenow file:(sample/class/updatenow.php):
<?php
header('Content-Type: text/plain; charset=utf-8');
require_once(__DIR__."\class\updatepass.php");
require_once(__DIR__."\class\dbconnection.php");
$user_id = $_POST["id"];
$new_name = $_POST["new_name"];
$new_address = $_POST["new_address"];
$new_email = $_POST["new_email"];
mysqli_query($connections, "UPDATE tblSample SET name='$new_name', address='$new_address', email='$new_email' WHERE id='$user_id'");
echo '<script type="text/javascript">alert("' . "Record has been updated" . '")</script>';
header('location: dbsample.php');
?>
Thank you for the help in advance, I will deeply appreciate it.

Update query not functioning

I'm trying to develop a small "To Do List" application. The data for the app is stored in a database, and it needs to perform all CRUD operations. As it is right now, Select, Insert, and Delete work just fine. I'm stuck on updating though. The index.php page is shown below:
<?php
session_start();
require_once 'connect.php';
if (isset($_POST['DeleteTask'])) {
$sqldelete = "DELETE FROM Tasks WHERE dbTaskID = :bvTaskID";
$stmtdelete = $db->prepare($sqldelete);
$stmtdelete->bindValue(':bvTaskID', $_POST['taskID']);
$stmtdelete->execute();
echo "<div>Task successfully deleted</div>";
}
if (isset($_POST['theSubmit'])){
echo '<p>New task added</p>';
$formfield['ffTaskName'] = trim($_POST['taskName']);
$formfield['ffTaskDue'] = trim($_POST['taskDue']);
if(empty($formfield['ffTaskName'])){$errormsg .= "<p>Task field is empty.</p>";}
if(empty($formfield['ffTaskDue'])){$errormsg .= "<p>Deadline field is empty.</p>";}
if ($errormsg != "") {
echo "<div class='error'><p>Please fill out all fields before submitting.</p>";
echo $errormsg;
echo "</div>";
} else {
try {
$sqlinsert = 'INSERT INTO Tasks (dbTaskName, dbTaskDue, dbTaskDone)
VALUES (:bvTaskName, :bvTaskDue, :bvTaskDone)';
$stmtinsert = $db->prepare($sqlinsert);
$stmtinsert->bindValue(':bvTaskName', $formfield['ffTaskName']);
$stmtinsert->bindValue(':bvTaskDue', $formfield['ffTaskDue']);
$stmtinsert->bindValue(':bvTaskDone', 0);
$stmtinsert->execute();
echo "<div><p>There are no errors. Thank you.</p></div>";
} catch(PDOException $e){
echo 'ERROR!!!' .$e->getMessage();
exit();
}
}
}
$sqlselect = "SELECT * from Tasks";
$result = $db->prepare($sqlselect);
$result->execute();
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>To Do Application</title>
</head>
<body>
<h1><u>To-Do List</u></h1>
<table border>
<tr>
<th>Task</th>
<th>Deadline</th>
<th>Status</th>
<th>Complete</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php
while ($row = $result->fetch()) {
if ($row['dbTaskDone'] == 0) {
$status = "Unfinished";
} else {
$status = "Finished";
}
echo '<tr><td>' . $row['dbTaskName']
. '</td><td>' . $row['dbTaskDue']
. '</td><td>' . $status;
/*if ($status == "Unfinished"){
echo '</td><td>';
echo '<form action="'. $_SERVER['PHP_SELF'] . '" method="post">';
echo '<input type="hidden" name="taskID" value"' . $row['dbTaskID'] . '">';
echo '<input type="submit" name="CompleteTask" value="Complete Task">';
echo '</form>';
}*/
echo '</td><td>';
echo '<form action="updateTask.php" method="post">';
echo '<input type="hidden" name="taskID" value="' . $row['dbTaskID'] . '">';
echo '<input type="submit" name="EditTask" id="EditTask" value="Edit Task">';
echo '</form></td><td>';
echo '<form action="'. $_SERVER['PHP_SELF'] . '" method="post">';
echo '<input type="hidden" name="taskID" value="' . $row['dbTaskID'] . '">';
echo '<input type="submit" name="DeleteTask" value="Delete Task">';
echo '</td></tr>';
}
?>
</table>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="toDoForm">
<fieldset><legend>New Task</legend>
<table>
<tr>
<th>Task</th>
<td><input type="text" name="taskName" id="taskName"
value="<?php echo $formfield['ffTaskName']; ?>"></td>
</tr><tr>
<th>Deadline</th>
<td><input type="text" name="taskDue" id="taskDue"
value="<?php echo $formfield['ffTaskDue']; ?>"></td>
</tr>
</table>
<input type="submit" name = "theSubmit" value="Add Task">
</fieldset>
</form>
</body>
</html>
Each record displays an "Edit" button that grabs the PK from the "Tasks" table and sends it to the updateTask.php page:
<?php
require_once 'connect.php';
$errormsg = "";
if (isset($_POST['EditTask']) ) {
$formfield['ffTaskID'] = $_POST['taskID'];
$sqlselect = "SELECT * FROM Tasks WHERE dbTaskId = :bvTaskID";
$result = $db->prepare($sqlselect);
$result->bindValue(':bvTaskID', $formfield['ffTaskID']);
$result->execute();
$row = $result->fetch();
if( isset($_POST['theEdit']) )
{
$formfield['ffTaskID'] = $_POST['taskID'];
$formfield['ffTaskName'] = trim($_POST['taskName']);
$formfield['ffTaskDue'] = trim($_POST['taskDue']);
if(empty($formfield['ffTaskName'])){$errormsg .= "<p>Task field is empty.</p>";}
if(empty($formfield['ffTaskDue'])){$errormsg .= "<p>Deadline field is empty.</p>";}
if ($errormsg != "") {
echo "<div class='error'><p>Please fill out all fields before submitting.</p>";
echo $errormsg;
echo "</div>";
} else {
try
{
$sqlUpdate = "UPDATE Tasks SET dbTaskName = :bvTaskName,
dbTaskDue = :bvTaskDue
WHERE dbTaskID = :bvTaskID";
$stmtUpdate = $db->prepare($sqlUpdate);
$stmtUpdate->bindvalue(':bvTaskName', $formfield['ffTaskName']);
$stmtUpdate->bindvalue(':bvTaskDue', $formfield['ffTaskDue']);
$stmtUpdate->bindvalue(':bvTaskID', $formfield['ffTaskID']);
$stmtUpdate->execute();
}
catch(PDOException $e)
{
echo 'ERROR!!!' .$e->getMessage();
exit();
}
}
}
}
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>To Do Application</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="editForm">
<fieldset><legend>Edit Task</legend>
<table>
<tr>
<th>Task</th>
<td><input type="text" name="taskName" id="taskName"
value="<?php echo $row['dbTaskName'];?>" ></td>
</tr><tr>
<th>Deadline</th>
<td><input type="text" name="taskDue" id="taskDue"
value="<?php echo $row['dbTaskDue']; ?>"></td>
</tr>
<tr>
<th>Submit Changes</th>
<input type="hidden" name="taskID" value="<?php echo $_formfield['ffTaskID']; ?>">
<td><input type="submit" name="theEdit" value="Submit Changes">
</table>
</fieldset>
</form>
</body>
</html>
The Name and Deadline fields populate appropriately based on the PK value passed from the last page. However, whenever I press the "Submit Changes" button, the update doesn't seem to execute. The page just refreshes and I see the table data remains unchanged in the database.
Solved the problem!
There were several issues that I discovered.
1.) In updateTask.php, I had the second if-statement nested within the first one. So it was running the update query as the page loaded, with no change to the data. So the 'theEdit' button did nothing since since it required the previous if statement's condition to run.
2.) The formfield 'ffTaskID' at the bottom of the form on updateTask.php to be passed on the 'theEdit' button press was typed incorrectly.
$_formfield
..should have been..
$formfield
At this point, the update query functions properly.
3.) The issue with the 'Edit' buttons has been fixed. Though I honestly can't say for certain how it was fixed. It may have been linked with the first part of the problem. So when that was fixed, so was this.
Either way, all seems to be functioning as it should. Thanks again to everyone who commented and helped.

Why are values from a url are not being passed to sticky form

I have created a php sticky form so data will not disappear when the submit button is clicked. A url link is being used to pass values to a form so they can be edited. However, the values from the url are not being passed into the form fields. Why are the values from the url not being passed into the form fields? Thank you so much for your time.
This is the code:
index.php
<?php
require_once('authorize.php');
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
require_once('appvars.php');
require_once('connectvars.php');
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$data = mysqli_query($conn, $query);
echo '<table>';
echo '<tr><th>Name</th><th>Caption</th><th>Action</th></tr>';
while ($row = mysqli_fetch_array($data)) {
//link
echo '<td><a href="link.php?id=' . $row['id'] . '&image=' . $row['image1'] . '&name=' . $row['name'] .
'&caption=' . $row['caption'] .
'&video=' . $row['video'] . '">Edit </a>';
echo '</td></tr>';
}
echo '</table>';
echo "<br><br>";
mysqli_close($conn);
?>
</body>
</html>
sticky_form.php
<!DOCTYPE html>
<html>
<head>
<title>Edit Conent</title>
</head>
<body>
<h3>Edit Conent</h3>
<?php
require_once('appvars.php');
require_once('connectvars.php');
$vid="";
$vname="";
$vcaption="";
$vvideo="";
$id ="";
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if(isset($_POST["button_edit"])){
$id = $_POST["id"];
$name = $_POST['name'];
$caption = $_POST['caption'];
$video = $_POST['video'];
$qry = mysqli_query($dbc,"Update table1 Set name='$name', caption='$caption', video='$video' Where id='$id'");
else if(isset($_GET["id"])){
$qry = mysqli_query($dbc,"Select * From table1 Where id='".$_GET["id"]."'");
while($row=mysqli_fetch_array($qry,MYSQLI_ASSOC)){
$vid=$row["id"];
$vname=$row["name"];
$vcaption=$row["caption"];
$vvideo=$row["video"];
}
}
?>
<body>
<form action='' method="post" enctype="multipart/form-data" >
<table>
<tr>
<td>ID</td>
<td><input type="text" name="id" value="<?php echo $vid;?>"></td></tr>
<tr>
<td>Name</td>
<td><input type="text" class="bigger_textbox" name="name" value="<?php if (isset($_POST['name'])) {echo htmlentities($_POST['name']);}?>"></td></tr>
<tr><td>Caption</td>
<td><input type="text" class="bigger_textbox" name="caption" value="<?php if (isset($_POST['caption'])) {echo htmlentities($_POST['caption']);}?>"></td></tr>
<tr><td>Video</td>
<td><input type="text" class="bigger_textbox" name="video" value="<?php if (isset($_POST['video'])) {echo htmlentities($_POST['video']);}?>"></td></tr>
<tr><td colspan="2">
<input type="submit" name="button_edit" value="Edit Content"></td></tr> </table>
</form>
<table border=1>
<tr><th>Name</th><th>Caption</th>
<th>Video</th> <th>Action</th></tr>
<?php
if (isset($_GET["id"])) {
$qry =mysqli_query($dbc, "Select * From table1 Where id='".$_GET["id"]."'");
while($row=mysqli_fetch_array($qry,MYSQLI_ASSOC)) {
echo '<tr><td>'.$row["name"].'</td>';
echo '<td>'.$row["caption"].'</td>';
echo '<td>'.$row["video"].'</td>';
echo '<td>Edit </td></tr>';
}
}
?>
</table>
</body>
</html>
Apparently you already have the values you need in stick_form.php:
else if(isset($_GET["id"])){
$qry = mysqli_query($dbc,"Select * From table1 Where id='".$_GET["id"]."'");
while($row=mysqli_fetch_array($qry,MYSQLI_ASSOC)){
$vid=$row["id"];
$vname=$row["name"];
$vcaption=$row["caption"];
$vvideo=$row["video"];
}
Try replacing this part of the code of stick_form.php:
<td><input type="text" class="bigger_textbox" name="name" value="<?php if (isset($_POST['name'])) {echo htmlentities($_POST['name']);}?>"></td></tr>
<tr><td>Caption</td>
<td><input type="text" class="bigger_textbox" name="caption" value="<?php if (isset($_POST['caption'])) {echo htmlentities($_POST['caption']);}?>"></td></tr>
<tr><td>Video</td>
<td><input type="text" class="bigger_textbox" name="video" value="<?php if (isset($_POST['video'])) {echo htmlentities($_POST['video']);}?>" </td></tr>
With:
<td><input type="text" class="bigger_textbox" name="name" value="<?php echo $vname; ?>"></td></tr>
<tr><td>Caption</td>
<td><input type="text" class="bigger_textbox" name="caption" value="<?php echo $vcaption; ?>"></td></tr>
<tr><td>Video</td>
<td><input type="text" class="bigger_textbox" name="video" value="<?php echo $vvideo; ?>"></td></tr>
Update
As you commented, after clicking the edit button, your form fields get empty. That's because you're not setting the correct variables in this part of your code:
if(isset($_POST["button_edit"])){
$id = $_POST["id"];
$name = $_POST['name'];
$caption = $_POST['caption'];
$video = $_POST['video'];
$qry = mysqli_query($dbc,"Update table1 Set name='$name', caption='$caption', video='$video' Where id='$id'");
Change it to:
if(isset($_POST["button_edit"])){
$vid = $_POST["id"];
$vname = $_POST['name'];
$vcaption = $_POST['caption'];
$vvideo = $_POST['video'];
$qry = mysqli_query($dbc,"Update table1 Set name='$vname', caption='$vcaption', video='$vvideo' Where id='$vid'");
Hope it helps.

PHP redirect to page on form submit based on select

I am trying to get PHP to read my select options as variables and then echo out a particular page based on the users choice.
The login system is functional but only logs me into one page.
<form action="login.php" method="POST">
<table cellspacing="10">
<tr>
<td>Email: </td>
<td><input type='text' name='email'/></td>
</tr>
<tr>
<td>Password: </td>
<td><input type='password' name='password'/></td>
</tr>
<tr>
<td>Event: </td>
<td>
<select name="event">
<option><?php echo $title?></option>
<option><?php echo $title2?></option>
</select>
</td>
</tr>
</table>
<br />
<button class="button" type='submit' name='Submit' value='Submit' />Login</button>
</form>
<?php
if (!empty($email) && !empty($password)) {
$pdo = getPdo();
$statement = $pdo->prepare('SELECT * FROM awdawda WHERE email=:email;');
$statement->bindParam(':email', $email);
$statement->execute();
$data = $statement->fetch();
$pdo = null;
if (!empty($data)) {
$dbemail = $data['email'];
$dbpassword = $data['password'];
$dbfullname = $data['fullname'];
if ($password == $dbpassword) {
$_SESSION['fullname'] = $dbfullname;
$_SESSION['email'] = $email;
$_SESSION['password'] = $password;
if ($_POST['event']) {
$event1=$title;
echo '<script type="text/javascript">window.location = "http://awda/adwa/awd1.php" </script>';
}
if ($_POST['event']) {
$event2=$title2;
echo '<script type="text/javascript">window.location = "http://awdaw/awda/adwa.php" </script>';
}
}
echo '<div style="padding: 10px;">Login Failed: Password Incorrect</div>';
}
echo '<div style="padding: 10px;">User not found</div>';
}
?>
That is the code I am using to login with. I left out what was above it since it all works fine right now.
You have to give value to the options of your select tag
<select name="event">
<option value="value1"><?php echo $title?></option>
<option value="value2"><?php echo $title2?></option>
</select>
and have to update your php logic according to these value
if ($_POST['event'] == "value1") {
$event1=$title;
echo '<script type="text/javascript">window.location = "http://awda/adwa/awd1.php" </script>';
}
if ($_POST['event'] == "value2") {
$event2=$title2;
echo '<script type="text/javascript">window.location = "http://awdaw/awda/adwa.php" </script>';
}

Updating data in PHP

I am using editing file for updating data of mobile no, email details etc, but it is not updating , it shows the results of data so connection is working but no updating of data is there. code:
<?php
include('header.php');
$msg='';
?>
<div class="page-cont1">
<!--heading starts-->
<?php
session_start(); //starts the session
if($_SESSION['user']){ //checks if user is logged in
}
else{
header("location:index.php"); // redirects if user is not logged in
}
$user = $_SESSION['user']; //assigns user value
$id_exists = false;
?>
<body>
<h2>Home Page</h2>
<p>Hello <?php Print "$user"?>!</p> <!--Displays user's name-->
Click here to logout<br/><br/>
Return to Home page
<h2 align="center">Currently Selected</h2>
<table border="1px" width="100%">
<tr>
<th>Id</th>
<th>E-Mail</th>
<th>Mobile No</th>
<th>Details</th>
<th>Extra Information</th>
</tr>
<?php
if(!empty($_GET['id']))
{
$id = $_GET['id'];
$_SESSION['id'] = $id;
$id_exists = true;
$query = mysql_query("Select * from doctor Where id='$id'"); // SQL Query
$count = mysql_num_rows($query);
if($count > 0)
{
while($row = mysql_fetch_array($query))
{
Print "<tr>";
Print '<td align="center">'. $row['id'] . "</td>";
Print '<td align="center">'. $row['your_email'] . "</td>";
Print '<td align="center">'. $row['mobile_no'] . "</td>";
Print '<td align="center">'. $row['detail'] . "</td>";
Print '<td align="center">'. $row['info'] . "</td>";
Print "</tr>";
}
}
else
{
$id_exists = false;
}
}
?>
</table>
<br/>
<?php
if($id_exists)
{
Print '
<form action="edit.php" method="POST">
Enter new E-Mail: <input type="text" name="your_email"/><br/>
Enter new Mobile: <input type="text" name="mobile_no"/><br/>
Enter new detail: <input type="text" name="detail"/><br/>
Enter new Extra Information: <input type="text" name="info"/><br/>
<input type="submit" value="Update List"/>
</form>
';
}
else
{
Print '<h2 align="center">There is no data to be edited.</h2>';
}
?>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$field_email = mysql_real_escape_string($_POST['your_email']);
$field_phone = mysql_real_escape_string($_POST['mobile_no']);
$detail = mysql_real_escape_string($_POST['detail']);
$field_message = mysql_real_escape_string($_POST['info']);
mysql_query("UPDATE doctor SET your_email='$field_email', mobile_no='$field_phone', detail='$detail', info='$field_message' WHERE id='$id'") ;
header("location: home.php");
}
?>
<?php
include('footer.php');
$msg='';
?>
</body>
More over header file includes the connect file, and one query form is there problem lie in header file or problem in edit file.
First replace DOCTOR with doctor in update query (as Utharsh has stated).
Second:
You'll have to include the id in your form to be posted.
Print '<form action="edit.php" method="POST">
Enter new E-Mail: <input type="text" name="your_email"/><br/>
Enter new Mobile: <input type="text" name="mobile_no"/><br/>
Enter new detail: <input type="text" name="detail"/><br/>
Enter new Extra Information: <input type="text" name="info"/><br/>
<input type="hidden" name="id" value="'.$id.'">
<input type="submit" value="Update List"/>
</form>';
replace DOCTOR withdoctor in your update query.
If problem persists than try to hard code your query and execute it in phpmyadmin.
put a hidden field in your form with reference to the id as jeff stated
After receiving ans i studied my code in the ans i read about ID so i noticed that in updating there is no ID reference so i changed my code now problem solved my working code is:
<?php
include('header.php');
$msg='';
?>
<div class="page-cont1">
<!--heading starts-->
<?php
session_start(); //starts the session
if($_SESSION['user']){ //checks if user is logged in
}
else{
header("location:index.php"); // redirects if user is not logged in
}
$user = $_SESSION['user']; //assigns user value
$id_exists = false;
?>
<body>
<h2>Home Page</h2>
<p>Hello <?php Print "$user"?>!</p> <!--Displays user's name-->
Click here to logout<br/><br/>
Return to Home page
<h2 align="center">Currently Selected</h2>
<table border="1px" width="100%">
<tr>
<th>Id</th>
<th>E-Mail</th>
<th>Mobile No</th>
<th>Details</th>
<th>Extra Information</th>
</tr>
<?php
if(!empty($_GET['id']))
{
$id = $_GET['id'];
$_SESSION['id'] = $id;
$id_exists = true;
$query = mysql_query("Select * from doctor Where id='$id'"); // SQL Query
$count = mysql_num_rows($query);
if($count > 0)
{
while($row = mysql_fetch_array($query))
{
Print "<tr>";
Print '<td align="center">'. $row['id'] . "</td>";
Print '<td align="center">'. $row['your_email'] . "</td>";
Print '<td align="center">'. $row['mobile_no'] . "</td>";
Print '<td align="center">'. $row['detail'] . "</td>";
Print '<td align="center">'. $row['info'] . "</td>";
Print "</tr>";
}
}
else
{
$id_exists = false;
}
}
?>
</table>
<br/>
<?php
if($id_exists)
{
Print '
<form action="edit.php" method="POST">
Enter new E-Mail: <input type="text" name="your_email"/><br/><br/>
Enter new Mobile: <input type="text" name="mobile_no"/><br/><br/>
Enter new detail: <textarea name="detail" rows="6" id="detail" style="width:200px;"></textarea><br/><br/>
Enter new Extra Information: <textarea name="info" rows="4" id="info" style="width:200px;"></textarea><br/><br/>
<input type="hidden" name="id" value="'.$id.'">
<input type="submit" value="Update List"/>
</form>
';
}
else
{
Print '<h2 align="center">There is no data to be edited.</h2>';
}
?>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$field_email = mysql_real_escape_string($_POST['your_email']);
$field_phone = mysql_real_escape_string($_POST['mobile_no']);
$detail = mysql_real_escape_string($_POST['detail']);
$field_message = mysql_real_escape_string($_POST['info']);
$id = $_SESSION['id'];
mysql_query("UPDATE doctor SET your_email='$field_email', mobile_no='$field_phone', detail='$detail', info='$field_message' WHERE id='$id'") ;
header("location: home.php");
}
?>
<?php
include('footer.php');
$msg='';
?>
</body>

Categories