Updating a Record Inside a Webpage

Hi

I’ve created 3 webpages to update a record within a phpmyadmin table.
The 1st page shows the database content and asks the user to select the “record id” they wish to update.
The 2nd page shows a form to change the content of the record.
The 3rd page offers the user a button to update another record, and includes the updating code.

The pages run together fine with no errors, but when i view my table, the record that should “supposedly” be changed remains the same.
If anyone could point me in the right direction it would be much appreciated!

//////////////////////////////////////
1st page:

<?php
$conn = mysql_connect (“localhost”,“root”,“”) or die (mysql_error());
mysql_select_db(“Library”);

$sql= “SELECT * FROM books”;
$result= mysql_query($sql, $conn) or die (mysql_error());
$num_rows = mysql_num_rows($result);
print “<b>There are $num_rows records in the database:</b><p>”;

print " <table>
";

print " <tr>
";
while ($field = mysql_fetch_field($result))
{
print " <th>$field->name</th>
";
}
print " </tr>
";

while($row= mysql_fetch_assoc($result))
{
print " <tr>
";
foreach ($row as $name => $value)
{
print " <td>$value</td>
";
}
print " </tr>
";
}

print " </table>
";
?>
<form method =“POST” action =“updatechangeform.php”>
<p>
Enter the Book ID to Edit: <input type=“text” name=“BookID” size=“5”>
<input type=“submit” value=“Update”>
<input type=“reset”>
</p>

///////////////////////////////////////////
2nd page:

<?php
include(“dbinfo.inc.php”);
$conn=@mysql_connect(localhost,$username,$password);
$rs=@mysql_select_db(“library”) or die( “Unable to select database”);
$id=$_POST[‘BookID’];

$sql=“SELECT * FROM books WHERE BookID=‘$id’”;

$result=mysql_query($sql);
$num=mysql_num_rows($result);

$i=0;
while ($i < $num) {
$title=mysql_result($result,$i,‘title’);
$cost=mysql_result($result,$i,‘cost’);
$stock=mysql_result($result,$i,‘noinstock’);
++$i;
}
?>

<form action=“updateanother.php” method=“POST”>
<label><b>Enter A Title:</b></label>
<input type =“text” name =“title”/></br>
<label><b>Enter The Cost:</b></label>
<input type =“text” name =“cost”/></br>
<label><b>No In Stock</b></label>
<input type =“text” name =“stock”/></br>
<button type =“submit”>
Update
</button>
<input type=“reset”>
</form>

/////////////////////////////////////////
3rd page:

<?php
include(“dbinfo.inc.php”);
$conn=@mysql_connect(localhost,$username,$password);
$rs=@mysql_select_db(“library”) or die( “Unable to select database”);
$id=$_POST[‘BookID’];
$title=$_POST[‘title’];
$cost=$_POST[‘cost’];
$noinstock=$_POST[‘noinstock’];
$sql=“UPDATE books SET title=‘$title’ ,cost=‘$cost’, noinstock=‘$noinstock’ WHERE id=‘$id’”;
mysql_query($sql);
mysql_close();
?>
<form method=“POST” action=“update.php”>
<input type=“submit” value=“Update Another Record”>
</form>

/////////////////////////////////////////

Thanks

sorry about the length but no other way to put it across lol

It doesn’t look like you are passing the id to the 3rd page. Try the following on page 2


<form action="updateanother.php" method="POST"> 
<label><b>Enter A Title:</b></label>
<input type ="text" name ="title"/><br/>
<label><b>Enter The Cost:</b></label>
<input type ="text" name ="cost"/><br/>
<label><b>No In Stock</b></label>
<input type ="text" name ="stock"/><br/>
<!--add the following line-->
<input type="hidden" name="BookID" value="<?php echo $id; ?>"/>
<button type ="submit">
Update
</button>
<input type="reset">
</form>

also the syntax for a line break is <br/> not </br>

i added
<input type=“hidden” name=“BookID” value=“<php echo $id; ?>”

still no change…then i changed it to

<input type=“hidden” name=“BookID” value=“<?php echo “$id” ?>”/>
and
<input type=“hidden” name=“BookID” value=“<?php echo $id ?>”>

and similar variations but stil nothing :confused:

any further help would be appreciated thanks

On page 2 the “No in Stock” field is named “stock” but on page 3 you are looking for $_POST[‘noinstock’] which is blank which could be causing your query to fail if the field type is int or set to not null.

Try changing

<input type ="text" name ="stock"/>

on page 2 to

<input type ="text" name ="noinstock"/>

You should also echo out your query on page 3 to make sure it is what you are expecting. You can use mysql_error to view any error that may be returned from your script as well.

i changed those mistakes thanks but stil no update :confused:

anything else would be appreciated

thanks

so where would i include the echo sorry? new to php

The echo would go right after you assign the query to the $sql variable


<?php
include("dbinfo.inc.php");
$conn=@mysql_connect(localhost,$username,$password);
$rs=@mysql_select_db("library") or die( "Unable to select database");
$id=$_POST['BookID'];
$title=$_POST['title'];
$cost=$_POST['cost'];
$noinstock=$_POST['noinstock'];
$sql="UPDATE books SET title='$title' ,cost='$cost', noinstock='$noinstock' WHERE id='$id'";

//echo $sql here
echo $sql;

mysql_query($sql);
mysql_close();
?>
<form method="POST" action="update.php">
    <input type="submit" value="Update Another Record">
</form>

I added the line but the result of it just makes a text line showing the sql query, but the record is still unchanged

thanks

The purpose of echoing the query is for debugging only. It wouldn’t fix the problem. Review the query that was displayed on the screen and make sure there are no problems with it. Is the table name correct, are the field names correct, are you getting the expected values for the fields?

Also you should make sure you have error reporting enabled so that you can see if there are any errors

You could echo out any mysql errors using the code below.


<?php
include("dbinfo.inc.php");
$conn=@mysql_connect(localhost,$username,$password);
$rs=@mysql_select_db("library") or die( "Unable to select database");
$id=$_POST['BookID'];
$title=$_POST['title'];
$cost=$_POST['cost'];
$noinstock=$_POST['noinstock'];
$sql="UPDATE books SET title='$title' ,cost='$cost', noinstock='$noinstock' WHERE id='$id'";

//echo $sql here
echo $sql;

//change this line of code to display mysql errors
if (mysql_query($sql)) {
  echo mysql_error($conn);
}

mysql_close();
?>
<form method="POST" action="update.php">
    <input type="submit" value="Update Another Record">
</form>