Mysql query succeeds but doesnt reflect in the database - php

Please guys my code isn't working and i don't know what to do anymore. All i'm trying to accomplish is a simple mysql update using php. The header query value works at the top of my code which i use to insert values in the form, but when i attempt to submit, for some reason unknown to me, my script succeeds but it doesn't update the database. The result of clicking the submit will be my "Javascript success alert" then " all the variables in my page become Undefined. My script is below.
<?php
//Catch the id from the header and query database
if(isset($_GET['id']))
$workID= mysqli_real_escape_string($connection, $_GET['id']);
$query = "SELECT * FROM music WHERE musicID = $workID";
$result= mysqli_query($connection, $query);
$row = mysqli_fetch_array($result);
$artID = $row['IDartiste'];
$query2 = mysqli_query($connection,"SELECT music.*, artiste.* FROM music INNER JOIN artiste ON music.IDartiste=artiste.artisteID WHERE artiste.artisteID = $artID");
$row2 = mysqli_fetch_array($query2);
?>
**This is the form for editing**
<form role="form" method="get" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<!-- Artiste name field--> <div class="form-group">
<span><label for="topic" style="color: #55AAFF">Artiste ID: </span> <span><?php echo $row['IDartiste'];?></span></label>
<!-- Article topic field--> <div class="form-group">
<label for="topic" style="color: #55AAFF">Music title:</label>
<input type="text" class="form-control" name="title" value="<?php echo $row['musicTitle'];?>" autofocus>
</div>
<!--Article textarea--> <div class="form-group">
<label for="body" style="color: #55AAFF">Content (text):</label>
<textarea name="article" class="form-control"><?php echo $row['musicArticle'];?>
</textarea>
</div>
<!-- Upload image start--> <div class="col-ms-6 col-xs-12" >
<div class="panel panel-default">
<div class="panel-heading" style="color: #55AAFF">Upload Image</div>
<div class="panel-body">
<iframe src="fileupload.php" width="100%" style="border:none"></iframe>
</div>
</div>
</div>
<!--Image link field--> <div class="form-group">
<label for="image_link" style="color: #55AAFF">Input image link: </label>
<input type="text" name="image" value="<?php echo $row['musicPhoto'];?>" class="form-control"/>
</div>
<!-- Upload music start--> <div class="col-ms-6 col-xs-12" >
<div class="panel panel-default">
<div class="panel-heading" style="color: #55AAFF">Upload Music</div>
<div class="panel-body">
<iframe src="music_file_upload.php" width="100%" style="border:none"></iframe>
</div>
</div>
</div>
<!--Music link field--> <div class="form-group">
<label for="music_link" style="color: #55AAFF">Input music link: </label>
<input type="text" name="file" class="form-control" value="<?php echo $row['musicFile'];?>"/>
</div>
<center>....<button type="submit" class="btn btn-default" id="btnsubmit" name="btnsubmit" style="color: #55AAFF">Update</button>....</center>
<br />
</form>
This is the update script
<?php
if(isset($_GET['btnsubmit'])){
date_default_timezone_set('Africa/Lagos');
$setdate= date('Y-m-d-h-m-s');
$date= strftime($setdate);
$time= strftime('%X');
$title = $_GET["title"];
$article = $_GET["article"];
$artisteImage = $_GET["image"];
$musicFile = $_GET["file"];
$sql_music = "UPDATE kingdomjoy.music SET musicTitle = '$title',
musicFile = '$musicFile',
musicPhoto = '$artisteImage',
musicArticle = '$article',
entryDate = '$date'
WHERE musicID = '$workID' LIMIT 1";
$musicupdate = $connection->query($sql_music);
if ($musicupdate === FALSE) {
echo "Error: " . die(mysqli_error($connection));}
else {echo"<script>swal('Success! music library updated')</script>";}
}
?>

Related

Php get from data which runs inside loop fetched from database

Hello i have a php code where i fetch my data from database and loop it around with each form. now i want to get the value from each looped form data. here is my code:
$query = "SELECT question,type,option1,option2,option3,option4,option5,option6,answer FROM question WHERE exam_id = '$exam_id'";
$result = mysqli_query($connection, $query);
while($row=mysqli_fetch_assoc($result))
{
if ($row['type'] == "true/false") {
echo '
<form class="form-horizontal" role="form" method="POST" action="">
<div class="form-group">
<div class="col-sm-10">
<p>'. $row["question"] . ' </p>
</div>
</div>
<div id="form-label">
<p class="alignleft"><b>Mark this question as:</b></p>
<div style="clear: both;"></div>
</div>
<!-- Text input-->
<div class="form-group">
<div class="col-md-2">
<input type="radio" placeholder="" name="answer" value = "true" id="" required> True
</div>
</div>
<!-- Text input-->
<div class="form-group">
<div class="col-md-2">
<input type="radio" placeholder="" name="answer" value= "false" id=""> False
</form>';
Maybe i understand what do you want.
You must to use only 1 form, and use array of object for many input (question).
question_page.php
$query = "SELECT question,type,option1,option2,option3,option4,option5,option6,answer FROM question WHERE exam_id = '$exam_id'";
$result = mysqli_query($connection, $query);
echo '<form class="form-horizontal" role="form" method="POST" action="response_question_page.php">';
while($row=mysqli_fetch_assoc($result))
{
if ($row['type'] == "true/false") {
echo '
<div class="form-group">
<div class="col-sm-10">
<p>'. $row["question"] . ' </p>
</div>
</div>
<div id="form-label">
<p class="alignleft"><b>Mark this question as:</b></p>
<div style="clear: both;"></div>
</div>
<!-- Text input-->
<div class="form-group">
<div class="col-md-2">
<input type="radio" placeholder="" name="question[][answer]" value = "true" id="" required> True
</div>
</div>
<!-- Text input-->
<div class="form-group">
<div class="col-md-2">
<input type="radio" placeholder="" name="question[][answer]" value= "false" id=""> False';
echo '<input type="hidden" name="question[][question]" value="'.$row["question"].'">';
}
}
echo '<input type="submit" value="Send all response"></form>';
in your response_question_page.php the object passed is:
{"question" => [
{
"question" => "what is your name",
"answer" => "true"
}
]}

PHP post request from HTML page showing blank input fields

Currently looking to implement functionality to edit details in MySQL database via a HTML page. The page itself shows all data in the database which matches the unique id of the user who is is logged in via a PHP session and echos that data to input boxes in a while loop.
When the user makes changes to the input text and hits the save changes link it then calls the edit endpoint which in turn calls the edit SQL function in a functions file.
I'm using an anchor tag wrapped in a button to send the id of the row that is being edited and all this sits inside a POST action form.
However the input texts are only showing as blank as if the endpoint is not receiving the text in the input field, and despite trying quite a few different methods I can't seem to get a result.
Code for Web page (not whole page but only concerned code)
<?php
$connect =mysqli_connect('localhost','root','','micaddy');
$id_query = mysqli_query($connect, "SELECT unique_id FROM users WHERE email = '{$_SESSION['login_user']}'");
$id_array = mysqli_fetch_assoc($id_query);
$uid = $id_array['unique_id'];
$result = mysqli_query($connect, "SELECT * FROM clubs WHERE user_id =
'$uid'");
?>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading clearfix"><h3 class="panel-title"><strong>Your Golfbag</strong><button type="button" class="btn btn-info btn-lg pull-right" data-toggle="modal" data-target="#addModal">Add Club</button></h3></div>
<?php while($row=mysqli_fetch_assoc($result)):?>
&nbsp <span><?php if(isset($_SESSION['message'])){ echo $_SESSION['message']; unset($_SESSION['message']);} ?></span>
<div class="panel-body">
<div class="container-fluid">
<div class="row">
<div class="col-md-5">
<div class="panel panel-default">
<div class="panel-heading"><h3 class="panel-title"><strong><?php echo $row['club_type'];?></strong></h3></div>
<div class="panel-body">
<form id="" method="POST" action="editClub.php">
<div class="form-group">
<label for="clubType">Club ID</label>
<input type="text" readonly="" class="form-control" id="inputClubType" value="<?php echo $row['id'];?>" name="clubIdInput">
</div>
<div class="form-group">
<label for="clubBrand">Type</label>
<input type="text" class="form-control" id="inputclubBrand" value="<?php echo $row['club_type'];?>" name="clubTypeInput">
</div>
<div class="form-group">
<label for="clubBrand">Brand</label>
<input type="text" class="form-control" id="inputclubBrand" value="<?php echo $row['brand'];?>" name="clubBrandInput">
</div>
<div class="form-group">
<label for="clubNum">Number or Type</label>
<input type="text" class="form-control" id="inputclubNum" value="<?php echo $row['club_number'];?>" name="clubNumInput">
</div>
<div id="deleteClub">
<button id="submitChange" type="button" class="btn btn-danger btn-lg"><?php echo "<a href='deleteClub.php?id=".$row['id']."'>Delete</a>" ?></button>
<button type="button" class="btn btn-info btn-lg"><?php echo "<a href='editClub.php?id=".$row['id']."'>Save Changes</a>" ?></button>
</div>
<span><?php if(isset($_SESSION['message'])){ echo $_SESSION['message']; unset($_SESSION['message']);} ?></span>
</form>
</div>
</div>
</div>
<div class="col-md-5">
<div class="panel panel-default">
<div class="panel-heading"><h3 class="panel-title"><strong>Club Image</strong></h3></div>
<div class="panel-body">
<div class="form-group">
<img src="club_images/<?php echo $row['clubImg']; ?>" class="img-rounded" width="250px" height="250px" alt="Image"/>
</div>
</div>
</div>
</div>
</div>
</div>
<span><?php if(isset($_SESSION['message'])){ echo $_SESSION['message']; unset($_SESSION['message']);} ?></span>
</div>
<?php endwhile;?>
</div>
</div>
</div>
</div>
The edit endpoint:
<?php
session_start();
$error='';
require_once '../include/DB_Functions.php';
$db = new DB_Functions();
if(empty($_POST['clubBrandInput']) || empty($_POST['clubNumInput'])){
$_SESSION['message'] = "Warning: Some fields are blank! Please try again";
header("Location: golfbag.php");
} else{
if(isset($_POST['clubBrandInput']) && isset($_POST['clubTypeInput']) && isset($_POST['clubNumInput'])){
$brand = $_POST['clubBrandInput'];
$type = $_POST['clubTypeInput'];
$num = $_POST['clubNumInput'];
$id = $_GET['id'];
$club = $db->editclub($brand, $type, $num, $id);
if($club) {
header("Location: golfbag.php");
$_SESSION['message'] = "Success! Details edited.";
}else{
header("Location: golfbag.php");
echo $error;
}
}
}
?>
The function method:
public function editClub($brand, $type, $num, $id){
$stmt = $this->conn->prepare("UPDATE clubs SET brand = '$brand', club_type = '$type', club_number = '$num' WHERE id = '$id'");
$result = $stmt->execute();
$stmt->close();
if($result){
$stmt = $this->conn->prepare("SELECT * FROM clubs WHERE user_id = ?");
$stmt->bind_param("s", $uid);
$stmt->execute();
$club = $stmt->get_result()->fetch_assoc();
$stmt->close();
return $club;
}else{
return false;
}
}
You do not have a <form> defined in this HTML.
You are also clicking an anchor link <button id="submitChange" type="button" class="btn btn-danger btn-lg"><?php echo "<a href='deleteClub.php?id=".$row['id']."'>Delete</a>" ?></button>
even though it is in a button.
Therefore you will only pass the id=".$row['id']." parameter to the endpoint and that will be passed in the $_GET array and not the $_POST array

Profile page not displaying user profile information or updating [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hope someone can help. I have a profile page that I want to display the logged in users details. So far I have this on the Profile page.
<?php
/* This script pulls the existing name input and displays it when the user logs in. */
session_start();
include("db.php"); ?>
<?php include("includes/header.php") ?>
<?php include("includes/nav.php") ?>
<?php
if(logged_in()) {
$result = mysqli_query($link,$query);
$row = mysqli_fetch_array($result);
if (!$_POST['name'] && $_POST['name']=="") $error.="<br />Please enter your name";
if (!$_POST['email'] && $_POST['email']=="") $error.="<br />Please enter your email";
if (!$_POST['DOB'] && $_POST['DOB']=="") $error.="<br />Please enter your date of birth";
if (!$_POST['country'] && $_POST['country']=="") $error.="<br />Please enter your country";
if ($error) {
echo '<div class="alert alert-success alert-dismissable">'.addslashes($error).'</div>';
}
if(isset($_POST['form-control'])) {
move_uploaded_file($_FILES['file']['tmp_name'],"img/".$_FILES['file']['name']);
$query = mysqli_query("UPDATE users SET image = '".$_FILES['file']['name']."'");
}
} else {
redirect("login.php");
}
?>
<Style>
.alert{
display:none;
}
#profileimg {
height: 100px;
width: auto;
}
</Style>
<div class="container">
<h1>Edit Profile</h1>
<hr>
<div class="row">
<!-- left column -->
<div class="col-md-3">
<div class="text-center">
<img src="//placehold.it/100" class="avatar img-circle" alt="avatar" id="profileimg">
<h6>Upload a different photo...</h6>
<input class="form-control" type="file" name="name">
</div>
</div>
<!-- edit form column -->
<div class="col-md-9 personal-info">
<div class="alert alert-success alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<strong>Profile updated.</strong>
</div>
<h3>Personal info</h3>
<form class="form-horizontal" role="form" action="edit_profile.php" method="post">
<div class="form-group">
<label class="col-lg-3 control-label name">name:</label>
<div class="col-lg-8">
<input class="form-control" value="<?php echo $row['name'];?>" type="text" name="name" required>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Email:</label>
<div class="col-lg-8">
<input class="form-control" value="<?php echo $row['email'];?>" type="text" name="email" required>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">DOB:</label>
<div class="col-lg-8">
<input class="form-control" value="<?php echo $row['DOB'];?>" type="date" name="DOB" required>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Country</label>
<div class="col-lg-8">
<input class="form-control" value="<?php echo $row['country'];?>" type="text" name="country" required>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<input class="btn btn-primary" value="Save Changes" type="submit">
<span></span>
<input class="btn btn-default" id="updated" value="Cancel" type="reset">
</div>
</div>
</form>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
<script>
$("#updated").click(function(){
$(".alert").hide().show('medium');
</script>
</body>
</html>
I then have another php file for the updating which is this:
<?php
session_start();
include("db.php");
$name = $_POST['name'];
$email = $_POST['email'];
$DOB = $_POST['DOB'];
$country = $_POST['country'];
$password = md5($salt.$_POST['password']);
$query = "UPDATE users SET name = '".$name."', email = '".$email."', DOB = '".$DOB."', country = '".$country."', password = '".$password."'";
$result = mysqli_query($link,$query);
header('Location: profile.php');
?>
So the short is it doesn't display or update and I am not sure why. I am new to PHP so go easy on me if this is simple, I have searched but can't seem to find the answer.
Thanks in advance.
Im also new to this but normally when I check if a SESSION id is active I use
if(isset($_SESSION['id'])) {
$query = "UPDATE users SET name = '".$name."', email = '".$email."', DOB = '".$DOB."', country = '".$country."', password = '".$password."' WHERE id='".$_SESSION['id']."'";
}
You also need to echo back the indexed rows that you are trying to query to display results
$name = row['username'];
echo $name;
There are lots of errors in your code: You are trying to upload a file in the same page whereas you send the form data to another page. How you handle form validation is also a little overhead. What I did change in the form is: I add name="save" in your submit button and added new hidden input for storing your user profile id. I am not sure what login() function did in your code, better stick to if($id){}.
Try this:
<?php
/* This script pulls the existing name input
and displays it when the user logs in. */
session_start();
include("db.php");
include("includes/header.php");
include("includes/nav.php");
$id = $_SESSION['id'];
if(loginned()) {//you can do if($id){}
$query="SELECT * FROM users WHERE id='$id' LIMIT 1";
$result = mysqli_query($link,$query);
$row = mysqli_fetch_array($result);
?>
<style>
.alert{
display:none;
}
#profileimg {
height: 100px;
width: auto;
}
</style>
<div class="container">
<h1>Edit Profile</h1>
<hr>
<div class="row">
<!-- left column -->
<!-- edit form column -->
<div class="col-md-9 personal-info">
<div class="alert alert-success alert-dismissable">
<button type="button" class="close" data-dismiss="alert"
aria-hidden="true">×</button>
<strong>Profile updated.</strong>
</div>
<h3>Personal info</h3>
<form class="form-horizontal" role="form"
action="edit_profile.php" method="post">
<div class="form-group">
<label class="col-lg-3 control-label name">name:</label>
<div class="col-lg-8">
<input class="form-control" value="<?php echo $row['name'];?>"
type="text" name="name" required>
</div>
</div>
<div class="col-md-3">
<div class="text-center">
<img src="//placehold.it/100" class="avatar
img-circle" alt="avatar" id="profileimg">
<h6>Upload a different photo...</h6>
<input class="form-control" type="file" name="name">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Email:</label>
<div class="col-lg-8">
<input class="form-control" value="<?php echo $row['email'];?>"
type="text" name="email" required>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">DOB:</label>
<div class="col-lg-8">
<input class="form-control" value="<?php echo $row['DOB'];?>"
type="date" name="DOB" required>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Country</label>
<div class="col-lg-8">
<input class="form-control" value="<?php echo $row['country'];?>"
type="text" name="country" required>
</div>
</div>
<div class="form-group">
<input type="hidden" name="id" value="<?php echo $row['id'];?>">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<input class="btn btn-primary" name="save"
value="Save Changes" type="submit">
<span></span>
<input class="btn btn-default" id="updated"
value="Cancel" type="reset">
</div>
</div>
</form>
</div>
<?php }else{ redirect("login.php"); } ?>
edit_profile.php First we check whether any post with a name of save is there, We validate the posted data. if validation is successful, we proceed to upload your file and then run your update query.
<?php
session_start();
include("db.php");
if(isset($_POST['save'])){
$id = isset($_POST['id'])? $_POST['id']:'';
$name = isset($_POST['name'])? $_POST['name']:'';
$email = isset($_POST['email'])? $_POST['email']:'';
$dob = isset($_POST['DOB'])? $_POST['DOB']:'';
$pass = isset($_POST['passwrd'])? md5($salt.$_POST['password']):'';
$country = isset($_POST['country'])? $_POST['country']:'';
if(empty($name)){
$error = 'Please enter your name';
}elseif(empty($email)){
$error = 'Please enter your email';
}elseif(empty($dob)){
$error = 'Please enter your date of birth';
}elseif(empty($country)){
$error = 'Please enter your country';
}elseif(empty($pass)){
$error = 'Please enter your password';
}else{
move_uploaded_file($_FILES['file']['tmp_name'],"img/".$_FILES['file']['name']);
$query = mysqli_query("UPDATE users SET image = '".$_FILES['file']['name']."'
WHERE id='$id'");
$query = "UPDATE users SET name = '$name', email = '$email',
DOB = '$DOB', country = '$country', password = '$password'
WHERE id='$id'";
$result = mysqli_query($link,$query);
header('Location: profile.php');
}
}
?>
<?php if(!empty($error)){
echo '<div class="alert alert-success
alert-dismissable">'.addslashes($error).'</div>';
}else{
echo '<div class="alert alert-success">Success</div>';
}
?>
I have added a demo here. At least this will help:

Update statement not working properly and changes the other records

I'm wondering why my update statement changes the venue_type of all the other records whenever I saved the edited record.
Been working on this update statement for quite sometime now and I just can't get it to work. It would be great if you guys could help me on this.
Thanks for the help! :)
<body>
<head>
<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/smoothscroll.js"></script>
<script src="js/resetOnClick.js"></script>
<link href="css/bootstrap.min.css" rel="stylesheet"/>
<link href="css/styles.css" rel="stylesheet"/>
<link href="css/notifbox.css" rel="stylesheet"/>
</head>
<center><label class="control-label" style="font-size:30px">Reservation</label></center>
<center><label class="control-label" style="font-size:15px">Edit Record</label></center>
<br/>
<br/>
<?php
$id = isset($_GET['id'])? $_GET['id'] : "";
include('config/config1.php');
$sel = "SELECT idReservation, venue.venue_type, reservation.reservation_date, reservation.reservation_time
FROM venue
INNER JOIN reservation ON reservation.Venue_idVenue = venue.idVenue where idReservation = '$id';";
$rsvtn = isset($_POST['idReservation']);
$query = mysqli_query($conn,$sel);
while($detail = mysqli_fetch_array($query))
{
$rid = $detail['idReservation'];
$ven = $detail['venue_type'];
$res_d = $detail['reservation_date'];
$res_t = $detail['reservation_time'];
?>
<form class="form-horizontal form-label-left" method="post">
<div class="form-group">
<!--BACK Button-->
Back
<!--BACK Button-->
</div>
<!--<input type="hidden" name="submitted" value="true"/>-->
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Venue<span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="venue_type" data-content="<?php echo $ven;?>" class="form-control col-md-7 col-xs-12" name="venue_type" placeholder="Venue" required="required" type="text" value="<?php echo $ven;?>">
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Reservation Date<span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="reservation_date" data-content="<?php echo $res_d;?>" class="form-control col-md-7 col-xs-12" name="reservation_date" placeholder="Reservation Date" required="required" type="date" value="<?php echo $res_d;?>">
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Reservation Time<span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="reservation_time" data-content="<?php echo $res_t;?>" class="form-control col-md-7 col-xs-12" name="reservation_time" placeholder="Reservation Time" required="required" type="time" value="<?php echo $res_t;?>">
</div>
</div>
<div class="ln_solid"></div>
<div class="form-group">
<div class="col-md-6 col-md-offset-3">
<input type="hidden" name="updRsvtn" value="<?php echo $detail['idReservation'];?>">
<input type="submit" class="btn btn-primary submits" value="Save Edited" onClick="Reservation Records.php" >
</div>
</div>
<?php
}
?>
<?php
if(isset($_POST['updRsvtn']))
{
include('config/config1.php');
$ven = $_POST['venue_type'];
$res_d = $_POST['reservation_date'];
$res_t = $_POST['reservation_time'];
if(!mysqli_query($conn,"UPDATE reservation, venue SET venue_type = '$ven', reservation_date = '$res_d', reservation_time = '$res_t' WHERE idReservation = '$id'"))
{
echo "Not Queried: Wrong variables !";
}
else
{
header("Location: Reservation Records.php");
}
mysqli_close($conn);
}
?>
</form>
<table class = "notif pos">
<thead>
<tr>
<th><center/><b>*Old Record</b></th>
</tr>
</thead>
<tbody>
<tr>
<td><center/><b>Venue: <?php echo $ven;?></td>
</tr>
<tr>
<td><center/><b>Reservation Date: <?php echo $res_d;?></td>
</tr>
<tr>
<td><center/><b>Reservation Time: <?php echo $res_t;?></td>
</tr>
</tbody>
</table>
</body>
if its not working that means your their is not value in
$_GET['id']. then $id is having blank value that's why your query
update for all rows.
if(isset($_POST['updRsvtn']))
{
$id = isset($_GET['id'])? $_GET['id'] : "";
$ven = $_POST['venue_type'];
$res_d = $_POST['reservation_date'];
$res_t = $_POST['reservation_time'];
if(!mysqli_query($conn,"UPDATE reservation, venue SET
venue_type = '$ven', reservation_date = '$res_d',
reservation_time = '$res_t' WHERE idReservation = '$id'"))
{
echo "Not Queried: Wrong variables !";
}
else
{
header("Location: Reservation Records.php");
}
mysqli_close($conn);
}
You could try JOIN in your query
UPDATE reservation t1
JOIN venue t2 ON (t1.Venue_idVenue = t2.idVenue)
SET t1.reservation_date = '$res_d',
t1.reservation_time = '$rest_t',
t2.venue_type = '$ven'
WHERE t1.idReservation = '$id'"

editing a record in a database with a form

first of all i would like to appolagise on the amount of code i am about to paste, i didn't want to snippet any more incase its a bit that's giving me the errors
i have a table named contacts and want to update the table by a form.
i am not sure if its the form or if its the code as the delete user isn't working
i have just started to learn this (a few days ago)so the code might be messy or not 100% secure as it should this is for a offline database so i would improve it as i learn.
<?php include("header.php");
//include database connection
include 'db_connect.php';
$action = isset( $_POST['action'] ) ? $_POST['action'] : "";
if($action == "update"){
//write query
$query = "update contacts
set
name = '".$mysqli->real_escape_string($_POST['name'])."',
surname = '".$mysqli->real_escape_string($_POST['surname'])."',
email = '".$mysqli->real_escape_string($_POST['email'])."',
pcode = '".$mysqli->real_escape_string($_POST['pcode'])."',
website = '".$mysqli->real_escape_string($_POST['website'])."',
gender = '".$mysqli->real_escape_string($_POST['gender'])."'
mobile = '".$mysqli->real_escape_string($_POST['mobile'])."'
phone = '".$mysqli->real_escape_string($_POST['phone'])."'
county = '".$mysqli->real_escape_string($_POST['county'])."'
town = '".$mysqli->real_escape_string($_POST['town'])."'
address = '".$mysqli->real_escape_string($_POST['address'])."'
notes = '".$mysqli->real_escape_string($_POST['notes'])."'
business = '".$mysqli->real_escape_string($_POST['business'])."'
where id='".$mysqli->real_escape_string($_REQUEST['id'])."'";
if( $mysqli->query($query) ) {
echo "User was updated.";
}else{
echo "Database Error: Unable to update record.";
}
}
if($action=='delete'){ //if the user clicked ok, run our delete query
$query = "DELETE FROM users WHERE id = ".$mysqli->real_escape_string($_GET['id'])."";
if( $mysqli->query($query) ){
echo "User was deleted.";
}else{
echo "Database Error: Unable to delete record.";
}}
$query = "select id, name, pcode, website, email, surname, mobile, phone, business, gender, address, town, county, notes
from contacts
where id='".$mysqli->real_escape_string($_REQUEST['id'])."'
limit 0,1";
$result = $mysqli->query( $query );
$row = $result->fetch_assoc();
$id = $row['id'];
$name = $row['name'];
$surname = $row['surname'];
$pcode = $row['pcode'];
$email = $row['email'];
$business = $row['business'];
$phone = $row['phone'];
$mobile = $row['mobile'];
$gender = $row['gender'];
$address = $row['address'];
$county = $row['county'];
$notes = $row['notes'];
$town = $row['town'];
$website = $row['website']; ?>
<?php echo "<a href='#' onclick='delete_user( {$id} );'>Delete</a>";
?>
<body>
<div class="div-middle-big">
<!--we have our html form here where new user information will be entered-->
<a href='index.php'>Back to index</a>
</td>
</tr>
</table>
</form>
<div id="loader_cont"><img src="img/loaders/page_loader.gif"></div>
<?php include'topnav.php' ?>
<div class="container">
<div class="main_content row-fluid">
<div class="span3">
<?php include'menu.php' ?>
<!--/.well -->
</div>
<!--/span-->
<div class="span9">
<div class="row-fluid">
<div class="span12">
<ul class="breadcrumb br_styled no_space">
<li> Dashboard <span class="divider">/</span> </li>
<li class="active">Profile</li>
</ul>
<div class="widget profile_cont">
<header>
<h3>Profile: <span class="profile_title"><?php echo$name; ?> <?php echo$surname; ?></span></h3>
<ul class="toggle_content">
<li class="arrow">Toggle Content</li>
</ul>
</header>
<section class="group">
<div class="info"> <img src="http://api.thumbalizr.com/?url=http://<?php echo$website; ?>&width=250" alt="Profile picture">
<h4>Profile Picture</h4>
<div class="profile_picture">
<input type="file" />
<!-- <input type="submit" /> -->
visit website
<!-- UPLOAD -->
</div>
<ul>
<li><i class="sweet-user"></i> Profile</li>
<li><i class="sweet-settings"></i> Settings</li>
<li><i class="sweet-mail"></i> Email <?php echo$name; ?></li>
<li><i class="sweet-cog-4"></i> Widgets</li>
<li><i class="sweet-exit"></i> Logout</li>
</ul>
<div class="span3">
<div class="widget">
<header>
<h3>Grid 3</h3>
<ul class="toggle_content" style="display: none;">
<li class="arrow">Toggle Content</li>
</ul>
</header>
<section class="code_align"> <code>class="span3"</code> </section>
</div>
</div>
</div>
<div class="details">
<form action='#' method='post' border='0' class="well form-horizontal">
<fieldset>
<h4 class="group"> <span>Personal details</span> </h4>
<div class="control-group">
<div class="controls"> </div>
</div>
<div class="control-group">
<label class="control-label" for="name">First name</label>
<div class="controls">
<input id="name" type="text" name="name" value="<?php echo$name; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="surname">Last name</label>
<div class="controls">
<input id="surname" type="text" name="surname" value="<?php echo$surname; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="business">Company Name</label>
<div class="controls">
<input id="business" type="text" name="business" value="<?php echo$business; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="phone">Phone number</label>
<div class="controls">
<input id="phone" type="text" name="phone" value="<?php echo$phone; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="mobile">Mobile number</label>
<div class="controls">
<input id="mobile" type="text" name="mobile" value="<?php echo$mobile; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="gender">Sex</label>
<div class="controls">
<select class="gender" style="width:210px;" tabindex="2">
<option value="<?php echo$gender; ?>"><?php echo$gender; ?></option>
<option value="female">Female</option>
<option value="male">Male</option>
</select>
</div>
</div>
<h4>Contact details</h4>
<div class="control-group">
<label class="control-label" for="email">E-mail</label>
<div class="controls">
<input id="email" type="text" name="email" value="<?php echo$email; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="website">Website</label>
<div class="controls">
<input id="website" type="text" name="website" value="<?php echo$website; ?>" data-original-title="Without the http://">
</div>
</div>
<div class="control-group">
<label class="control-label" for="address">Address</label>
<div class="controls">
<textarea id="address" rows="3" name="address" ><?php echo$address; ?></textarea>
</div>
</div>
<div class="control-group">
<label class="control-label" for="skypeid">Town</label>
<div class="controls">
<input id="town" type="text" name="town" value="<?php echo$town; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="county">County</label>
<div class="controls">
<input id="county" type="text" name="county" value="<?php echo$county; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="pcode">Post code</label>
<div class="controls">
<input id="pcode" type="text" name="pcode" value="<?php echo$pcode; ?>">
</div>
</div>
<h4>Notes about <?php echo$name; ?> <?php echo$surname; ?></h4>
<p>
<textarea id="notes" rows="5" name="notes" ><?php echo$notes; ?></textarea>
</p>
<div class="form-actions">
<!-- so that we could identify what record is to be updated -->
<input type='hidden' name='id' value='<?php echo $id ?>' />
<!-- we will set the action to edit -->
<input type='hidden' name='action' value='update' />
<input type='submit' value='Edit' />
</div>
</fieldset>
</form>
The problem with the above code is thats its not updating my database and i am getting
Database Error: Unable to update record
UPDATE
i have gone back to my old files and now this dosent work
ok i gone right back to the basic files i had....
<meta http-equiv="refresh" content="0; url=../contacts.php"> <?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = "DELETE FROM contacts
WHERE created='$_GET[id]'";
mysql_select_db('pcrepairs');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not delete data: ' . mysql_error());
}
echo "Deleted data successfully\n";
mysql_close($conn);
?>
i am now getting this error
Could not delete data: Unknown column 'created' in 'where clause'
You seems to be using users table in your delete query.Does the users table exist?,if not please change it to contacts.Please let me know
Thanks
Forgetting PHP for a moment if you were to issue a SQL query, say in the command-line, you would need to use single quotes to signify the search string.
So it would like this:
DELETE FROM users WHERE id = '100';
The above has to remain true when you construct the query via PHP:
$query = "DELETE FROM users WHERE id='".$mysqli->real_escape_string($_GET['id'])."'";
If your code's failing, you really need to get into the mindset of debugging your code. Approach it in smaller chunks and work your way back up. So for instance, you can try executing the above query with a hard-coded id value in the console and confirm it works.
Can you try echo'ing the $query value before running it through mysqli? Get that sql statement and try manually running it through the database. You may also want to double check your data types. You can get an error if you try, for example, setting an NUMBER/INT field with a string value.
You forgot the commas in your SQL UPDATE statement:
$query = "update contacts
set
name = '".$mysqli->real_escape_string($_POST['name'])."',
surname = '".$mysqli->real_escape_string($_POST['surname'])."',
email = '".$mysqli->real_escape_string($_POST['email'])."',
pcode = '".$mysqli->real_escape_string($_POST['pcode'])."',
website = '".$mysqli->real_escape_string($_POST['website'])."',
gender = '".$mysqli->real_escape_string($_POST['gender'])."',
mobile = '".$mysqli->real_escape_string($_POST['mobile'])."',
phone = '".$mysqli->real_escape_string($_POST['phone'])."',
county = '".$mysqli->real_escape_string($_POST['county'])."',
town = '".$mysqli->real_escape_string($_POST['town'])."',
address = '".$mysqli->real_escape_string($_POST['address'])."',
notes = '".$mysqli->real_escape_string($_POST['notes'])."',
business = '".$mysqli->real_escape_string($_POST['business'])."'
where id='".$mysqli->real_escape_string($_REQUEST['id'])."'";
You also need to review your HTML code.
EDIT
The SQL syntax for an update statement is:
UPDATE my_table_name SET col1='value1', col2='value2', ... WHERE conditions
And this should work for the delete query:
$query = "DELETE FROM users WHERE id='".$mysqli->real_escape_string($_GET['id'])."'";
If you are using PHP5+ I recommend you to use PDO instead of the old sqlite functions.
You also need to verify your data before saving into the DB.

Categories