I have an update form that has three inputs. Now, I want to retrieve values from database in the inputs and update them after I click update and it works fine. But, the problem is that I must refresh after pressing the update button to see the changes (the automatic refresh and manual refresh). I need the value to change instantly after pressing the update button. How to do this? This is my code:
<?php
require_once 'core/init.php';
$result = $db->query("SELECT * FROM customers WHERE id = '$customer_id'");
while($res = mysqli_fetch_assoc($result)) {
$first_name = $res['first_name'];
$last_name = $res['last_name'];
$mobile_number = $res['mobile_number'];
}
if(isset($_POST['submit_0'])) {
$first_Name = ((isset($_POST['First_Name']))?sanitize($_POST['First_Name']):'');
$last_Name = ((isset($_POST['Last_Name']))?sanitize($_POST['Last_Name']):'');
$db->query("UPDATE customers SET first_name = '{$first_Name}', last_Name = '{$last_Name}' WHERE id = '{$customer_id}'");
}
?>
<form action="personal_details.php" method="post" id="personal_details">
<div class="row">
<div class="form-group col-lg-12">
<label for="First_Name" style="font-weight: bold; cursor:text;"><span style="color:red;">* </span>First Name</label>
<input type="text" name="First_Name" id="First_Name" class="form-control form-control-lg"
value="<?=$first_name;?>" style="width:770px; background-color:#EEEEEE;">
</div>
</div>
<div class="row">
<div class="form-group col-lg-12">
<label for="Last_Name" style="font-weight: bold; cursor:text;"><span style="color:red;">* </span>Last Name</label>
<input type="text" name="Last_Name" id="Last_Name" class="form-control form-control-lg"
value="<?=$last_name;?>" style="width:770px; background-color:#EEEEEE;">
</div>
</div>
<div class="row">
<div class="form-group col-lg-7">
<label for="mobile_number" style="font-weight: bold;"><span style="color:red;">* </span>Mobile Phone Number</label>
<input type="tel" name="mobile_number" id="monu" class="form-control form-control-lg"
value="<?=$mobile_number;?>" style="background-color:#EEEEEE; cursor:default;" readonly>
</div>
</div>
Related
This question already has answers here:
500 internal server error, how to debug [duplicate]
(2 answers)
Why does this PDO statement silently fail?
(2 answers)
Closed 2 years ago.
I have a form that submits business information. We dont always have all the info so I want to have it so that only the Business Name and Contact are required. The issue I have now is if the email is empty the page errors out. Give me a 500 error. I am using the !emtpy and trim in the script.
add/index.php
session_start();
if (!isset($_SESSION['logged_in'])) {
header('Location:https://portal.site.net/');
}
require 'dbconnect.php';
if(isset($_POST['addsmb'])){
$businessName = !empty($_POST['businessName']) ? trim($_POST['businessName']) : null;
$contactName = !empty($_POST['contactName']) ? trim($_POST['contactName']) : null;
$title = !empty($_POST['title']) ? trim($_POST['title']) : null;
$phone = !empty($_POST['phone']) ? trim($_POST['phone']) : null;
$email = !empty($_POST['email']) ? trim($_POST['email']) : null;
$url = !empty($_POST['url']) ? trim($_POST['url']) : null;
$city = !empty($_POST['city']) ? trim($_POST['city']) : null;
$state = !empty($_POST['state']) ? trim($_POST['state']) : null;
$outletid = !empty($_POST['outletid']) ? trim($_POST['outletid']) : null;
$user = !empty($_POST['user']) ? trim($_POST['user']) : null;
$sql = "SELECT COUNT(Phone) AS num FROM smb_leads WHERE Phone = :phone";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(':phone', $phone);
//.
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if($row['num'] > 0){
$_SESSION['error'] = "1";
header("Location: ../error.php".$qstring);
exit;
}
if(empty($_POST['email'])){
$email = "No Email";
}
$sql = "INSERT INTO smb_leads (Outlet_ID, URL, State, City, DUNS_Name, Contact_Name, Title, Phone, created_by, email) VALUES (:outletid, :url, :state, :city, :businessName, :contactName, :title, :phone, :user, :email)";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(':businessName', $businessName);
$stmt->bindValue(':contactName', $contactName);
$stmt->bindValue(':title', $title);
$stmt->bindValue(':phone', $phone);
$stmt->bindValue(':url', $url);
$stmt->bindValue(':city', $city);
$stmt->bindValue(':state', $state);
$stmt->bindValue(':outletid', $outletid);
$stmt->bindValue(':user', $user);
$stmt->bindValue(':email', $email);
$result = $stmt->execute();
}
// Redirect to the listing page
header("Location: ../index.php".$qstring);
form
<form action="add/index.php" method="post">
<span style="color: gray; font-size: 12px;" >Fields marked with a * are required </span>
<div class="form-row">
<div class=".form-group.required col-md-4">
<input type="text" class="form-control" id="businessName" name="businessName" placeholder="* Business Name">
</div>
<div class="form-group col-md-4">
<input type="text" class="form-control" id="contactName" name="contactName" placeholder="* Contact Name">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<input type="text" class="form-control" id="title" name="title" placeholder="Contact Title">
</div>
<div class="form-group col-md-4">
<input type="text" class="form-control" id="phone" name="phone" placeholder="* Contact Phone #">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<input type="text" class="form-control" id="email" name="email" placeholder="Email Address">
</div>
<div class="form-group col-md-4">
<input type="text" class="form-control" id="url" name="url" placeholder="Company Website">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<input type="text" class="form-control" id="city" name="city" placeholder="City">
</div>
<div class="form-group col-md-2">
<input type="text" class="form-control" id="state" name="state" placeholder="State">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<input type="text" class="form-control" id="outletid" name="outletid" placeholder="Outlet ID" value="<?php echo $_SESSION['outlet_id']; ?>">
</div>
<div class="form-group col-md-4">
<input type="text" class="form-control" id="user" name="user" placeholder="* Your Name">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-1">
</div>
<button type="submit" name="addsmb" value="Register" class="btn btn-primary">Submit</button>
</form>
Im guessing my error is somewhere in here.
$phone = !empty($_POST['phone']) ? trim($_POST['phone']) : null;
$email = !empty($_POST['email']) ? trim($_POST['email']) : null;
The script works just fine if i leave everything else blank but the name, business and email.
My Error
This page isn’t working portal.site.net is currently unable to handle this request.
HTTP ERROR 500
how do I foreach through html input form and insert multiple rows or one based on a selected date field? in other words when a user enters "name" "description" and "shift" and then selects either one date or more then one. PHP will then enter the same information for either one new row or multiples based on how many dates were selected.
<?php
if(isset($_REQUEST['submit']))
{
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "xxxx", "xxxx", "xxxx");
// Check connection
if($link === false){
die("| ERROR: Could not connect. " . mysqli_connect_error());
}
$name = mysqli_real_escape_string($link, $_REQUEST['name']);
$desc = mysqli_real_escape_string($link, $_REQUEST['description']);
$shift = mysqli_real_escape_string($link, $_REQUEST['shift']);
$date = mysqli_real_escape_string($link, $_REQUEST['daterange']);
$sql = "insert into db (name,description,shift,evdate) values ('$name', ' $desc','$shift','$date')";
$sql2 = "insert into db (name,description,shift,evdate) values ('$name', ' '$desc','$shift','$insert')";
if ($date=0) {
$result = mysqli_query($link, $sql);
}else{
$daterange = explode(',',$date);
foreach($daterange as $insert) {
$result = mysqli_query($link, $sql2);
}
}
if(mysqli_query($link, $sql)){
echo "";
} else{
echo "| ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
if ($link->multi_query($sql) === TRUE) {
echo "It Worked..... Maybe!!!!!!";
} else {
echo "Error: " . $sql . "<br>" . $link->error;
}
}
$link->close();
?>
<form action="test_insert.php" method="post">
<div class="col col-lg-2 col-lg-offset-0">
<div class="form-group col-lg-offset-0 col-lg-12">
<label for="Name">Employee Name:</label>
<input type="text" name="name" placeholder="First & Last Name" id="name" required>
<p class="help-block col-lg-12">First and Last Name Please.</p>
</div>
</div>
<div class="col col-lg-offset-0 col-lg-2">
<div class="form-group col-lg-12">
<label for="description">Description:</label>
<input type="text" name="description" id="description" placeholder="description..." required>
<p class="help-block">For Example: "Vacation Full Day" or "PTO 2 Hours." </p>
</div>
</div>
<div class="col col-lg-offset-0 col-lg-3">
<label for="shift">Shift:</label><br>
<input type="radio" name="shift" value="First Shift" id="shift" checked> First Shift |
<input type="radio" name="shift" value="Second Shift" id="shift"> Second Shift |
<input type="radio" name="shift" value="Third Shift" id="shift"> Third Shift
<p class="help-block">Select Correct Shift Worked.</p>
</div>
<div class="col col-lg-offset-0 col-lg-3">
<div class="form-group col-lg-10">
<label for="date2">Date/Dates:</label>
<input type="text" id="datepicker1" name="daterange" placeholder="Select Your Date" />
</div>
<div class="form-group col-lg-10">
<label for="date2">Date/Dates:</label>
<input type="text" id="datepicker2" name="daterange" placeholder="Select Your Date" />
</div>
<div class="form-group col-lg-10">
<label for="date2">Date/Dates:</label>
<input type="text" id="datepicker3" name="daterange" placeholder="Select Your Date" />
</div>
<div class="form-group col-lg-10">
<label for="date2">Date/Dates:</label>
<input type="text" id="datepicker4" name="daterange" placeholder="Select Your Date" />
</div>
<div class="form-group col-lg-10">
<label for="date2">Date/Dates:</label>
<input type="text" id="datepicker5" name="daterange" placeholder="Select Your Date" />
</div>
<div class="form-group col-lg-6">
<input type="submit" name="submit" class= "btn btn-primary">
</div>
</div>
</div>
</form>
Think the best way is to use AJAX,
Then with the response as a string you make a table or foreach in php as a string and then use the .html adapter to output the newly made data.
function submitForm(form){
var url = form.attr("action");
var formData = {};
$(form).find("input[name]").each(function (index, node) {
formData[node.name] = node.value;
});
$.post(url, formData).done(function (data) {
$('#showresults').html(result);
});
}
After submit in signup page my signUp page redirects to info.php where I want to collect additional info of user using email id he gives on signup page but when I tried to get the email id of user through sessions, session return empty value.
THIS IS MY SIGNUP CODE
<?php
session_start();
if(isset($_POST['submit'])){
$name= $_POST['_user'];
$email = $_POST['_email'];
$pass = $_POST['_password'];
//Insert Data
$sql = "INSERT INTO signup(name,email,password)
VALUES('$name','$email','$pass')";
//Data Validation
if(mysqli_query($conn,$sql)){
echo "<script>alert('SignUp Successfull')</script>";
$_SESSION['user_email'] = $email;
header('Location: info.php');
}
else{
echo "<script>window.alert('You are already a user.')</script>";
}
}
mysqli_close($conn);
?>
AND THIS MY INFO.PHP CODE
<?php
session_start();
if(isset($_POST['_submit'])){
if(empty($_POST['_address']) || empty($_POST['_country']) || empty($_POST['_number']) || empty($_POST['_cnic']) || empty($_POST['_passport'])){
echo "<script>window.alert('All fields are required')</script>";
}
else{
$address = $_POST['_address'];
$country = $_POST['_country'];
$number = $_POST['_number'];
$cnic = $_POST['_cnic'];
$passport = $_POST['_passport'];
$email=$_SESSION['user_email'];
$query = "INSERT INTO info(email,address,country,mobile,cnic,passport)
VALUES('$email','$address','$country','$number','$cnic','$passport')";
if(mysqli_query($conn,$query)){
header('Location: ../index.php');
}
else{
echo "<script>window.alert('Error While Entering the data!.')</script>";
}
}
}
mysqli_close($conn);
?>
In addition I use this global session variable for login page and it works fine.
UPDATE
SIGNUP HTML CODE
<div class="outside">
<form class="form-horizontal" role="form" method="post">
<div class="form-group">
<label class="control-label col-sm-3 glyphicon glyphicon-user" for="name"></label>
<div class="control-label col-sm-8">
<input type="text" name="_user" class="form-control" id="name" placeholder="Full Name">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="email">
<img class="glyphicon1" src="../assests/at-sign.png">
</label>
<div class="control-label col-sm-8">
<input type="email" name="_email" class="form-control" id="email" placeholder="Enter Email">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3 glyphicon glyphicon-lock" for="password"></label>
<div class="control-label col-sm-8">
<input type="password" name="_password" class="form-control" id="password" placeholder="Enter Password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-3">
<button name="submit" id="submit" value="Upload" type="submit" class="btn btn-default">Confirm SignUp</button>
</div>
</div>
<p>Already a User? LogIn</p>
</form>
</div>
Use this for Returns the auto generated id used in the last query
mysqli_insert_id($link)
Hey I am trying to get this code running for the past few days now. I do not know what is the problem. Whenever I run the code I can see it running but an empty row gets inserted. Basically I ave tried to hard code the data and the data gets inserted. Here is the HTML form:
<form action="register.php" id="contactForm" type="post">
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>First name *</label>
<input type="text" class="form-control" name="fname" >
</div>
<div class="col-md-6">
<label>Last name *</label>
<input type="text" class="form-control" name="lname" >
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>Gender *</label><br>
<select name="gender">
<option> Male </option>
<option> Female </option>
</select>
</div>
<div class="col-md-6">
<label>Stream *</label><br>
<select name="stream">
<option> B-Tech </option>
<option> M-Tech </option>
</select>
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>Email *</label>
<input type="text" class="form-control" name="email" >
</div>
<div class="col-md-6">
<label>Mobile *</label>
<input type="text" class="form-control" name="mobile">
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>College *</label>
<input type="text" class="form-control" name="college" >
</div>
<div class="col-md-6">
<label>Job Kind *</label>
<input type="text" class="form-control" name="job" >
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
    
<input type="submit" value="Register" class="btn btn-primary btn-lg"
data-loading-text="Loading..." name="submit">
</div>
</div>
</form>
Here is the registration.php
<?php
$connection = mysql_connect("EDITED by billy, was an I.P and port number", "user", "password"); // Establishing Connection with Server
$db = mysql_select_db("Registrations_connect", $connection); // Selecting Database from Server
$first_name = $_POST["fname"];
$last_name = $_POST["lname"];
$sex = $_POST["gender"];
$field = $_POST["stream"];
$contact = $_POST["mobile"];
$eaddress = $_POST["email"];
$institute = $_POST["college"];
$naukri = $_POST["job"];
$query = mysql_query("insert into students(fname, lname, gender, stream, mobile, email, college, job)
values ('$name', '$last_name', '$sex', '$field','$contact', '$eaddress', '$intitute', '$naukri')");
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
mysql_close($connection); // Closing Connection with Server
?>
After running; In the inspect element I checked the response:- It shows Data Inserted successfully but actually an empty row is getting inserted. Basically what i think I am not able to correctly grab the data properly from form. Can somebody please check what is the problem. It will be a great help.
The attribute is method, not type. This typo is causing your form to process a GET rather than a POST. So all your variable assignments are wrong.
$first_name = $_POST["fname"];
would be
$first_name = $_GET["fname"];
or you could use the $_REQUEST; or you can just correct the attribute,
<form action="register.php" id="contactForm" method="post">
Your code also is wide open to SQL injections and is using the deprecated mysql_ functions. You should update to mysqli or pdo and be using prepared statements with parameterized queries.
More on SQL injections:
http://php.net/manual/en/security.database.sql-injection.phpHow can I prevent SQL injection in PHP?https://www.owasp.org/index.php/SQL_Injection_Prevention_Cheat_Sheet#Defense_Option_1:_Prepared_Statements_.28Parameterized_Queries.29
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.