I'm trying to update a form to mysql database with php but when i add values to the input fields they're posted empty. This is the error:
Error Save [UPDATE Customers SET Forename = '', Surename = '', Father
= '', ID = '', AMKA = '', Address = '', AddressNumber = '', PostCode = '', Area = '', City = '', WHERE CustomerCode = '4']
As you can see the GET for the customer code works fine but the POST is not working.
Here is my code for the edit form:
<?php
$conn = new mysqli('localhost', 'root', 'password','erp');
if ($conn->connect_errno) {
die('Could not connect: ' . $conn->connect_error);
}
$id = $_GET['CustomerCode'];
$sql = $conn->query("SELECT Forename, Surename, FathersName, IDNumber, AMKA, Address, AddressNumber, PostCode, Area FROM Customers WHERE CustomerCode= '$id'");
$sqlList = $conn->query("SELECT City FROM Customers");
$row = $sql->fetch_array();
?>
<form action="SavedRecord.php?CustomerCode=<?php echo $id; ?>" method="post">
<table>
Name: <input type="text" name="Name" value="<?php echo $row['Forename']; ?>">
Surename: <input type="text" name="Surename" value="<?php echo $row['Surename']; ?>">
Father: <input type="text" name="Father" value="<?php echo $row['FathersName']; ?>">
ID: <input type="text" name="ID" value="<?php echo $row['IDNumber']; ?>">
AMKA: <input type="text" name="AMKA" onkeypress="return event.charCode >= 48 && event.charCode <= 57" value="<?php echo $row['AMKA']; ?>">
Address: <input type="text" name="Address" value="<?php echo $row['Address']; ?>">
Address Number: <input type="text" name="AddressNumber" onkeypress="return event.charCode >= 48 && event.charCode <= 57" value="<?php echo $row['AddressNumber']; ?>">
PostCode: <input type="text" name="PostCode" onkeypress="return event.charCode >= 48 && event.charCode <= 57" value="<?php echo $row['PostCode']; ?>">
Area: <input type="text" name="Area" value="<?php echo $row['Area']; ?>">
City: <select name="Cities">
<option>Select
<?php while($list = mysqli_fetch_array($sqlList)) { ?>
<option value="<?php echo $list['City']; ?>"><?php echo $list['City']; ?></option>
<?php if($list['City'] == $select) { echo $list['City']; } ?>
</option>
<?php } ?>
</option>
</select>
</table>
<input type="submit" value="Update">
</form>
And the update form:
<?php
$conn = new mysqli('localhost', 'root', 'password','erp');
if ($conn->connect_errno) {
die('Could not connect: ' . $conn->connect_error);
}
print_r($_POST);
$name = $_POST['Name'];
$surename = $_POST['Surename'];
$father = $_POST["Father"];
$id = $_POST["ID"];
$amka = $_POST["AMKA"];
$address = $_POST["Address"];
$addressNum = $_POST["AddressNumber"];
$postcode = $_POST["PostCode"];
$area = $_POST["Area"];
$city = $_POST["City"];
$customerCode = $_GET["CustomerCode"];
$updData = "UPDATE Customers SET
Forename = '$name',
Surename = '$surename',
Father = '$father',
ID = '$id',
AMKA = '$amka',
Address = '$address',
AddressNumber = '$addressNum',
PostCode = '$postcode',
Area = '$area',
City = '$city',
WHERE CustomerCode = '$customerCode'";
$updQuery = $conn->query($updData);
if($updQuery) {
echo "Data Updated";
} else {
echo "Error Save [".$updData."]";
}
?>
Your Error is you have misspelled the table field values. Have a check below and replace the code in the WITH Section
Replace
$updData = "UPDATE Customers SET
Forename = '$name',
Surename = '$surename',
Father = '$father',
ID = '$id', // here is your error the field name is not ID it is IDNumber
AMKA = '$amka',
Address = '$address',
AddressNumber = '$addressNum',
PostCode = '$postcode',
Area = '$area',
City = '$city',
WHERE CustomerCode = '$customerCode'";
With
$updData = "UPDATE Customers SET
Forename = '$name',
Surename = '$surename',
FathersName = '$father',
IDNumber = '$id',
AMKA = '$amka',
Address = '$address',
AddressNumber = '$addressNum',
PostCode = '$postcode',
Area = '$area',
City = '$city',
WHERE CustomerCode = '$customerCode'";
Here i will provide with the Exact output that is needed for you and i have tested it in my local-host and working fine.
Edit form Page:
<?php
$conn = new mysqli('localhost', 'root', '','erp');
if ($conn->connect_errno) {
die('Could not connect: ' . $conn->connect_error);
}
$id = $_GET['CustomerCode'];
$sql = $conn->query("SELECT Forename, Surename, FathersName, IDNumber, AMKA, Address, AddressNumber, PostCode, Area, City FROM Customers WHERE CustomerCode= '$id'");
$sqlList = $conn->query("SELECT City FROM Customers");
$row = $sql->fetch_array();
?>
<form action="SavedRecord.php?CustomerCode=<?php echo $id; ?>" method="post">
<table>
Name: <input type="text" name="Name" value="<?php echo $row['Forename']; ?>">
Surename: <input type="text" name="Surename" value="<?php echo $row['Surename']; ?>">
Father: <input type="text" name="Father" value="<?php echo $row['FathersName']; ?>">
ID: <input type="text" name="ID" value="<?php echo $row['IDNumber']; ?>">
AMKA: <input type="text" name="AMKA" onkeypress="return event.charCode >= 48 && event.charCode <= 57" value="<?php echo $row['AMKA']; ?>">
Address: <input type="text" name="Address" value="<?php echo $row['Address']; ?>">
Address Number: <input type="text" name="AddressNumber" onkeypress="return event.charCode >= 48 && event.charCode <= 57" value="<?php echo $row['AddressNumber']; ?>">
PostCode: <input type="text" name="PostCode" onkeypress="return event.charCode >= 48 && event.charCode <= 57" value="<?php echo $row['PostCode']; ?>">
Area: <input type="text" name="Area" value="<?php echo $row['Area']; ?>">
City: <select name="Cities">
<option>Select
<?php while($list = mysqli_fetch_array($sqlList)) { ?>
<option value="<?php echo $list['City']; ?>" <?php if($list['City']==$row['City']){echo 'selected="selected"';} ?>><?php echo $list['City']; ?></option>
</option>
<?php } ?>
</option>
</select>
</table>
<input type="submit" value="Update">
</form>
SavedRecord.php
<?php
$conn = new mysqli('localhost', 'root', '','erp');
if ($conn->connect_errno) {
die('Could not connect: ' . $conn->connect_error);
}
print_r($_POST);
$name = $_POST['Name'];
$surename = $_POST['Surename'];
$father = $_POST["Father"];
$id = $_POST["ID"];
$amka = $_POST["AMKA"];
$address = $_POST["Address"];
$addressNum = $_POST["AddressNumber"];
$postcode = $_POST["PostCode"];
$area = $_POST["Area"];
$city = $_POST["Cities"];
$customerCode = $_GET["CustomerCode"];
$updData = "UPDATE Customers SET
Forename = '$name',
Surename = '$surename',
FathersName = '$father',
IDNumber = '$id',
AMKA = '$amka',
Address = '$address',
AddressNumber = '$addressNum',
PostCode = '$postcode',
Area = '$area',
City = '$city's
WHERE CustomerCode = '$customerCode'";
$updQuery = $conn->query($updData);
if($updQuery) {
echo "Data Updated";
} else {
echo "Error Save [".$updData."]";
}
?>
This code works perfect. have it a try and let me know if any hurdles happens to you again.
Related
I am trying to populate afew fields using a load button when I select from the dropdown list. However, the load button is not working and the php codes look fine to me. So I am wondering which part went wrong.
I am wondering if I should put the load button at AuthorityCode or below the form. However, I have tried both methods and both doesn't work.
<strong>Authority Code: </strong>
<select name=authorityid value=authorityid>Select Authority</option>
<option value = "">Select</option><br/>
<?php
$connection = new mysqli("localhost", "username", "password", "dbname");
$stmt = $connection->prepare("SELECT AuthorityId FROM AuthorityList");
$stmt->execute();
$stmt->bind_result($authorityid);
while($stmt->fetch()){
echo "<option value = '$authorityid'>$authorityid</option>";
}
$stmt->close();
$connection->close();
?>
</select>
<?php
if(isset($_POST["loadbtn"])){
$authorityid = $_POST["authorityid"];
$conn = new mysqli("localhost", "username", "password", "dbname");
$stmt = $conn->prepare("SELECT AuthorityName, Address, TelephoneNo, FaxNo FROM AuthorityList WHERE AuthorityId = '$authorityid'");
$stmt->execute();
$result = mysqli_query($stmt, $conn);
$details = mysqli_fetch_array($result);
$savedName = $details["AuthorityName"];
$savedAddress = $details["Address"];
$savedTel = $details["TelephoneNo"];
$savedFax = $details["FaxNo"];
}
// while ($row = $result->fetch_array(MYSQLI_NUM)){
// $authorityname = $row[0];
// $address = $row[1];
// $telephone = $row[2];
// $fax = $row[3];
// }
?>
<form action="" method="post" >
<input type="submit" value="Load" name="loadbtn"><br/><br/>
<strong>Authority Name: </strong> <input type="text" name="authorityname" value="<?php echo $savedName; ?>"/><br/>
<strong>Address: </strong> <input type="text" name="address" value="<?php echo $savedAddress; ?>"/><br/>
<strong>Telephone No: </strong> <input type="text" name="telephone" value="<?php echo $savedTel; ?>"/><br/>
<strong>Fax No: </strong> <input type="text" name="fax" value="<?php echo $savedFax; ?>"/><br/>
Change form to:
<form action="window.location.reload()" method="post" >
Your select values are not getting submitted as they are not part of the form. Also your 2nd query was not running properly. I've cheanged it below. Please take a look
Try this-
<strong>Authority Code: </strong>
<form action="" method="post" >
<select name="authorityid">Select Authority
<option value = "">Select</option><br/>
<?php
$connection = new mysqli("localhost", "username", "password", "dbname");
$stmt = $connection->prepare("SELECT AuthorityId FROM AuthorityList");
$stmt->execute();
$stmt->bind_result($authorityid);
while($stmt->fetch()){
echo "<option value = '$authorityid'>$authorityid</option>";
}
$stmt->close();
$connection->close();
?>
</select>
<?php
if(isset($_POST["loadbtn"])){
$authorityid = $_POST["authorityid"];
$conn = new mysqli("localhost", "username", "password", "dbname");
$qry = "SELECT AuthorityName, Address, TelephoneNo, FaxNo FROM AuthorityList WHERE AuthorityId = '$authorityid'";
$result = $conn->query($qry);
$details = mysqli_fetch_array($result);
$savedName = $details["AuthorityName"];
$savedAddress = $details["Address"];
$savedTel = $details["TelephoneNo"];
$savedFax = $details["FaxNo"];
}
?>
<input type="submit" value="Load" name="loadbtn"><br/><br/>
<strong>Authority Name: </strong>
<input type="text" name="authorityname" value="<?php echo isset($savedName) ? $savedName : ''; ?>"/>
<br/>
<strong>Address: </strong>
<input type="text" name="address" value="<?php echo isset($savedAddress) ? $savedAddress : ''; ?>"/>
<br/>
<strong>Telephone No: </strong>
<input type="text" name="telephone" value="<?php echo isset($savedTel) ? $savedTel : ''; ?>"/>
<br/>
<strong>Fax No: </strong>
<input type="text" name="fax" value="<?php echo isset($savedFax) ? $savedFax : ''; ?>"/>
<br/>
</form>
Your error seems to be from mysql part of the code. Below you'll find a sample code that has mysql part removed from it and is running-
<!DOCTYPE html>
<html>
<head>
<title>Fix</title>
</head>
<body>
<strong>Authority Code: </strong>
<form action="" method="post" >
<select name="authorityid">Select Authority
<option value = "">Select</option><br/>
<?php
echo "<option value = '123'>123</option>";
echo "<option value = '234'>234</option>";
echo "<option value = '345'>345</option>";
echo "<option value = '456'>456</option>";
?>
</select>
<?php
if(isset($_POST["loadbtn"])){
$authorityid = $_POST["authorityid"];
if ($authorityid == '123') {
$savedName = 'nameTest';
$savedAddress = 'addressTest';
$savedTel = 'telTest';
$savedFax = 'faxTest';
}
}
?>
<input type="submit" value="Load" name="loadbtn"><br/><br/>
<strong>Authority Name: </strong>
<input type="text" name="authorityname" value="<?php echo isset($savedName) ? $savedName : ''; ?>"/>
<br/>
<strong>Address: </strong>
<input type="text" name="address" value="<?php echo isset($savedAddress) ? $savedAddress : ''; ?>"/>
<br/>
<strong>Telephone No: </strong>
<input type="text" name="telephone" value="<?php echo isset($savedTel) ? $savedTel : ''; ?>"/>
<br/>
<strong>Fax No: </strong>
<input type="text" name="fax" value="<?php echo isset($savedFax) ? $savedFax : ''; ?>"/>
<br/>
</form>
</body>
</html>
Edited: changed the mysqli_query function so that it works properly.
I have built a database named "artStore" and a table within that database named "inventory". I've been able to figure out how to create a new entry to the database, but I'm trying to create a page that can edit those entries.
Here is "inventory" the table I created:
$sql = "CREATE TABLE inventory (
id INT(6) AUTO_INCREMENT PRIMARY KEY,
product VARCHAR(30),
category VARCHAR(30),
seller VARCHAR(30)
)";
Here is what I'm currently trying:
<?php
$resultProduct = "product";
$resultCategory = "category";
$resultSeller = "seller";
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$resultProduct = $row["product"];
$resultCategory = $row["category"];
$resultSeller = $row["seller"];
}
} else {
echo "No Results";
}
?>
<form action="update.php" method="POST">
<label for="product">Product</label>
<input id="product" type="text" name="product" value="<?php echo $resultProduct; ?>">
<br>
<label for="category">Category:</label>
<input id="category" type="text" name="category" value="<?php echo $resultCategory; ?>">
<br>
<label for="seller">Seller:</label>
<input id="seller" type="text" name="seller" value="<?php echo $resultSeller; ?>">
<br>
<input id="id" type="text" name="id" value="<?php echo $resultId; ?>" style="display:none;">
<input type="submit" value="Update My Record">
</form>
What I'm trying in update.php
$product = $_POST['product'];
$category = $_POST['category'];
$seller = $_POST['seller'];
$sql = "INSERT INTO inventory (product, category, seller) VALUES ('$product', '$category', '$seller')";
if ($connection->query($sql) === true) {
echo "Inserted Successfully";
} else {
echo "Error occured in the insert: " . $connection->error;
}
$connection->close();
Try the below code I added hidden field on form and changes on your sql query
<?php
$resultProduct = "product";
$resultCategory = "category";
$resultSeller = "seller";
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$resultProduct = $row["product"];
$resultCategory = $row["category"];
$resultSeller = $row["seller"];
$resultId = $row["id"];
}
} else {
echo "No Results";
}
?>
<form action="update.php" method="POST">
<input type="text" name="update_id" value="<?php echo $resultId; ?>">
<label for="product">Product</label>
<input id="product" type="text" name="product" value="<?php echo $resultProduct; ?>">
<br>
<label for="category">Category:</label>
<input id="category" type="text" name="category" value="<?php echo $resultCategory; ?>">
<br>
<label for="seller">Seller:</label>
<input id="seller" type="text" name="seller" value="<?php echo $resultSeller; ?>">
<br>
<input id="id" type="text" name="id" value="<?php echo $resultId; ?>" style="display:none;">
<input type="submit" value="Update My Record">
</form>
In update.php
$product = $_POST['product'];
$category = $_POST['category'];
$seller = $_POST['seller'];
$updateId = $_POST['update_id'];
$sql = "UPDATE inventory set product = '$product',category='$category',seller='$seller' WHERE id = '$updateId'";
<?php
$resultProduct = "product";
$resultCategory = "category";
$resultSeller = "seller";
$resultId = "product_id";
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$resultProduct = $row["product"];
$resultCategory = $row["category"];
$resultSeller = $row["seller"];
$resultid = $row["id"];
}
} else {
echo "No Results";
}
?>
You were using $resultId in the form but you have not declared and assigned a value to it in the loop.
<form action="update.php" method="POST">
<input id="id" type="hidden" name="id" value="<?php echo $resultId; ?>">
<label for="product">Product</label>
<input id="product" type="text" name="product" value="<?php echo $resultProduct; ?>">
<br>
<label for="category">Category:</label>
<input id="category" type="text" name="category" value="<?php echo $resultCategory; ?>">
<br>
<label for="seller">Seller:</label>
<input id="seller" type="text" name="seller" value="<?php echo $resultSeller; ?>">
<br>
<input type="submit" value="Update My Record">
</form>
<!-- Instead of passing the ID in textbox with display: none you can pass it directly in hidden -->
<?php
$product = $_POST['product'];
$category = $_POST['category'];
$seller = $_POST['seller'];
$product_id = $_POST['id'];
if($product_id!='')//MEANS WE HAVE TO UPDATE THE RECORD
{
// UPDATE QUERY WILL UPDATE THE SAME RECORD ONLY MATCHING THE UNIQUE PRODUCT ID
$sql = "UPDATE inventory SET product = '$product', category = '$category', seller = '$seller' WHERE id = '$product_id' LIMIT 1";
if ($connection->query($sql) === true) {
echo "Updated Successfully";
} else {
echo "Error occured in the insert: " . $connection->error;
}
}
else// If id is blank it means you have a new record
{
$sql = "INSERT INTO inventory (product, category, seller) VALUES ('$product', '$category', '$seller')";
if ($connection->query($sql) === true) {
echo "Inserted Successfully";
} else {
echo "Error occured in the insert: " . $connection->error;
}
}
$connection->close();
?>
The code below is for updating data in a MySQL table. It was written by pulling all the data from one query but I have tried to adapt it to pull data from two queries to improve the ordering. Now only some of the records update when the submit button is clicked and I'm not sure how to fix it.
The original code was:
if(isset($_POST['submit'])){
$password = $_POST['password'];
$total = $_POST['total'];
$park_id = $_POST['park_id'];
if($password=="****"){
for($i =1; $i<=$total; $i++){
$ride_id = $_POST['ride_id'.$i];
$name = $_POST['ride_name'.$i];
$type = $_POST['type'.$i];
$topride = $_POST['topride'.$i];
$info = $_POST['info'.$i];
$speed = $_POST['speed'.$i];
$height = $_POST['height'.$i];
$length = $_POST['length'.$i];
$inversions = $_POST['inversions'.$i];
$query = "update tpf_rides set name='$name',type='$type'";
if($topride!=""){$query .= ",top_ride=$topride";}
if($info!=""){$query .= ",info='$info'";}
if($height!=""){$query .= ",height=$height";}
if($length!=""){$query .= ",length=$length";}
if($speed!=""){$query .= ",speed=$speed";}
if($inversions!=""){$query .= ",inversions=$inversions";}
$query .= " where ride_id=".$ride_id." and park_id=".$park_id;
mysql_query($query);
}
header('location:index.php?msg=Successfully Updated.');
}else{
echo "Enter Correct Password.";
}
}
if(isset($_GET['id'])){
$id = $_GET['id'];
$sql = "select name from tpf_parks where park_id=".$id;
$result = mysql_fetch_array(mysql_query($sql));
echo '<h2>'.$result['name'].'</h2>';
$qry = "select * from tpf_rides where park_id=".$id;
$res = mysql_query($qry);
$no = mysql_num_rows($res);
$x = 0;
if($no>0){ ?>
<form action="" method="post">
<input type="hidden" value="<?=$no?>" name="total">
<input type="hidden" value="<?=$id?>" name="park_id">
<table> <?php
while($row = mysql_fetch_array($res)){ $x++;
echo '<input type="hidden" value="'.$row['ride_id'].'" name="ride_id'.$x.'">';
echo '<tr><td>Name : </td><td><input type="text" name="ride_name'.$x.'" value="'.$row['name'].'"></td></tr>';
echo '<tr><td>Type : </td><td><input type="text" name="type'.$x.'" value="'.$row['type'].'"></td></tr>';
echo '<tr><td>Top Ride : </td><td><input type="text" name="topride'.$x.'" value="'.$row['top_ride'].'"></td></tr>';
echo '<tr><td>Info : </td><td><input type="text" name="info'.$x.'" value="'.$row['info'].'"></td></tr>';
if($row['type']!="Roller Coaster"){
echo '<tr><td>Speed : </td><td><input type="text" name="speed'.$x.'" value="'.$row['speed'].'"></td></tr>';
echo '<tr><td>Height : </td><td><input type="text" name="height'.$x.'" value="'.$row['height'].'"></td></tr>';
echo '<tr><td>Length : </td><td><input type="text" name="length'.$x.'" value="'.$row['length'].'"></td></tr>';
echo '<tr><td>Inversions : </td><td><input type="text" name="inversions'.$x.'" value="'.$row['inversions'].'"></td></tr>';
}
echo '<tr><td colspan="2"><hr></td></tr>';
} ?>
<tr><td>Password :</td><td><input type="password" value="" name="password" id="password"></td></tr>
<tr><td></td><td><input onclick="return check()" type="submit" value="Save" name="submit"></td></tr>
</table>
</form>
<?php
}else{
echo "No Rides in this park.";
}
}else{
if(isset($_GET['msg'])){echo $_GET['msg'].'<br>';}
$qry = "select * from tpf_parks order by name";
$res = mysql_query($qry);
?>
Select Park : <select name="park" onChange="getdata(this.options[this.selectedIndex].value)">
<option value="">Select Park</option>
<?php
while($row = mysql_fetch_array($res)) { ?>
<option value="<?=$row['park_id']?>"><?=$row['name']?></option>
<? } ?>
</select>
<?php } ?>
and the new code where I altered the queries is here:
if(isset($_POST['submit'])){
$password = $_POST['password'];
$total = $_POST['total'];
$park_id = $_POST['park_id'];
if($password=="*****"){
for($i =1; $i<=$total; $i++){
$ride_id = $_POST['ride_id'.$i];
$name = $_POST['ride_name'.$i];
$type = $_POST['type'.$i];
$topride = $_POST['topride'.$i];
$info = $_POST['info'.$i];
$speed = $_POST['speed'.$i];
$height = $_POST['height'.$i];
$length = $_POST['length'.$i];
$inversions = $_POST['inversions'.$i];
$query = "update tpf_rides set name='$name',type='$type'";
if($topride!=""){$query .= ",top_ride=$topride";}
$query .= ",info='$info'";
if($height!=""){$query .= ",height=$height";}
if($length!=""){$query .= ",length=$length";}
if($speed!=""){$query .= ",speed=$speed";}
if($inversions!=""){$query .= ",inversions=$inversions";}
$query .= " where ride_id=".$ride_id." and park_id=".$park_id;
mysql_query($query);
}
header('location:index.php?msg=Successfully Updated.');
}else{
echo "Enter Correct Password.";
}
}
if(isset($_GET['id'])){
$id = $_GET['id'];
$sql = "select name from tpf_parks where park_id=".$id;
$result = mysql_fetch_array(mysql_query($sql));
echo '<h2>'.$result['name'].'</h2>';
$qry = "SELECT * FROM tpf_rides
WHERE park_id = $id AND type LIKE '%Roller Coaster%' ORDER BY name ASC";
$res = mysql_query($qry);
$qry2 = "SELECT * FROM tpf_rides
WHERE park_id = $id AND type NOT LIKE '%Roller Coaster%' ORDER BY name ASC";
$res2 = mysql_query($qry2);
$qry3 = "SELECT * FROM tpf_rides WHERE park_id = $id";
$res3 = mysql_query($qry2);
$no = mysql_num_rows($res3);
$x = 0;
$xx = 0;
if($no>0){ ?>
<form action="" method="post">
<input type="hidden" value="<?=$no?>" name="total">
<input type="hidden" value="<?=$id?>" name="park_id">
<table> <?php
while($row = mysql_fetch_array($res)){ $x++;
echo '<input type="hidden" value="'.$row['ride_id'].'" name="ride_id'.$x.'">';
echo '<tr><td>Name : </td><td><input type="text" name="ride_name'.$x.'" value="'.$row['name'].'"></td></tr>';
echo '<tr><td>Type : </td><td><input type="text" name="type'.$x.'" value="'.$row['type'].'"></td></tr>';
echo '<tr><td>Top Ride : </td><td><input type="text" name="topride'.$x.'" value="'.$row['top_ride'].'"></td></tr>';
echo '<tr><td>Info : </td><td><input type="text" name="info'.$x.'" value="'.$row['info'].'"></td></tr>';
echo '<tr><td>Speed : </td><td><input type="text" name="speed'.$x.'" value="'.$row['speed'].'"></td></tr>';
echo '<tr><td>Height : </td><td><input type="text" name="height'.$x.'" value="'.$row['height'].'"></td></tr>';
echo '<tr><td>Length : </td><td><input type="text" name="length'.$x.'" value="'.$row['length'].'"></td></tr>';
echo '<tr><td>Inversions : </td><td><input type="text" name="inversions'.$x.'" value="'.$row['inversions'].'"></td></tr>';
echo '<tr><td colspan="2"><hr></td></tr>';
}
while($row2 = mysql_fetch_array($res2)){ $xx++;
echo '<input type="hidden" value="'.$row2['ride_id'].'" name="ride_id'.$xx.'">';
echo '<tr><td>Name : </td><td><input type="text" name="ride_name'.$xx.'" value="'.$row2['name'].'"></td></tr>';
echo '<tr><td>Type : </td><td><input type="text" name="type'.$xx.'" value="'.$row2['type'].'"></td></tr>';
echo '<tr><td>Top Ride : </td><td><input type="text" name="topride'.$xx.'" value="'.$row2['top_ride'].'"></td></tr>';
echo '<tr><td>Info : </td><td><input type="text" name="info'.$xx.'" value="'.$row2['info'].'"></td></tr>';
echo '<tr><td colspan="2"><hr></td></tr>';
}
?>
<tr><td>Password :</td><td><input type="password" value="" name="password" id="password"></td></tr>
<tr><td></td><td><input onclick="return check()" type="submit" value="Save" name="submit"></td></tr>
</table>
</form>
<?php
}else{
echo "No Rides in this park.";
}
}else{
if(isset($_GET['msg'])){echo $_GET['msg'].'<br>';}
$qry = "select * from tpf_parks order by name";
$res = mysql_query($qry);
?>
Select Park : <select name="park" onChange="getdata(this.options[this.selectedIndex].value)">
<option value="">Select Park</option>
<?php
while($row = mysql_fetch_array($res)) { ?>
<option value="<?=$row['park_id']?>"><?=$row['name']?></option>
<? } ?>
</select>
<?php } ?>
After testing what is not getting amended, it is data from both the LIKE and NOT LIKE queries being skipped so perhaps a record count problem?
Any ideas what I have done wrong?
Are you sure your column type has value 'Roller Coaster' or not?
Sorry to post as an answer as I have no rights no comment.
I am trying to update my products table using a product form but it is throwing an error for an undefined variable here is my code for the productform.php
<?php
$ProductID= $_GET['ProductID'];
//Connect and select a database
mysql_connect ("localhost", "root", "");
mysql_select_db("supplierdetails");
{
$result = mysql_query("SELECT * FROM products WHERE ProductID=$ProductID");
while($row = mysql_fetch_array($result))
{
$ProductID= $_GET['ProductID'];
$ProdDesc = $row['ProdDesc'];
$SupplierID = $row['SupplierID'];
$Location = $row['Location'];
$Cost = $row['Cost'];
$Status = $row['Status'];
$MRL = $row['MRL'];
$ProductID = $row['ProductID'];
}
?>
//form
<form action="Edit_Prod.php" method="post">
<input type="hidden" name="ProductID" value="<?php echo $ProductID; ?>"/>
<label>Product Description:
<span class="small">Please enter a description for the product </span>
</label>
<input type="text" name="ProdDesc" value="<?php echo $ProdDesc ;?>" />
<label>Supplier ID
<span class="small">Please enter the Supplier ID</span>
</label>
<input type="text" name="SupplierID" value="<?php echo $SupplierID ;?>" />
<label>Bay Location:
<span class="small">Please select the bay location for this product </span>
</label>
<select name="Location" value="<?php echo $Location ;?>" />
<option>A1</option>
<option>A2</option>
<option>A3</option>
<option>A4</option>
<option>A5</option>
<option>B1</option>
<option>B2</option>
<option>B3</option>
<option>B4</option>
<option>B5</option>
<option>C1</option>
<option>C2</option>
<option>C3</option>
<option>C4</option>
<option>C5</option>
<option>D1</option>
<option>D2</option>
<option>D3</option>
<option>D4</option>
<option>D5</option>
<option>E1</option>
<option>E2</option>
<option>E3</option>
<option>E4</option>
<option>E5</option>
</select>
<label>Product Cost:
<span class="small">Please enter a new cost for the product (per roll) </span>
</label>
<input type="text" name="Cost" value="<?php echo $Cost ;?>" />
<label>Status:
<span class="small">Please select a status for the product </span>
</label>
<select name="Status" value="<?php echo $Status ;?>" />
<option>Live</option>
<option>Mature</option>
<option>Obsolete</option>
</select>
<label>MRL
<span class="small">Please enter a minimum re-order level for this product </span>
</label>
<input type="text" name="MRL" value="<?php echo $MRL ;?>" />
<input type="submit" value= "Edit Product Details"/>
Undefined variable all lines, do i have to do anything differently if using drop down areas*
*Edit_Prod.php*
<?php
$con = mysql_connect("localhost", "root", "");
mysql_select_db("supplierdetails");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
//Run a query
$ProductID = $_POST['ProductID'];
$result = mysql_query ("SELECT * FROM products WHERE Productid= '".$Productid."'") or die(mysql_error());
$row = mysql_fetch_array($result);
$ProdDesc = $_POST['ProdDesc'];
$SupplierID = $_POST['SupplierID'];
$Location = $_POST['Location'];
$Cost = $_POST['Cost'];
$Status = $_POST['Status'];
$MRL = $_POST['MRL'];
$Productid=$row['Productid'];
$query="UPDATE products SET ProdDesc='".$ProdDesc."', SupplierID='".$SupplierID."', Location='".$Location."', Cost='".$Cost."', Status='".$Status."', MRL='".$MRL."' WHERE ProductID='".$ProductID."'";
$result = mysql_query($query);
//Check whether the query was successful or not
if($result)
{
echo "Products Updated";
header ("Location: Products.php");
}
else
{
die ("Query failed");
}
?>
i think $row vairable is used to get values from database which you have missed
$productID= $_GET['productid'];
$records =mysql_query(" select * from tableName where productid=$productID");
$row=mysql_fetch_array($records);
then your code will work
$supplId= $row['supplId'];
...
Answering to your question "do i have to do anything differently if using drop down areas", of course you have to change it. Change:
<select name="Location" value="<?php echo $Location ;?>" />
<option>A1</option>
<option>A2</option>
<option>A3</option>
<option>A4</option>
<option>A5</option>
.
.
. . .. .
To:
<select name="Location" />
<option value="<?=$Location;?>" selected="selected" ></option>
<option>A1</option>
<option>A2</option>
<option>A3</option>
<option>A4</option>
<option>A5</option>
like that. I think you got the idea. You have to get value as an option under select. Try it. Good luck.
I am submitting an HTML form using php and I have these fields in my form:
Name:(varchar)
Address:(varchar)
FAX:(int)
PHONE:(int)
EMAIL:(varchar)
plus a primary ID that gets added on its own.
Now my form doesn't get submitted until I enter the fax field? Does anyone know why is it happening?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<body>
<?php
require_once('../../public_html/input.php');
require_once('../../public_html/nauth.php');
require_once('../../public_html/mysql.php');
if (isset($_GET['more']))
{
$more = $_GET['more'];
$new = 8;
}
if (isset($_POST['submit']))
{
$more = 4;
$new = 8;
$name = $_SESSION['username'];
$dbc = mysqli_connect(cname, chost, cpwd, cdb);
$com_name = mysqli_real_escape_string($dbc, trim($_POST['com_name']));
$com_address = mysqli_real_escape_string($dbc, trim($_POST['com_address']));
$com_phone = mysqli_real_escape_string($dbc, trim($_POST['com_phone']));
$com_fax = mysqli_real_escape_string($dbc, trim($_POST['com_fax']));
$source_id = $_POST['source'];
$com_email = mysqli_real_escape_string($dbc, trim($_POST['com_email']));
$entered_by = $name;
$query = "select * from company_customers where name='$com_name'";
$data = mysqli_query($dbc, $query);
if (mysqli_num_rows($data) == 0) //to check if the name already exists
{
$query="insert into company_customers(name,address,phone,fax,email,entered_by,source_id) values('$com_name','$com_address','$com_phone','$com_fax','$com_email','$entered_by','$source_id')";
mysqli_query($dbc, $query);
$query = "select * from company_customers where name='$com_name'";
$data = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($data);
$id = $row['id']; //to add the company id in the contacts table
$i = 0;
while (!empty($_POST['name' . $i]))
{
$name = $_POST['name' . $i];
$designation = $_POST['desig' . $i];
$phone = $_POST['phone' . $i];
$query = "insert into contacts(name,designation,id,phone) values('$name','$designation','$id','$phone')";
mysqli_query($dbc, $query);
$i++;
}
mysqli_close($dbc);
echo 'Added';
require_once('option.php');
exit();
}
else echo 'company already entered';
}
else if (isset($_POST['more']))
{
$more = $_POST['count'];
$new = $more + 4;
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<fieldset><legend>Company Profile</legend>
<label for="com_name">Company Name:</label><input type="text" name="com_name" value="<?php if(isset($_POST['more'])) echo $_POST['com_name']; ?>"/><br />
<label for="com_address">Company Address:</label><input type="text" name="com_address" value="<?php if(isset($_POST['more'])) echo $_POST['com_address']; ?>" /><br />
<label for="com_phone">Company Phone:</label><input type="text" name="com_phone" value="<?php if(isset($_POST['more'])) echo $_POST['com_phone']; ?>" /><br />
<label for="com_fax">Company Fax:</label><input type="text" name="com_fax" value="<?php if(isset($_POST['more'])) echo $_POST['com_fax']; ?>" /><br />
<label for="com_email">Company Email:</label><input type="text" name="com_email" value="<?php if(isset($_POST['more'])) echo $_POST['email']; ?>" /><br />
<?php
$dbc = mysqli_connect(cname, chost, cpwd, cdb);
$query = "select * from source";
$data = mysqli_query($dbc, $query);
$number_row = mysqli_num_rows($data);
$source[0] = '';
$source_id[0] = '';
$i = 1;
while ($row = mysqli_fetch_array($data))
{
$source[$i] = $row['src'];
$source_id[$i] = $row['source_id'];
$i++;
}
echo '<label for="source">Source</label><select name="source">';
for ($i = 0; $i <= $number_row; $i++)
echo '<option value="' . $source_id[$i] . '">' . $source[$i] . '</option>';
echo '</select>';
?>
</fieldset>
<fieldset><legend>Company Contact</legend>
<?php
for ($i = 0; $i < $more; $i++)
{
?>
<label for="<?php echo 'name' . $i; ?>">Name:</label><input type="text" name="<?php echo 'name' . $i; ?>" value="<?php if (isset($_POST['more'])) echo $_POST['name' . $i]; ?>" />
<label for="<?php echo 'desig' . $i; ?>">Designation:</label><input type="text" name="<?php echo 'desig' . $i; ?>" value="<?php if (isset($_POST['more'])) echo $_POST['desig' . $i];?>" />
<label for="<?php echo 'phone' . $i;?>">Phone:</label><input type="text" name="<?php echo 'phone' . $i;?>" value="<?php if (isset($_POST['more'])) echo $_POST['phone' . $i]; ?>" /><br />
<?php
}
?>
<input type="hidden" name="count" value="<?php echo $new; ?>" />
<input type="submit" name="more" value="more" />
</fieldset>
<input type="submit" name="submit" value="ADD" />
</form>
</body>
</html>
Try setting the column to allow null values in mysql. E.g.
ALTER TABLE company_customers CHANGE fax fax INT(11) NULL