Fetching data issue from mysql database in this code - php

I am creating an Invitation Card app for my upcoming event which will be held. My code successfully inserts the data into mysql database named booking having table name data. But there is problem with retrieving. When I fill the form and submit, it saves data in db but generates nothing. It gives following error:
Fatal error: Call to a member function query() on resource in C:\xampp\htdocs\booking\index.php on line 44
Here is my code, please tell me how to resolve this issue. I shall be very thankful to you.
<html>
<body>
<?php
if(isset($_POST['add'])){
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn){
die('Could not connect: ' . mysql_error());
}
if(! get_magic_quotes_gpc()){
$emp_name = addslashes($_POST['emp_name']);
$emp_fname = addslashes($_POST['emp_fname']);
$emp_cnic = addslashes($_POST['emp_cnic']);
$emp_address = addslashes($_POST['emp_address']);
} else {
$emp_name = addslashes($_POST['emp_name']);
$emp_fname = addslashes($_POST['emp_fname']);
$emp_cnic = addslashes($_POST['emp_cnic']);
$emp_address = addslashes($_POST['emp_address']);
}
$sql = "INSERT INTO data ". "(CNIC, Name, FatherName, PostalAddress) " .
"VALUES('$emp_cnic', '$emp_name', '$emp_fname', '$emp_address')";
mysql_select_db('booking');
$retval = mysql_query($sql, $conn);
if(! $retval) {die('Could not enter data: ' . mysql_error());}
?>
<table border=2>
~~~~~~Your Invitation Card~~~~~
<tr><td>Your Name</td><td><?php
$sql = "SELECT name FROM data";
$result = $conn->query($sql);
echo $result;
?></td></tr><br>
<tr><td>Your Father Name</td><td>
$sql = "SELECT fname FROM data";
$result = $conn->query($sql);
echo $result;
?></td></tr><br>
<tr><td>Your CNIC Number</td><td>
$sql = "SELECT cnic FROM data";
$result = $conn->query($sql);
echo $result;
?></td></tr><br>
<tr><td>Your Postal Address</td><td>
$sql = "SELECT address FROM data";
$result = $conn->query($sql);
echo $result;
?></td></tr><br>
<tr><td>You are informed to approach Location XA-55 at 1800 Thursday with print of this
Invitation card to paticipate in the function. </td></tr><br>
</table>
<?php
mysql_close($conn);
} else {
?>
<form method = "post" action = "<?php $_PHP_SELF ?>">
Name: <input type="text" name="emp_name" id="emp_name"><br>
Father Name: <input type="text" name="emp_fname" id="emp_fname"><br>
CNIC: <input type="text" name="emp_cnic" id="emp_cnic"><br>
Address: <input type="text" name="emp_address" id="emp_address"><br>
<input type="submit" name="add" id="add" value="Submit">
<?php
}
?>
</body></html>

Change
$result = $conn->query($sql);
To
$result = mysql_query($sql);
For more info click here

I think you should be using mysql_query instead of $conn->query

I thin i spotted two errors in your code.
you should use
mysql_query($sql,$conn);
instead of (that was mentioned before)
$result = $conn->query($sql);
You missed a couple of opening php tags in your html table.
Try following code and let me know if it works.
if(! $conn){
die('Could not connect: ' . mysql_error());
}
if(! get_magic_quotes_gpc()){
$emp_name = addslashes($_POST['emp_name']);
$emp_fname = addslashes($_POST['emp_fname']);
$emp_cnic = addslashes($_POST['emp_cnic']);
$emp_address = addslashes($_POST['emp_address']);
} else {
$emp_name = addslashes($_POST['emp_name']);
$emp_fname = addslashes($_POST['emp_fname']);
$emp_cnic = addslashes($_POST['emp_cnic']);
$emp_address = addslashes($_POST['emp_address']);
}
$sql = "INSERT INTO data ". "(CNIC, Name, FatherName, PostalAddress) " .
"VALUES('$emp_cnic', '$emp_name', '$emp_fname', '$emp_address')";
mysql_select_db('booking');
$retval = mysql_query($sql, $conn);
if(! $retval) {die('Could not enter data: ' . mysql_error());}
?>
<table border=2>
~~~~~~Your Invitation Card~~~~~
<tr><td>Your Name</td><td><?php
$sql = "SELECT name FROM data";
$result = mysql_query($sql,$conn);
echo $result;
?></td></tr><br>
<tr><td>Your Father Name</td><td>
<?php
$sql = "SELECT fname FROM data";
$result = mysql_query($sql,$conn);
echo $result;
?></td></tr><br>
<tr><td>Your CNIC Number</td><td>
<?php
$sql = "SELECT cnic FROM data";
$result = mysql_query($sql,$conn);
echo $result;
?></td></tr><br>
<tr><td>Your Postal Address</td><td>
<?php
$sql = "SELECT address FROM data";
$result = mysql_query($sql,$conn);
echo $result;
?></td></tr><br>
<tr><td>You are informed to approach Location XA-55 at 1800 Thursday with print of this
Invitation card to paticipate in the function. </td></tr><br>
</table>
<?php
mysql_close($conn);
} else {
?>
<form method = "post" action = "<?php $_PHP_SELF ?>">
Name: <input type="text" name="emp_name" id="emp_name"><br>
Father Name: <input type="text" name="emp_fname" id="emp_fname"><br>
CNIC: <input type="text" name="emp_cnic" id="emp_cnic"><br>
Address: <input type="text" name="emp_address" id="emp_address"><br>
<input type="submit" name="add" id="add" value="Submit">
<?php
}
?>
</body></html>

Related

Updating MySQL using PHP

I'm using easyPHP. I'm trying to update the records in my database but I keep getting <?php echo $btitle; ?> and <?php echo $bauthor; ?> written in the text boxes in my HTML form and data isn't updated but it does print "Updated data successfully".
Here's my form code:
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title>Update Book</title>
</head>
<body>
<h1>Update Your Library</h1>
<form method = "post" action = "editBook.php">
<?php
$conn = mysql_connect ("localhost", "root", "");
$db = mysql_select_db ("library", $conn);
$query = "select * from Books where No = ". $_GET['bid'];
$result = mysql_query($query, $conn);
while ($row = mysql_fetch_assoc($result))
{
$bid = $row ['bid'];
$btitle = $row ['btitle'];
$bauthor = $row ['bauthor'];
}
mysql_close($conn);
?>
<table>
<input type="hidden" name="bid" size="5" value="<?php echo $bid;?>">
<tr>
<td>Title:</td>
<td><input type="text" name="btitle" size="100"value="<?php echo $btitle;?>"></td>
</tr>
<tr>
<td>Author:</td>
<td><input type="text" name="bauthor" size="100" value="<?php echo $bauthor;?>"></td>
</tr>
</table>
<p>
<input type="submit" value="Update">
</p>
</form>
</body>
</html>
<?php
$dbhost = '127.0.0.1';
$dbuser = 'root';
$dbpass = '';
$bid=$_POST['bid'];
$btitle=$_POST['btitle'];
$bauthor=$_POST['bauthor'];
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db('library');
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$sql = "update books
set Title='$btitle',
Author='$bauthor'
where book_id='$bid'";
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not enter data: ' . mysql_error());
}
print "updated data successfully\n";
mysql_close($conn);
?>
It seems like the files does not get parsed by PHP. The problem may be a filename extention not defined in configuration.
On other hand I see another problem which may raise when the first issue is solved. Viariables which hold information are out of scope. This will lead to empty answer.
When you read from database:
while ($row = mysql_fetch_assoc($result))
{
$bid = $row ['bid'];
$btitle = $row ['btitle'];
$bauthor = $row ['bauthor'];
}
those 3 variables are created in the while(){} scope and they do not exist outside of it. Just initialize them with empty string before the loop in the main scope:
$bid = $btitle = $bauthor = '';
while ($row = mysql_fetch_assoc($result))
{
$bid = $row ['bid'];
$btitle = $row ['btitle'];
$bauthor = $row ['bauthor'];
}
Try This ::
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title>Update Book</title>
</head>
<body>
<h1>Update Your Library</h1>
<form method = "post" action = "save.php">
<?php
$conn = mysql_connect("localhost", "root", "");
$db = mysql_select_db("library", $conn);
$query = "select * from Books where No = " . $_GET['bid'];
$result = mysql_query($query, $conn);
while ($row = mysql_fetch_assoc($result))
{
$bid = $row['bid'];
$btitle = $row['btitle'];
$bauthor = $row['bauthor'];
}
?>
<table>
<input type="hidden" name="bid" size="5" value="<?php echo (isset($bid))?$bid:'';?>">
<tr><td>Title:</td><td><input type="text" name="btitle" size="100" value="<?php echo (isset($btitle))?$btitle:'';?>"></td></tr>
<tr><td>Author:</td><td><input type="text" name="bauthor" size="100" value="<?php echo (isset($bauthor))?$bauthor:'';?>"></td></tr>
</table>
<p><input type="submit" value="Update"></p>
</form>
</body>
</html>
<?php
mysql_close($conn);
?>

How to remove/hide text at the very top of the page

Problem
I would like to know how I would make the text at the top of the screen not show when connecting or searching the database. Below is a screenshot of what I am talking about.
Is it possible for it to not appear at all? Below is the code that connects to the database, as well as the code for the form below.
Code
The code to connect to the database:
<?php
// Connecting, selecting database
$dbconn = pg_connect("host=***** port=*****
dbname=***** user=***** password=*****")
or die('Could not connect: ' . pg_last_error());
//collect
if(isset($_POST['search'])) {
$searchq = $_POST['search'];
// $searchq = preg_replace("#[^0-9a-z]#i"."".$searchq);
// Performing SQL query
$query = "SELECT vegan FROM enumbers WHERE code LIKE '%$searchq%'";
$ret = pg_query($dbconn, $query);
if(!$ret){
echo pg_last_error($dbconn);
exit;
}
$output = '';
while($row = pg_fetch_assoc($ret)){
$code = $row['code'];
print_r($row);
$name = $row['name'];
$type = $row['type'];
$vegan = $row['vegan'];
$output .= '<div> '.vegan.' ';
}
}
echo "Operation done successfully\n";
pg_close($dbconn);
?>
The code for the form:
<div id="tablebox">
<!-- Search bar -->
<p>Is It Vegan?</p>
<form name="form1" method="post" action="searchEnumbers.php">
<input name="search" type="text" size="30" maxlength="5" />
<input name="submit" type="submit" value="Search" />
</form>
<?php
print("$output");
?>
</div>
</div>
Just remove these two lines from your code
print_r($row);
.....
echo "Operation done successfully\n";
Try this one, Changes are noted via php comment (//)
<?php
// Connecting, selecting database
$dbconn = pg_connect("host=***** port=*****
dbname=***** user=***** password=*****")
or die('Could not connect: ' . pg_last_error());
//collect
if(isset($_POST['search'])) {
$searchq = $_POST['search'];
// $searchq = preg_replace("#[^0-9a-z]#i"."".$searchq);
// Performing SQL query
$query = "SELECT vegan FROM enumbers WHERE code LIKE '%$searchq%'";
$ret = pg_query($dbconn, $query);
if(!$ret){
echo pg_last_error($dbconn);
exit;
}
$output = '';
while($row = pg_fetch_assoc($ret)){
$code = $row['code'];
//Commented 1
//print_r($row);
$name = $row['name'];
$type = $row['type'];
$vegan = $row['vegan'];
$output .= '<div> '.vegan.' ';
}
}
//Commented 2
//echo "Operation done successfully\n";
pg_close($dbconn);
?>

Page coming up blank after I compile PHP and PostgreSQL

Problem
I am having a problem displaying the page after I compiled this code, but I cannot see what is wrong with it and I cannot debug due to it not appearing on the web.
PHP and PostgreSQL Code:
<?php
// Connecting, selecting database
$dbconn = pg_connect("host=***** port=*****
dbname=***** user=***** password=*****")
or die('Could not connect: ' . pg_last_error());
//collect
if(isset($_POST['search'])) {
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i"."".$searchq);
// Performing SQL query
$query = "SELECT * FROM enumbers WHERE code LIKE '%$searchq%'") or die ("could not search!");
$result = query($query);
if($result = 0){
$output = 'There is no such E-Number!'
}else{
while($row = mysql_fetch_array($query)) {
$code = $row['code'];
$name = $row['name'];
$type = $row['type'];
$vegan = $row['vegan'];
$output .= '<div> '.vegan.' ';
}
}
}
?>
The Form and Printing Code:
<div id="tablebox">
<!-- Search bar -->
<p>Is It Vegan?</p>
<form name="form1" method="post" action="searchEnumbers.php">
<input name="search" type="text" size="30" maxlength="5" />
<input name="submit" type="submit" value="Search" />
</form>
<?php
print("$output");
?>
</div>
Update
I think the problem might be I'm using some MySQL code, but I cannot tell If I am.
You are using mysql_fetch_array instead of postgres method. Please see the sample
<?php
// Connecting, selecting database
$dbconn = pg_connect("host=**** port=****
dbname=**** user=**** password=****")
or die('Could not connect: ' . pg_last_error());
$output = '';
//collect
if(isset($_POST['search'])) {
$searchq = $_POST['search'];
// $searchq = preg_replace("#[^0-9a-z]#i"."".$searchq);
// Performing SQL query
$query = "SELECT * FROM enumbers WHERE code LIKE '%$searchq%'";
$ret = pg_query($dbconn, $query);
if(!$ret){
echo pg_last_error($dbconn);
exit;
}
$output = '';
while($row = pg_fetch_assoc($ret)){
$code = $row['code'];
print_r($row);
$name = $row['name'];
$type = $row['type'];
$vegan = $row['vegan'];
$output .= '<div> '.vegan.' ';
}
}
echo "Operation done successfully\n";
pg_close($dbconn);
?>

PHP how to insert select box value into database

i have a question
i already make a form to view two select box value with database and one text filed value
i want to know how to insert those value back into database with different table
my script on view select box value is something like this
<?php
$query = "SELECT gejala FROM gejala where idatribut =110000";
$result = mysql_query($query) or die(mysql_error()."[".$query."]");
?>
<select name="gejala1">
<?php
while ($row = mysql_fetch_array($result)) {
echo "<option value='".$row['gejala']."'>".$row['gejala']."</option>";
}
?>
</select>
<p>Subatribut2
<?php
$query = "SELECT gejala FROM gejala where idatribut =110000";
$result = mysql_query($query) or die(mysql_error()."[".$query."]");
?>
<select name="gejala2">
<?php
while ($row = mysql_fetch_array($result)) {
echo "<option value='".$row['gejala']."'>".$row['gejala']."</option>";
}
?>
</select>
<p>
<label>value
<input type="text" name="textfield">
</label>
<p>
<input name="submit" type="button" value="submit">
<p>
how to know how to insert this value into another table(pucuk) with same field
thankyou
i already try insert script
<?php
if(isset($_POST['gejala1'])) {
$gejala1 = $_POST['gejala1'];
$sql = "INSERT INTO pucuk (gejala1) VALUES ({$gejala2})";
$dbLink = mysql_connect('localhost', 'root', 'root') or die(mysql_error());
mysql_select_db('cbrteh', $dbLink) or die(mysql_errno());
$result = mysql_query($sql);
if($result) {
echo "Record successfully inserted!";
}
else {
echo "Record not inserted! (". mysql_error() .")";
}
}
if(isset($_POST['gejala2'])) {
$gejala2 = $_POST['gejala2'];
$sql = "INSERT INTO pucuk (gejala2) VALUES ({$gejala2})";
$dbLink = mysql_connect('localhost', 'root', 'root') or die(mysql_error());
mysql_select_db('cbrteh', $dbLink) or die(mysql_errno());
$result = mysql_query($sql);
if($result) {
echo "Record successfully inserted!";
}
else {
echo "Record not inserted! (". mysql_error() .")";
}
}
}
?>
</form>
but when i hit submit button it still has no effect
In while loop itself u can write insert query and send the option box value into the field u want,try it !
after a submit, your select option value will be in the $_POST[] variable under the name of your select. Namely, here "gejala2"
put your code in a form with method=POST and after submitting your form you will get all your new values in _POST array
for example
if(isset($_POST['submit']))
{
$var1=$_POST['select1'];
$var2=$_POST['select2'];
$var3=$_POST['select3'];
//then write update/insert query as your requirement
}
Try this
<?php
if(isset($_POST['gejala1'])) {
$gejala1 = $_POST['gejala1'];
$sql = "INSERT INTO pucuk (gejala1) VALUES ({$gejala2})";
$dbLink = mysql_connect('localhost', 'root', 'root') or die(mysql_error());
mysql_select_db('cbrteh', $dbLink) or die(mysql_errno());
$result = mysql_query($sql);
if($result) {
echo "Record successfully inserted!";
}
else {
echo "Record not inserted! (". mysql_error() .")";
}
}
if(isset($_POST['gejala2'])) {
$gejala2 = $_POST['gejala2'];
$sql = "INSERT INTO pucuk (gejala2) VALUES ({$gejala2})";
$dbLink = mysql_connect('localhost', 'root', 'root') or die(mysql_error());
mysql_select_db('cbrteh', $dbLink) or die(mysql_errno());
$result = mysql_query($sql);
if($result) {
echo "Record successfully inserted!";
}
else {
echo "Record not inserted! (". mysql_error() .")";
}
}
?>
<form action="" method="POST"><!-- add this -->
<?php
$query = "SELECT gejala FROM gejala where idatribut =110000";
$result = mysql_query($query) or die(mysql_error()."[".$query."]");
?>
<select name="gejala1">
<?php
while ($row = mysql_fetch_array($result)) {
echo "<option value='".$row['gejala']."'>".$row['gejala']."</option>";
}
?>
</select>
<p>Subatribut2
<?php
$query = "SELECT gejala FROM gejala where idatribut =110000";
$result = mysql_query($query) or die(mysql_error()."[".$query."]");
?>
<select name="gejala2">
<?php
while ($row = mysql_fetch_array($result)) {
echo "<option value='".$row['gejala']."'>".$row['gejala']."</option>";
}
?>
</select>
<p>
<label>value
<input type="text" name="textfield">
</label>
<p>
<input name="submit" type="submit" value="submit"> <!-- changed from type="button" to type="submit"> -->
<p>
</form>

How to update specific rows through PHP form but I get undefined index: id

I want to create a book inventory with Create, Read, Update and Delete. I got it all going except for the Update query. Here is my code. First is the proj.php
<html>
<body>
<b>BOOK INVENTORY</b>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
Title: <input type="text" name="title">
Subtitle: <input type="text" name="subtitle">
Author: <input type="text" name="author">
Genre/Type: <select name="genre">
<option value="Business">Business</option>
<option value="Family">Family</option>
<option value="Romance">Romance</option>
<option value="Education">Education</option>
<option value="Self-help">Self-help</option>
<option value="Spiritual">Spiritual</option>
<option value="Music">Music</option>
</select>
Publish Date: <input type="date" name="publishdate">
Publisher: <input type="text" name="publisher">
Series: <input onkeypress="return isNumberKey(event)" type="text" name="series">
ISBN: <input type="text" name="isbn">
Pages: <input onkeypress="return isNumberKey(event)" type="text" name="pages">
Price: <input type="text" value="$" name="price">
<input type="submit" value="Create" name="submit">
</form>
<!--create new book entry-->
<?php
include 'connection.php';
if (!isset($_POST['submit'])) {
// form not submitted
}
else {
// get form input
// check to make sure it's all there
// escape input values for greater safety
$title = empty($_POST['title']) ? die ("ERROR: Enter a Title") : mysql_escape_string($_POST['title']);
$subtitle = empty($_POST['subtitle']) ? die ("ERROR: Enter an Subtitle") : mysql_escape_string($_POST['subtitle']);
$author = empty($_POST['author']) ? die ("ERROR: Enter an Author") : mysql_escape_string($_POST['author']);
$genre = empty($_POST['genre']) ? die ("ERROR: Enter a Genre/Type") : mysql_escape_string($_POST['genre']);
$publishdate = empty($_POST['publishdate']) ? die ("ERROR: Enter a Publish Date") : mysql_escape_string($_POST['publishdate']);
$publisher = empty($_POST['publisher']) ? die ("ERROR: Enter a Publisher") : mysql_escape_string($_POST['publisher']);
$series = empty($_POST['series']) ? die ("ERROR: Enter a Series") : mysql_escape_string($_POST['series']);
$isbn = empty($_POST['isbn']) ? die ("ERROR: Enter an ISBN") : mysql_escape_string($_POST['isbn']);
$pages = empty($_POST['pages']) ? die ("ERROR: Enter a Pages") : mysql_escape_string($_POST['pages']);
$price = empty($_POST['price']) ? die ("ERROR: Enter a Price") : mysql_escape_string($_POST['price']);
// create query
$insert = "INSERT INTO books (Title, Subtitle, Author, Genre, Publishdate, Publisher, Series, ISBN, Pages, Price)
VALUES ('$title', '$subtitle', '$author', '$genre', '$publishdate', '$publisher', '$series', '$isbn', '$pages', '$price')";
// execute query
$result = mysql_query($insert) or die ("Error in query: $insert. ".mysql_error());
// print message of the new book inserted
//echo "New book record created entitled "."$ ";
}
if (!isset($_POST['submit'])) {
}
$query = "SELECT * FROM symbols";
?>
<!------------><!------------><!------------><!------------><!------------>
<!------------><!------------><!------------><!------------><!------------>
<!--Delete Record-->
<?php
// set server access variables
$host = "localhost";
$user = "root";
$pass = "";
$db = "david";
// create mysqli object
// open connection
$mysqli = new mysqli($host, $user, $pass, $db);
// check for connection errors
if (mysqli_connect_errno()) {
die("Unable to connect!");
}
// if id provided, then delete that record
if (isset($_GET['id'])) {
// create query to delete record
$query = "DELETE FROM books WHERE id = ".$_GET['id'];
// execute query
if ($mysqli->query($query)) {
// print number of affected rows
echo $mysqli->affected_rows." row(s) affected";
}
else {
// print error message
echo "Error in query: $query. ".$mysqli->error;
}
}
// query to get records
$query = "SELECT * FROM books";
// execute query
if ($result = $mysqli->query($query)) {
// see if any rows were returned
if ($result->num_rows > 0) {
// yes
// print them one after another
echo "<table cellpadding=10 border=1>";
while($row = $result->fetch_array()) {
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>Delete</td>";
echo "<td>More...</td>";
echo "</tr>";
}
}
// free result set memory
$result->close();
}
else {
// print error message
echo "Error in query: $query. ".$mysqli->error;
}
// close connection
$mysqli->close();
?>
<?php
include 'edit.php';
if (!isset($_POST['submit'])) {
}
?>
And here is my edit.php where I get the error undefined index : id at line 5
<?php
mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db("david") or die(mysql_error());
$UID = (int)$_GET['id'];
$query = mysql_query("SELECT * FROM books WHERE 'id' = '$UID'") or die(mysql_error());
if(mysql_num_rows($query)>=1){
while($row = mysql_fetch_array($query)) {
$title = $row['title'];
$subtitle = $row['subtitle'];
$author = $row['author'];
$genre = $row['genre'];
$publishdate = $row['publishdate'];
$publisher = $row['publisher'];
$series = $row['series'];
$isbn = $row['isbn'];
$pages = $row['pages'];
$price = $row['price'];
}
?>
<form action="update.php" method="get">
<input type="hidden" name="id" value="<?=$UID;?>">
Title: <input type="text" name="ud_title" value="<?=$title?>"><br>
Subtitle: <input type="text" name="ud_subtitle" value="<?=$subtitle?>"><br>
Author: <input type="text" name="ud_author" value="<?=$author?>"><br>
Genre: <input type="text" name="ud_genre" value="<?=$genre?>"><br>
Publish Date: <input type="text" name="ud_publishdate" value="<?=$publishdate?>"><br>
Publisher: <input type="text" name="ud_publisher" value="<?=$publisher?>"><br>
Series: <input type="text" name="ud_series" value="<?=$series?>"><br>
ISBN: <input type="text" name="ud_isbn" value="<?=$isbn?>"><br>
Pages: <input type="text" name="ud_pages" value="<?=$pages?>"><br>
Price: <input type="text" name="ud_price" value="<?=$price?>"><br>
<input type="Submit">
</form>
<?php
}else{
}
?>
and here is my update.php with the update query
<?php
mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db("david") or die(mysql_error());
$ud_ID = (int)$_POST["id"];
$ud_title = mysql_real_escape_string($_POST["ud_title"]);
$ud_subtitle = mysql_real_escape_string($_POST["ud_subtitle"]);
$ud_author = mysql_real_escape_string($_POST["ud_author"]);
$ud_genre = mysql_real_escape_string($_POST["ud_genre"]);
$ud_publishdate = mysql_real_escape_string($_POST["ud_publishdate"]);
$ud_publisher = mysql_real_escape_string($_POST["ud_publisher"]);
$ud_series = mysql_real_escape_string($_POST["ud_series"]);
$ud_isbn = mysql_real_escape_string($_POST["ud_isbn"]);
$ud_pages = mysql_real_escape_string($_POST["ud_pages"]);
$ud_price = mysql_real_escape_string($_POST["ud_price"]);
$query="UPDATE books
SET title = '$ud_title', subtitle = '$ud_subtitle', author = '$ud_author', genre = '$ud_genre', publishdate = '$ud_publishdate',
publisher = '$ud_publisher', series = '$ud_series', isbn = '$ud_isbn', pages = '$ud_pages', price = '$ud_price'
WHERE id='$ud_ID'";
mysql_query($query)or die(mysql_error());
if(mysql_affected_rows()>=1){
echo "<p>($ud_ID) Record Updated<p>";
}else{
echo "<p>($ud_ID) Not Updated<p>";
}
?>
Please kindly help me get rid of this error:
undefined index: id at edit.php.
I'm shocked by the terrible formating and not able to read the code.
But according to your description, you get undefined index: id because you either did not include the id field in the form, or you include it in the url but use $_POST instead of $_GET.
EDIT:
If this is exactly the code you have, then the problem should be the last few lines in proj.php.
include 'edit.php' is not the correct way to call edit page, but you should use a link tag which links to edit.php?id=some_id, just like you did with show.php

Categories