Update query for database in PHP not working - php

I am having trouble updating my database data. I put the data that I want to update but when I click on the "Update" button it does
nothing. I have called the file on another php file by using Update
It also shows this error
Notice: Undefined variable: fname in
C:\xampp\htdocs\project\change1.php on line 71
Can someone help me figure this issue, please?
<?php include("config.php"); ?>
<?php
if (isset($_GET['edit'])) {
$update = true;
$record = mysqli_query($con, "SELECT * FROM employee WHERE id='".$_GET['edit']."'");
$row = mysqli_fetch_array($record,MYSQLI_BOTH);
}
if (isset($_POST['update'])) {
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$password = $_POST['password'];
$addr = $_POST['addr'];
$phone = $_POST['phone'];
$id=$_GET['edit'];
$query = "UPDATE employee SET fname='".$fname."',lname='".$lname."',password='".$password."',addr='".$addr."',phone='".$phone."' WHERE id='".$id."'";
$result = mysqli_query($con,$query) or die ("problem inserting new record into database");
if($result){
header('location: show_db.php');
}
else {echo "Update not successful"; }
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Update Data</title>
</head>
<body>
Home
<br/><br/>
<input type="hidden" name="id" value="<?php echo $id; ?>">
Name:<input type="text" name="fname" value="<?php echo $fname ; ?>">
Surname:<input type="text" name="lname" value="<?php echo $lname; ?>">
Password:<input type="text" name="password" value="<?php echo $password; ?>">
Address:<input type="text" name="addr" value="<?php echo $addr; ?>">
Contact:<input type="text" name="phone" value="<?php echo $phone; ?>">
<input type="submit" name="update" value="Update">
</body>
</html>

Put the html inputs inside a form
<form name ="form1" method ="get" action="">
<input type="hidden" name="id" value="<?php echo $id; ?>">
Name:<input type="text" name="fname" value="<?php echo $fname ; ?>">
Surname:<input type="text" name="lname" value="<?php echo $lname; ?>">
Password:<input type="text" name="password" value="<?php echo $password; ?>">
Address:<input type="text" name="addr" value="<?php echo $addr; ?>">
Contact:<input type="text" name="phone" value="<?php echo $phone; ?>">
<input type="submit" name="update" value="Update">
</form>

Related

PHP form completely blank when trying to populate using MySQL

I have a MySQL database that holds customer orders. I am trying to create a form that a user would enter after selecting the ID of a row they would like to modify.
What the code below is suppose to do is auto-populate a html form with all of the data from the MySQL row. It would take the ID given to it by a previous php file and then use that to grab the row it needs from the database. Unfortunately, no matter what I change or do, the webpage only produces blank boxes where the data is suppose to be.
Here's the code for edit.php (the form that is suppose to auto-populate.)
<!DOCTYPE html>
<html>
<body>
<form action="update_submit.php" method="post">
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$servername = "server";
$username = "book";
$password = "pass";
$dbname = "book";
$db_selected = new mysqli($servername, $username, $password, $dbname);
if ($db_selected->connect_error) {
die("Connection failed: " . $dbc->connect_error);
}
if(!empty($_GET['ID'])){
$id = intval($_GET['ID']);
}
try {
$results = $db_selected -> prepare("SELECT * FROM customerorders WHERE ID = ?");
$results -> bind_param("i", $id);
$results -> execute();
} catch(Exception $e) {
echo $e -> getMessage();
die();
}
$row = $results->get_result()->fetch_assoc();
?>
<input type="hidden" name="ID" value="<?php echo $row['ID']; ?>">
<input type="text" name="lastName" value="<?php echo $row['lastName']; ?>" required><br />
<input type="text" name="firstName" value="<?php echo $row['firstName']; ?>" required><br />
<input type="text" name="email" value="<?php echo $row['email']; ?>" required><br />
<input type="text" name="phoneNumber" value="<?php echo $row['phoneNumber']; ?>" required><br />
<input type="text" name="department" value="<?php echo $row['department']; ?>" ><br />
<input type="text" name="typeOfOrder" value="<?php echo $row['typeOfOrder']; ?>" required><br />
<input type="text" name="vendor" value="<?php echo $row['vendor']; ?>" required><br />
<input type="text" name="product" value="<?php echo $row['product']; ?>" required><br />
<input type="text" name="PONumber" value="<?php echo $row['PONumber']; ?>" required><br />
<input type="text" name="accountNumber" value="<?php echo $row['accountNumber']; ?>" required><br />
<input type="text" name="dateOrdered" value="<?php echo $row['dateOrdered']; ?>" required><br />
<input type="text" name="dateExpected" value="<?php echo $row['dateExpected']; ?>" required><br />
<input type="text" name="comments" value="<?php echo $row['comments']; ?>" required><br />
<input type="submit" name="sumbit">
</form>
</body>
</html>
I have error reporting on and nothing ever comes up when I try to find problems. I've also switched around my ' and " multiple times just to see if it ever makes a difference. Nothing I have tried seems to work. I've also looked through every stack overflow question I could possibly find relating to my issue and I still can't find anything that would fix my problem.
Any help is appreciated. I am a novice to php and MySQL so I apologize in advance if the code seems nonsensical or if there is an already obvious solution.

Can't update fields from db

I'm trying to update some fileds in the database using a form and for some reason it just refreshes the page without doing anything. Here's the code I used:
<?php
$id = $_GET["id"];
$select = "SELECT * FROM blog_members WHERE memberID = '$id'";
$result = mysqli_query($link, $select);
$row=mysqli_fetch_assoc($result);
$id2=$row['memberID'];
$username=$row['username'];
$password = $row['password'];
$email = $row['email'];
?>
<form id="form" action="#" method="post">
<br><table cellspacing='0' cellpadding='0'>
<tr><td>Username:</td>
<td><input name="username" type="text" id="username" placeholder="<?php echo $username; ?>" size="25" /></td></tr>
<tr><td>Password:</td>
<td><input name="password" type="text" id="password" placeholder="<?php echo $password; ?>" size="25" /></td></tr>
<tr><td>Email:</td>
<td><input name="email" type="text" id="email" placeholder="<?php echo $email; ?>" size="25" /></td></tr>
</table>
<br>
<input type="submit" name="Edit" value="Save" />
</center></form>
<?php
if(isset($POST['Edit'])){
$username2 = $_POST['username'];
$password2 = $_POST['password'];
$email2 = $_POST['email'];
$edit = mysqli_query($link, "UPDATE blog_members SET username='$username2',password='$password2',email='$email2' WHERE memberID='$id2'");
$result1 = mysqli_query($link,$edit);
if(!$result){
echo mysqli_error($link);
}else{
echo "Changes have been saved successfully!";
echo "<meta http-equiv=\"refresh\" content=\"2;URL=view-users.php\">";
}
}
?>
I think the query $edit it's not working properly since the page just gets refreshed and the fields from the databse are not updated. I feel like I'm missing something. Any suggestions?
Also change below code
if(isset($POST['Edit'])){
with this, $_POST is right syntax not $POST
if(isset($_POST['Edit'])){
Try This..
<input name="username" type="text" id="username" value="<?php echo $username; ?>" size="25" />
<input name="password" type="text" id="username" value="<?php echo $password; ?>" size="25" />
<input name="email" type="text" id="email" value="<?php echo $email; ?> " size="25" />
I got two things..
You lost the ID that is gets $id = $_GET["id"];.
Mysql Update Query two times.
Solution:
Post your id in the form and get the posted id and use it to update and use a single query to update.
Updates: I miss the main point and that is $POST, you need to change the if(isset($POST['Edit'])) to if(isset($_POST['Edit'])).
Let me know, if you need more support from me.

Filling html forms with mysql data using php coming up null

I am trying to fill a html form with data being received out of my mysql database. However I cannot set the forms to display on-load the variables being extracted from the database. I would like the form on-load to hold the data last entered into the forms which have been added to the database previously.
$query = "SELECT FROM character_tbl WHERE character_player
='".$_SESSION["user"]."' character_tbl";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$name = $row['character_name'];
$race = $row['character_race'];
$class = $row['character_class'];
$alignment = $row['character_alignment'];
$hp = $row['character_hp'];
$str = $row['character_str'];
$dex = $row['character_dex'];
$con = $row['character_con'];
$int = $row['character_int'];
$wis = $row['character_wis'];
$cha = $row['character_cha'];
$ac = $row['character_ac'];
$touch = $row['character_touch'];
$flat = $row['character_flat'];
$fort = $row['character_fort'];
$ref = $row['character_ref'];
$will = $row['character_will'];
}
echo $will;
mysql_close();
?>
<!DOCTYPE html>
<html>
<body>
<div id="nav">
<form action="user.php">
<input type="submit" value="Back">
</form>
</div>
<div id="section">
<form action="update.php" method="POST">
Character Name:<br>
<input type="text" name="name" value="<?php echo $name;?>">
<br>
Race<br>
<input type="text" name="race" value="<?php echo $race;?>">
<br>
Class<br>
<input type="text" name="class" value="<?php echo $class;?>">
<br>
Alignment<br>
<input type="text" name="alignment" value="<?php echo $alignment;?>">
<br>
HP<br>
<input type="text" name="hp" value="<?php echo $hp;?>">
<br>
STR<br>
<input type="number" name="str" value="<?php echo $str;?>">
<br>
DEX<br>
<input type="number" name="dex" value="<?php echo $dex;?>">
<br>
CON<br>
<input type="text" name="con" value="<?php echo $con;?>">
<br>
INT<br>
<input type="text" name="int" value="<?php echo $int;?>">
<br>
WIS<br>
<input type="text" name="wis" value="<?php echo $wis;?>">
<br>
CHA<br>
<input type="text" name="cha" value="<?php echo $cha;?>">
<br>
AC<br>
<input type="text" name="ac" value="<?php echo $ac;?>">
<br>
Touch AC<br>
<input type="text" name="touch" value="<?php echo $touch;?>">
<br>
Flat-Footed AC<br>
<input type="text" name="flat" value="<?php echo $flat;?>">
<br>
Fortitude<br>
<input type="text" name="fort" value="<?php echo $fort;?>">
<br>
Reflex<br>
<input type="text" name="ref" value="<?php echo $ref;?>">
<br>
Will<br>
<input type="text" name="will" value="<?php echo $will;?>">
</br>
<input type="submit" value="Update">
</form>
I think the SQL has error:
SELECT FROM character_tbl WHERE character_player
try:
SELECT * FROM character_tbl WHERE character_player
You have syntax error in your mysql query. You have not place field or columns name or (*) for all columns to extract.
Try like this..
$query = "SELECT * FROM character_tbl WHERE character_player ='".$_SESSION['user']."'";

PHP form MySql EDIT / UPDATE - Can't get form to populate with data

I'm very new to PHP and MySQL. I've created a form which creates rows in my MySQL tables, I created a page that shows all those entries. Now I'm trying to edit those entries. I created another form that should populate with info from the selected row..the only problem is that it's not. Here's all my code including login.php...just in case. Please let me . I've looked everywhere for an answer and couldn't find one. All I found was mysql and not mysqli.
<?php //login.php
$db_hostname = 'localhost';
$db_database = 'mydata';
$db_username = 'user';
$db_password = '123#';
// Connect to server.
$link = mysqli_connect($db_hostname, $db_username, $db_password);
if (!$link) {
die('Not connected : ' . mysqli_error());
}
// Select the database.
$db_selected = mysqli_select_db($link, $db_database);
if (!$db_selected) {
die ('Can\'t use database : ' . mysqli_error());
}
?>
edit_client.php
<?php
$title = "Edit Client Form";
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<div> <!-- form container div -->
<form action="" method="post">
<div> <!-- form description div -->
<h2><?php echo $title ?></h2>
<p>Edit a client here.</p>
</div>
<?php
require_once = 'login.php';
// get value of id that sent from address bar
$id=$_GET['id'];
if(isset($id)) {
$sql="SELECT * FROM `clients` WHERE client_id='$id'";
$result=mysqli_query($sql);
$row=mysqli_fetch_assoc($result);
?>
<input type="hidden" name="id" value="<?php echo $row['client_id']; ?>">
<input type="text" name="first_name" value="<?php echo $row['first_name']; ?>" required><br />
<input type="text"name="last_name" value="<?php echo $row['last_name']; ?>" required><br />
<input type="text"name="company_name" value="<?php echo $row['company_name']; ?>" ><br />
<input type="text" name="address" value="<?php echo $row['address']; ?>"><br />
<input type="text" name="city" value="<?php echo $row['city']; ?>" required><br />
<input type="text" name="state" value="<?php echo $row['state']; ?>" required><br />
<input type="number" name="zip_code" value="<?php echo $row['zip_code']; ?>" required><br />
<input type="tel" name="tel_number" value="<?php echo $row['tel_number']; ?>" required><br />
<input type="email" name="email" value="<?php echo $row['email']; ?>" required><br />
<input value="Submit" type="submit">
</form>
</div>
</body>
</html>
here's the table from "display_client" that sends the ID to the edit_form.php page
<td align="left">' . '<a href="\edit_client.php?id=' . $row['client_id'] . '/">Edit</td>
any pointers in the right direction would be much appreciated. thank you.
As suggested below I have used PDO Prepared statements to achieve my desired result. Thank you thank you! #nomistic !
<?php
$title = "Edit Client Form";
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<div> <!-- form container div -->
<form action="update_client_submit.php" method="post">
<div> <!-- form description div -->
<h2><?php echo $title ?></h2>
<p>Edit a client here.</p>
</div>
<?php
require_once 'login.php';
if(!empty($_GET['id'])){
$client_id = intval($_GET['id']);
}
try {
$results = $db -> prepare('SELECT * FROM clients WHERE client_id = ?');
$results -> bindParam(1, $client_id);
$results -> execute();
} catch(Exception $e) {
echo $e -> getMessage();
die();
}
$row = $results -> fetch(PDO::FETCH_ASSOC);
?>
<input type="hidden" name="client_id" value="<?php echo $row['client_id']; ?>">
<input type="text" name="first_name" value="<?php echo $row['first_name']; ?>" required><br />
<input type="text" name="last_name" value="<?php echo $row['last_name']; ?>" required><br />
<input type="text" name="company_name" value="<?php echo $row['company_name']; ?>" required><br />
<input type="text" name="address" value="<?php echo $row['address']; ?>" required><br />
<input type="text" name="city" value="<?php echo $row['city'] ?>" ><br />
<input type="text" name="state" value="<?php echo $row['state'] ?>" required><br />
<input type="text" name="zip_code" value="<?php echo $row['zip_code']; ?>" required><br />
<input type="text" name="tel_number" value="<?php echo $row['tel_number']; ?>" required><br />
<input type="text" name="email" value="<?php echo $row['email']; ?>" required><br />
<input type="submit" name="sumbit">
</form>
</body>
</html>
Edit, reducing this and correcting a potential problem in you query.
you need a loop to get the data. It should be have something like this (using your data)
$id=$_GET['id'];
if(isset($id)) {
$sql="SELECT * FROM clients WHERE client_id=$id";
$result=mysqli_query($sql);
$row=mysqli_fetch_assoc($result);
while($row=mysqli_fetch_assoc($result)) {
echo '<input type="hidden" name="id" value="'.$row['client_id']. '">';
}
Note: You may want to look into using prepared statements; they are pretty easy to learn, especially at this stage, and you will have a lot of advantages, such as not having to worry about whether you are quoting int and varchar variables in your query; you'd declare them separately. They are also MUCH more secure (preventing SQL injection) and can make large queries run faster. You would just need to switch to object oriented style. (note, listing your variables in your select is what you should be doing anyway rather than select from *). The select is shortened a little for ease of explanation.
Here's an example of how yours would work with mysqli:
This is your database connection (not much different, just slightly different syntax)
$db_selected = new mysqli($db_hostname, $db_username, $db_password, $db_database)
if ($db_selected->connect_error) {
die("Connection failed: " . $dbc->connect_error);
}
Here are your queries. bind_param just identifies the item as an INT and then replaces the ? in the query with the variable $id, and bind_result takes the results of your query and turns them into variables.
The rest is fairly self-explanatory
$sql = $db_selected->prepare("SELECT client_id, first_name, last_name FROM clients WHERE client_id= ? ");
$sql->bind_param("i", $id);
$sql->execute();
$sql->store_result();
$sql->bind_result($client_id, $first_name,$last_name);
while ($sql->fetch()) {
echo '<input type="hidden" name="id" value="'.$client_id. '">';
echo '<input type="text" name="first_name" value="'.$first_name. '" required><br />';
echo '<input type="text" name="last_name" value="'. $last_name. '" required><br />';
}
$sql->free_result();
$sql->close();
If you need some more information, here are some resources: mysqli prepared statements

php + MySQL editing table data

This question is relating to 2 php scripts.
The first script is called pick_modcontact.php where I choose a contact (from a contact book like phone book), then posts to the script show_modcontact.php When I click the submit button on the form on pick.modcontact.php. As a result of submitting the form I am then taken to show_modcontact.php. As the variables are not present the user is directed back to pick_modcontact.php
I can not work out how to correct the code so that it will show the results of the script show_modcontact.php
This script shows all contacts in a database which is an "address book" this part works fine. please see below.
Name:pick_modcontact.php
if ($_SESSION['valid'] != "yes") {
header( "Location: contact_menu.php");
exit;
}
$db_name = "testDB";
$table_name = "my_contacts";
$connection = #mysql_connect("localhost", "admin", "user") or die(mysql_error());
$db = #mysql_select_db($db_name, $connection) or die(mysql_error());
$sql = "SELECT id, f_name, l_name FROM $table_name ORDER BY f_name";
$result = #mysql_query($sql, $connection) or die(mysql_error());
$num = #mysql_num_rows($result);
if ($num < 1) {
$display_block = "<p><em>Sorry No Results!</em></p>";
} else {
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$f_name = $row['f_name'];
$l_name = $row['l_name'];
$option_block .= "<option value\"$id\">$f_name, $l_name</option>";
}
$display_block = "<form method=\"POST\" action=\"show_modcontact.php\">
<p><strong>Contact:</strong>
<select name=\"id\">$option_block</select>
<input type=\"submit\" name=\"submit\" value=\"Select This Contact\"></p>
</form>";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Modify A Contact</title>
</head>
<body>
<h1>My Contact Management System</h1>
<h2><em>Modify a Contact</em></h2>
<p>Select a contact from the list below, to modify the contact's record.</p>
<? echo "$display_block"; ?>
<br>
<p>Return to Main Menu</p>
</body>
</html>
This script is for modifying the contact: named show_modcontact.php
<?php
if (!$_POST['id']) {
header( "Location: pick_modcontact.php");
exit;
} else {
session_start();
}
if ($_SESSION['valid'] != "yes") {
header( "Location: pick_modcontact.php");
exit;
}
$db_name = "testDB";
$table_name = "my_contacts";
$connection = #mysql_connect("localhost", "admin", "pass") or die(mysql_error());
$db = #mysql_select_db($db_name, $connection) or die(mysql_error());
$sql = "SELECT f_name, l_name, address1, address2, address3, postcode, prim_tel, sec_tel, email, birthday FROM $table_name WHERE id = '" . $_POST['id'] . "'";
$result = #mysql_query($sql, $connection) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$f_name = $row['f_name'];
$l_name = $row['l_name'];
$address1 = $row['address1'];
$address2 = $row['address2'];
$address3 = $row['address3'];
$country = $row['country'];
$prim_tel = $row['prim_tel'];
$sec_tel = $row['sec_tel'];
$email = $row['email'];
$birthday = $row['birthday'];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Modify A Contact</title>
</head>
<body>
<form action="do_modcontact.php" method="post">
<input type="text" name="id" value="<? echo $_POST['id']; ?>" />
<table cellpadding="5" cellspacing="3">
<tr>
<th>Name & Address Information</th>
<th> Other Contact / Personal Information</th>
</tr>
<tr>
<td align="top">
<p><strong>First Name:</strong><br />
<input type="text" name="f_name" value="<? echo "$f_name"; ?>" size="35" maxlength="75" /></p>
<p><strong>Last Name:</strong><br />
<input type="text" name="l_name" value="<? echo "$l_name"; ?>" size="35" maxlength="75" /></p>
<p><strong>Address1:</strong><br />
<input type="text" name="f_name" value="<? echo "$address1"; ?>" size="35" maxlength="75" /></p>
<p><strong>Address2:</strong><br />
<input type="text" name="f_name" value="<? echo "$address2"; ?>" size="35" maxlength="75" /></p>
<p><strong>Address3:</strong><br />
<input type="text" name="f_name" value="<? echo "$address3"; ?>" size="35" maxlength="75" /> </p>
<p><strong>Postcode:</strong><br />
<input type="text" name="f_name" value="<? echo "$postcode"; ?>" size="35" maxlength="75" /></p>
<p><strong>Country:</strong><br />
<input type="text" name="f_name" value="<? echo "$country"; ?>" size="35" maxlength="75" /> </p>
<p><strong>First Name:</strong><br />
<input type="text" name="f_name" value="<? echo "$f_name"; ?>" size="35" maxlength="75" /></p>
</td>
<td align="top">
<p><strong>Prim Tel:</strong><br />
<input type="text" name="f_name" value="<? echo "$prim_tel"; ?>" size="35" maxlength="75" /></p>
<p><strong>Sec Tel:</strong><br />
<input type="text" name="f_name" value="<? echo "$sec_tel"; ?>" size="35" maxlength="75" /></p>
<p><strong>Email:</strong><br />
<input type="text" name="f_name" value="<? echo "$email;" ?>" size="35" maxlength="75" /> </p>
<p><strong>Birthday:</strong><br />
<input type="text" name="f_name" value="<? echo "$birthday"; ?>" size="35" maxlength="75" /> </p>
</td>
</tr>
<tr>
<td align="center">
<p><input type="submit" name="submit" value="Update Contact" /></p>
<br />
<p>Retuen To Menu</p>
</td>
</tr>
</table>
</form>
</body>
</html>
note for site admin, I am re posting this question with the hope of someone else reading over it. older questions seem to go dead after a while.
I think you're missing an "=" sign on the value attribute here:
$option_block .= "<option value\"$id\">$f_name, $l_name</option>";
Maybe you meant this?
$option_block .= "<option value=\"$id\">$f_name, $l_name</option>";

Categories