how to use php retrieve data from postgresql and showing in html - php

I was trying to retrieve data from PostgreSQL database, In PHP.
Retrieving the data is fine but it does not work when it is linked with HTML.
When I click on find button in HTML interface, it did not really retrieve data from PostgrSQL.
Can anybody help?
PHP
$host = "host=sv4gis";
$dbname = "dbname=survey";
$credentials = "user=sde password=dfd54f";
$objectid = "";
$jobno = "";
$unit = "";
$coordinate = "";
$name = "";
$northing = "";
$easting = "";
$elev = "";
$db = pg_connect( "$host $dbname $credentials" );
if(!$db){
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
function getPosts() {
$posts = array();
$posts[0] = $_POST['objectid'];
$posts[1] = $_POST['jobno'];
$posts[2] = $_POST['unit'];
$posts[3] = $_POST['coordinate'];
$posts[4] = $_POST['name'];
$posts[5] = $_POST['northing'];
$posts[6] = $_POST['easting'];
$posts[7] = $_POST['elev'];
return $posts;
}
// Selection Operation
if (isset($_POST['search'])) {
$data = getPosts();
$sql = "SELECT * FROM monu_from_Lst WHERE OBJECTID = $data[0]";
$ret = pg_query($db, $sql);
if($ret) {
if(pg_num_rows($ret)) {
while($row = pg_fetch_array($ret)) {
echo $objectid = $row['objectid'];
echo $jobno = $row['jobno'];
echo $unit = $row['unit'];
echo $coordinate = $row['coordinate'];
echo $name = $row['name'];
echo $northing = $row['northing'];
echo $easting = $row['easting'];
echo $elev = $row['elev'];
}
echo "Operation done successfully\n";
// pg_close($db);
}else{
echo 'No Data For This Id';
}
} else {
echo 'Result Error';
}
}
HTML
<form action="php_insert_update_delete_search.php" method="post">
<input type="number" name="objectid" placeholder="OBJECTID" value="<?php echo $objectid;?>">
<input type="text" name="jobno" placeholder="JOB#" value="<?php echo $jobno;?>">
<input type="text" name="unit" placeholder="UNIT" value="<?php echo $unit;?>">
<input type="text" name="coordinate" placeholder="COORDINATE" value="<?php echo $coordinate;?>">
<input type="text" name="name" placeholder="NAME" value="<?php echo $name;?>">
<input type="number" name="northing" placeholder="NORTHING" value="<?php echo $northing;?>">
<input type="number" name="northing" placeholder="NORTHING" value="<?php echo $northing;?>">
<input type="number" name="easting" placeholder="EASTING" value="<?php echo $easting;?>">
<input type="number" name="elev" placeholder="ElEVATION" value="<?php echo $elev;?>">
<div>
<input type="submit" name="search" value="Find">
</div>
</form>

Your code seems to be fine. The problem might be with
<form action="php_insert_update_delete_search.php" method="post">
If you are posting the file to itself try using
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
Make sure your query runs successfully. if your query does not run successfully
try following
$sql = "SELECT * FROM \"monu_from_Lst\" WHERE objectid= $data[0]";
I have working code as following
<?php
$host = "host=localhost"; // use your hostname
$dbname = "dbname=blogsitedb"; // use your dbname
$credentials = "user=postgres password=admin"; // use your credentials
$objectid = "";
$jobno = "";
$unit = "";
$coordinate = "";
$name = "";
$northing = "";
$easting = "";
$elev = "";
$db = pg_connect("$host $dbname $credentials");
if(!$db)
{
echo "Error : Unable to open database\n";
}
else
{
echo "Opened database successfully\n";
}
function getPosts()
{
$posts = array();
$posts[0] = $_POST['objectid'];
$posts[1] = $_POST['jobno'];
$posts[2] = $_POST['unit'];
/*
$posts[3] = $_POST['coordinate'];
$posts[4] = $_POST['name'];
$posts[5] = $_POST['northing'];
$posts[6] = $_POST['easting'];
$posts[7] = $_POST['elev'];
*/
return $posts;
}
// Selection Operation
if(isset($_POST['search']))
{
$data = getPosts();
$sql = "SELECT * FROM \"monu_from_Lst\" WHERE objectid= $data[0]";
$ret = pg_query($db, $sql);
if($ret)
{
if(pg_num_rows($ret))
{
while($row = pg_fetch_array($ret))
{
echo $objectid = $row['objectid'];
echo $jobno = $row['jobno'];
echo $unit = $row['unit'];
/*echo $coordinate = $row['coordinate'];
echo $name = $row['name'];
echo $northing = $row['northing'];
echo $easting = $row['easting'];
echo $elev = $row['elev'];*/
}
echo "Operation done successfully\n";
// pg_close($db);
}
else
{
echo 'No Data For This Id';
}
}
else
{
echo 'Result Error';
}
}
?>
<!DOCTYPE Html>
<html>
<head>
<title>PHP INSERT UPDATE DELETE SEARCH</title>
</head>
<body>
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
<input type="number" name="objectid" placeholder="OBJECTID" value="<?php echo $objectid;?>"><br><br>
<input type="text" name="jobno" placeholder="JOB#" value="<?php echo $jobno;?>"><br><br>
<input type="text" name="unit" placeholder="UNIT" value="<?php echo $unit;?>"><br><br>
<input type="text" name="coordinate" placeholder="COORDINATE" value="<?php echo $coordinate;?>"><br><br>
<input type="text" name="name" placeholder="NAME" value="<?php echo $name;?>"><br><br>
<input type="number" name="northing" placeholder="NORTHING" value="<?php echo $northing;?>"><br><br>
<input type="number" name="northing" placeholder="NORTHING" value="<?php echo $northing;?>"><br><br>
<input type="number" name="easting" placeholder="EASTING" value="<?php echo $easting;?>"><br><br>
<input type="number" name="elev" placeholder="ElEVATION" value="<?php echo $elev;?>"><br><br>
<div>
<!-- Input For Find Values With The given objectid -->
<input type="submit" name="search" value="Find">
</div>
</form>
</body>
</html>

Related

I'm Unable to set value of textbox using php variable

I'm trying to set value of html input type text textboxes to empty when user clicks Search button and empID is not matched, but its giving error:
mysqli_num_rows() expects parameter 1 to be mysqli_result
Here is code:
<html>
<body>
<form action="" method="post">
<h2>Employee Form</h2>
<input type="text" name="empID">
<input type="submit" name="searchRec" value="Search" />
<hr>
Employee ID: <input type="text" name="empIDC" value="<?php echo htmlentities($employeeID); ?>">
<br><br>
Name: <input type="text" name="name" value="<?php echo htmlentities($Name); ?>">
<br><br>
Address: <input type="text" name="address" value="<?php echo htmlentities($Address); ?>">
<br><br>
</form>
<?php
if( isset( $_REQUEST['searchRec'] ))
{
$employeeID = ($_POST["empID"]);
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "bc140_DB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT empID, Name, Address, Dateofbirth, Salary, Timein from Employee where empID == $employeeID";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result > 0)){ while($row = mysqli_fetch_array($result, MYSQL_ASSOC)) { $employeeID = $row['empID']; $Name = $row['Name']; $Address = $row['Address']; $Dateofbirth = $row['Dateofbirth']; $Salary = $row['Salary']; $timestamp = $row['timeIn']; } }else{ $employeeID = ""; $Name = ""; $Address = ""; $Dateofbirth = ""; $Salary = ""; $timestamp = ""; }
}
?>
</body>
</html>
1st : Change your code order otherwise you will get undefined error . your trying the embed the variable with html before creating the variable .
2nd : should be use single = not == empID = $employeeID
3rd : your mixing mysql with mysqli here mysql_fetch_array($result, MYSQL_ASSOC)
Change to
mysqli_fetch_array($result,MYSQLI_ASSOC);
4th: And also use isset() to confirm that variable exists or not if exists echo it otherwise echo the empty string .
5th: change your if like this if(mysqli_num_rows($result)>0){ }
file.php
<?php
if( isset( $_REQUEST['searchRec'] ))
{
......
$employeeID = $row['empID'];
$Name = $row['Name'];
$Address = $row['Address'];
$Dateofbirth = $row['Dateofbirth'];
$Salary = $row['Salary'];
$timestamp = $row['timeIn'];
......
}
?>
<html>
<body>
.....
Employee ID: <input type="text" name="empIDC" value="<?php if(isset($employeeID)){ echo htmlentities($employeeID); } else { echo ""; } ?>">
.....
</body>
</html>
you have forgotten ';' value="<?php echo htmlentities($employeeID); ?>"

PHP CRUD - Update function

I am creating the BindableCollection class which implements IBindableCollection.
What I want to do is CRUD operations above the collection (array of associative arrays). I have all of the CRUD functionallity, but I lack update function which doesn't work.
Code
public function updateFirstWhere($attr, $value, $updateCallback) {
$i = 0;
foreach($this->value as $data) {
if($data[$attr] == $value) {
call_user_func_array($updateCallback, $this->value[$i]);
return;
}
$i++;
}
}
When I pass the $this->value[$i] (which is all of the data inside collection) to the callback function and I do something like that:
public function deleteThePerson($sender) {
$this->collection->updateFirstWhere("id", $sender->data_id, function(&$data) {
$data["name"] = "durisvk";
});
}
I don't see the change in the data.
I want to set the name to durisvk but I can't.
Any Ideas Please?
</head>
<body>
<?php
// UPDATE CRUD element.
$idc = -1;
$naam = "";
$merk = "";
$typ = "";
$kl = "";
$con = new PDO ("mysql:host=localhost;dbname=cars_database", "root", "");
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(isset($_GET["action"])) {
if($_GET["action"] == "edit") {
if($_SERVER["REQUEST_METHOD"] == "POST") {
$statement = $con->prepare("UPDATE cars_assigment SET Name=?, Brand=?, Type=?, Color=? WHERE Id = ?");
$statement->bindValue(1, $_POST["Name"]);
$statement->bindValue(2, $_POST["Brand"]);
$statement->bindValue(3, $_POST["Type"]);
$statement->bindValue(4, $_POST["Color"]);
$statement->bindValue(5, $_POST["Id"]);
$statement->execute();
}
$statement = $con->prepare("SELECT * FROM cars_assigment WHERE Id = ?");
$statement->bindValue(1, $_GET["Id"]);
$statement->execute();
$record = $statement->fetchObject();
if($record != false) {
$idc = $record-> Id;
$naam = $record-> Name;
$merk = $record-> Brand;
$typ = $record-> Type;
$kl =$record-> Color;
}
}
}
?>
<h1> Update </h1>
<form method="POST">
ID: <input type="text" name="Id" value="<?php echo $idc ?>">
Naam: <input type="text" name="Name" value="<?php echo $naam ?>">
Brand: <input type="text" name="Brand" value="<?php echo $merk ?>">
Type: <input type="text" name="Type" value="<?php echo $typ ?>">
Color: <input type="text" name="Color" value="<?php echo $kl ?>">
<input name="Id" type="hidden" value="<?php echo $idc; ?>"> </input>
<input type="submit" value="Save"> </input>
</form>
</body>

How to Edit a MySQL Data base from a PHP Site

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();
?>

Only updating half the data to MySQL table

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.

PHP form not submitting until all the info entered

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

Categories