Jump to content

PHP database updates display


tet3828

Recommended Posts

The porpose of the script below is to display and edit data from a mysql table. the row that is being edited is dependant upon the varible passed by the previous page.

well the script DOES view the item and DOES change column of the row I want to edit.

BUT the column I want to edit shows itself as a blank field until I enter data.
In other words the script updates a null (or empty) value and overwrites the current data b4 the user gets to see what the exiting data was.. ???

I think my if statement is being carried out before the form data is entered.. why is that? and how can I avoid that?

<?php
/// if submit button is pressed
if (!isset($_POST['submit']))
{
$id = $_GET['id'];
$newId =  $_POST['newId'];
$query = "UPDATE products SET itemName='$newId' WHERE `itemId`=\"$id\"";
$result = mysql_query($query);

if ($result)  echo "<p>Update done.</p>";
else echo "<p>yeah that sux, it didn't work</p>";
}
?>
<?php



/// Store Passed Data as $id
$id = $_GET['id'];

/// Select data from  mySQL table
$qry = "SELECT itemName,itemCat,itemSub,itemId,itemPrice,itemDesc,itemSmall FROM `products` WHERE `itemId`=\"$id\"";
$result = mysql_query($qry) or die(mysql_error());

///  page header
echo "Listed below is the stored data for the item #$id <br />
Use the fields below the item table to modify the information.";


/// Display item data loop
while($row = mysql_fetch_array($result))
{


echo "<table border=\"1\"><tr><td>".$row['itemName']."<td>".$row['itemId']."</td></td></tr>";
echo "<tr><td><img src=\"".$row['itemSmall']."\" /></td><td valign=top width=150>Description:
".$row['itemDesc']."</td>";
echo "</tr><tr><td>Price: $".$row['itemPrice']."</td><td>Avilibility: </td></tr>";
echo "<tr><td width=150>Catagory: ".$row['itemCat']."</td><td width=150>Sub-Catagory:".$row['itemSub']."</td></tr>";
}
echo "</table>";
echo "
";
?>

<form method="post" action="<?php echo $PHP_SELF;?>">
<p>
Item Name:

</p>
<input type="text" name="newId" size="10" maxlength="10" value="" />

</form>

</body>
Link to comment
https://forums.phpfreaks.com/topic/25850-php-database-updates-display/
Share on other sites

also if you would like to pass the values and fill in the item box try this
[code]<?php
/// if submit button is pressed
if (isset($_POST['submit']))
{
$id = $_POST['id'];
$newId =  $_POST['newId'];
$query = "UPDATE products SET itemName='$newId' WHERE `itemId`=\"$id\"";
$result = mysql_query($query);

if ($result)  echo "<p>Update done.</p>";
else echo "<p>yeah that sux, it didn't work</p>";
} else {
/// Store Passed Data as $id
$id = $_GET['id'];

/// Select data from  mySQL table
$qry = "SELECT itemName,itemCat,itemSub,itemId,itemPrice,itemDesc,itemSmall FROM `products` WHERE `itemId`=\"$id\"";
$result = mysql_query($qry) or die(mysql_error());

///  page header
echo "Listed below is the stored data for the item #$id

Use the fields below the item table to modify the information.";


/// Display item data loop
while($row = mysql_fetch_array($result))
{


echo "<table border=\"1\"><tr><td>".$row['itemName']."<td>".$row['itemId']."</td></td></tr>";
echo "<tr><td><img src=\"".$row['itemSmall']."\" /></td><td valign=top width=150>Description:
".$row['itemDesc']."</td>";
echo "</tr><tr><td>Price: $".$row['itemPrice']."</td><td>Avilibility: </td></tr>";
echo "<tr><td width=150>Catagory: ".$row['itemCat']."</td><td width=150>Sub-Catagory:".$row['itemSub']."</td></tr>";
}
echo "</table>";
echo "
";
?>

<form method="post" action="<?php echo $PHP_SELF;?>">
<p>
Item Name:

</p>
<input type=hidden name=id value="<?=$_GET['id']?>">
<input type="text" name="newId" size="10" maxlength="10" value="<?=$row['itemName']?>" /><br />
<input type=submit name=submit value=Submit />

</form>

</body>
<?php
}
?>[/code]

Ray
ray and alpine... thank you. between your responses I was able to get the exact results I needed however...

I don't understand some of the basics could someone explain the functions of thes line in detail

<input type=hidden name=id value="<?=$_GET['id']?>">


<input type="text" name="newId" size="10" maxlength="10" value="<?=$row['itemName']?>" />


<input type=submit name=submit value=Submit />
[color=blue]<input type=hidden name=id value="<?=$_GET['id']?>">[/color]

Takes the current id value (?id=1) from your adress-bar and puts it "invisible" in your form to post on as "id"


[color=blue]<input type="text" name="newId" size="10" maxlength="10" value="<?=$row['itemName']?>" />[/color]

Takes the mysql query result row "itemName" value to post on as "newId" from your form


[color=blue]<input type=submit name=submit value=Submit />[/color]

Is your form submit button
So I began adding more forms to my edit item script. naturally I need my users to have the option to change any aspects of an item in my product database. Everything works fine whichever fields I add the data is modified and displayed. PERFECT

Now my question is.... [b]what if the user only wants to change the itemName field and not itemDesc? he/she will leave the field they do not want to edit blank.[/b] [b]as of now my script doesn't have the potential to ignore the processing of a empty field.

What to I need to add to my script to make this happen?[/b]
I am guessing some something like

IF <the form> = NULL
THEN <new value> = <existing value>

here is the revised script: (thanks to all phpfreak contributers)

<?php
/// if submit button is pressed
if (isset($_POST['submit']))
{
$id = $_GET['id'];
$newName =  $_POST['newName'];
$newDesc = $_POST['newDesc'];
$query = "UPDATE products SET itemDesc='$newDesc', itemName='$newName' WHERE `itemId`=\"$id\"";
$result = mysql_query($query);

if ($result)  echo "<p>Update done.</p>";
else echo "<p>yeah that sux, shit didn't work</p>";
}
?>
<?php



/// Store Passed Data as $id
$id = $_GET['id'];

/// Select data from  mySQL table
$qry = "SELECT itemName,itemCat,itemSub,itemId,itemPrice,itemDesc,itemSmall FROM `products` WHERE `itemId`=\"$id\"";
$result = mysql_query($qry) or die(mysql_error());

///  page header
echo "Listed below is the stored data for the item #$id <br />
Use the fields below the item table to modify the information.";


/// Display item data loop
while($row = mysql_fetch_array($result))
{


echo "<table border=\"1\"><tr><td>".$row['itemName']."<td>Item ID#".$row['itemId']."</td></td></tr>";
echo "<tr><td><img src=\"".$row['itemSmall']."\" /></td><td valign=top width=150>Description:
".$row['itemDesc']."</td>";
echo "</tr><tr><td>Price: $".$row['itemPrice']."</td><td>Avilibility: </td></tr>";
echo "<tr><td width=150>Catagory: ".$row['itemCat']."</td><td width=150>Sub-Catagory:".$row['itemSub']."</td></tr>";
}
echo "</table>";
echo "
";
?>

<form method="post" action="<?php echo $PHP_SELF;?>">
<p>
Item Name:

</p>
<input type=hidden name=id value="<?=$_GET['id']?>">
<input type="text" name="newName" size="10" maxlength="10" value="<?=$row['itemName']?>" /><br />
<textarea name="newDesc" rows="3" cols="25" > <?=$row['itemDesc']?></textarea> <br />
<input type=submit name=submit value=Submit />

</form>
I would assume that the user will leave the pre-filled formfield alone "as is" rather than to blank it out.
I suggest something like this to check that all fields contain data before allowing update:
[code]
<?php

if(!empty($_POST['field_1']) || !empty($_POST['field_2']))
{
// ok
}
else
{
// echo empty field message to the user and show the update-form with the last posted values
}

?>
[/code]

You should also filter the user input to prevent sql injection, you can get the basic idea here: http://en.wikibooks.org/wiki/Programming:PHP:SQL_Injection
I don't think my question was intruperted properly. Must be due to my lack of explanation.

In my script an HTML script displays the current information of an SQL row.
item name, price, description ect...

the forms below that table give the user the option to modify the existing data

say I have 4 forms and a submit button.... *name *price *description and *idnumber. but if the user only wants to modify the *name, he/she would leave *price *descripton *idnumber blank.
As of now my script stores the empty field into the table ereasing the existing data.
[b]is there a way to process the forms that are filed out and ignore the null fields?  [/b]
I apologise if this is just a repeat of my last question. If I get no response ill assume I can use the suggestion from the prior script to do what I need. THanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.