/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] HELP – Updates Don’t Display on Submit

Hey all,
The following script displays record data, and updates that data just fine,
however you have to reload the page to see the updates.
How might I alter so that the updated data returns in real time without refreshing the page?

[code=php]<?php
require_once (‘../mysql_connect.php’);
mysql_select_db (‘bike_shop’);

$rows_per_page = 1;
$sql = “SELECT * FROM catalog”;
$result = mysql_query($sql);
$total_records = mysql_num_rows($result);
$pages = ceil($total_records / $rows_per_page);
mysql_free_result($result);

$screen = $_GET[‘screen’];
if (!isset($screen)) $screen = 0;
$start = $screen * $rows_per_page;
$sql = “SELECT id,description FROM catalog “;
$sql .= “LIMIT $start, $rows_per_page”;
$result = mysql_query($sql);
$rows = mysql_num_rows($result);
for ($i = 0; $i < $rows; $i++) {
$record=mysql_result($result,$i,”id”);
$descripton=mysql_result($result,$i,”Description”);
$disp_count=($screen+1);
echo ”
<form action=” method=’post’>
<div align=’center’>
<table width=’57%’ bordercolor=’#C0C0C0′>
<tr>
<td width=’14%’ align=’right’><b>Description:</b></td>
<td colspan=’2′><textarea name=’description’ rows=’5′ cols=’60’>$descripton</textarea></td>
</tr>
<tr>
<td width=’14%’ align=’right’ valign=’top’></td>
<td align=’center’><input type=’submit’ value=’Submit’ name=’submit’></td>
</tr>

</table></div>
</form>”;
if (isset($_POST[‘submit’])) { // Handle the form.
$Description = $_POST[‘description’];
$update=” UPDATE catalog SET Description=’$Description’ WHERE id = ‘$record’ “;
$result=mysql_query($update) or die (“Query failed – updating Products”); // Run the query. ;
}
}
echo “<p align=’center’><table ><tr><td>n”;
if($screen > 0)
{
$prev = $screen-1;
$prevlink = “<a href=””.$url.”?screen=$prev”>Previous</a> || “;
}
else
{
$prevlink = “Previous || “;
}
echo $prevlink;
if($screen < $pages-1)
{
$next = $screen+1;
$nextlink = “<a href=””.$url.”?screen=$next”>Next</a>”;
}
else
{
$nextlink = “Next”;
}
echo $nextlink .”<br>”;
echo “</td></tr></table></p>”;
?> [/code]

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@NightShift58Jan 16.2007 — The code you've posted is sort of... well, a bit of a logical mess. Why? Let's see...

You're using one PHP page to handle both data input through a <form> and the processing of that data. That's fine, it can be done. However, you have to remember that it's still a two-step process.

First, PHP will display the forms for data entry and once these have been submitted (i.e. clicking on submit), the same page is called again - but this time, your code has to do an intercept and update the data in the database table and then display the updated data in forms agains for another round of updates.

Also, your code as it is now will only work as long as the variable "$rows_per_page" is equal to 1. If you increase that to as little as 2, you will run into a problem with the input variable name because they are identically named in your forms. We need to use arrays to keep them apart.

The following is your code rebuilt/restructured to match your processing goals. I can't test it, so use it on test data first.[code=php]<?php
require_once ('../mysql_connect.php');
mysql_select_db ('bike_shop');

$rows_per_page = 1;

// If we're coming from a form submission, processed the submitted information first
IF (isset($_POST['submit'])) :
$arrDesc = $_POST['desc'];
FOREACH ($arrDesc as $thisID => $thisDesc) :
$thisDesc = mysql_real_escape_string($thisDesc);
$update = " UPDATE catalog ";
$update .= " SET Description='$thisDesc' ";
$update .= " WHERE id='$thisID' "; // update only on matching ID
$update .= " AND Description <> '$thisDesc' "; // only update if description was changed
$update .= " LIMIT 1"; // only 1 record will match, no need to look for more
$result=mysql_query($update) or die("Query failed - updating Products: $update<br>" . mysql_error());
ENDFOREACH;
ENDIF;

$sql = "SELECT count(*) as totalrecs FROM catalog";
$result = mysql_query($sql) or die("SQL Error: $sql<br>" . mysql_error());
$row = mysql_fetch_array($result);
$pages = ceil($row['totalrecs'] / $rows_per_page);
$screen = (isset($_GET['screen']) AND $_GET['screen'] <> "") ? $_GET['screen'] : 0;
$start = $screen * $rows_per_page;
$sql = "SELECT id, description FROM catalog ";
$sql .= "LIMIT $start, $rows_per_page";
$result = mysql_query($sql) or die("SQL Error: $sql<br>" . mysql_error());

echo "<form action='" . $_SERVER['PHP_SELF'] . "?screen=$screen method='post'>";
echo "<div align='center'>";
echo "<table width='57%' bordercolor='#C0C0C0'>";
WHILE ($row = mysql_fetch_array($result)) :
echo "<tr>";
echo "<td width='14%' align='right'><b>Description:</b></td>";
echo "<td colspan='2'><textarea name='desc[$row['id']' rows='5' cols='60'>{$row['Description']}</textarea></td>";
echo "</tr>";
ENDWHILE;
echo "<tr>";
echo "<td width='14%' align='right' valign='top'></td>";
echo "<td align='center'><input type='submit' value='Submit changes' name='submit'></td>";
echo "</tr>";
echo "</table></div>";
echo "</form>";

echo "<p align='center'><table><tr><td>n";
$prevlink = "Previous || ";
$nextlink = "Next";
IF ($screen > 0) :
$prevlink = "<a href='" . $_SERVER['PHP_SELF'] . "?screen=" . ($screen-1) . "'>Previous</a> || ";
ENDIF;
IF ($screen < $pages-1) :
$nextlink = "<a href='" . $_SERVER['PHP_SELF'] . "?screen=" . ($screen+1) . "'>Next</a> || ";
ENDIF;
echo $prevlink . $nextlink . "<br>";
echo "</td></tr></table></p>";
?>[/code]
Copy linkTweet thisAlerts:
@tjmcdauthorJan 16.2007 — The code you've posted is sort of... well, a bit of a logical mess.[/QUOTE] LOL! That pretty well sums me up!

Anyway, WOW have you been BUSY since your December Join date!

I sure do appreciate all the effort and consideration you've put into my little mess here!

As for increasing [COLOR="Red"]$rows_per_page > 1[/COLOR], I can't see any event wherein I would want to increase it,

however testing your update of my illogical code just the same, throws:Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:AreaProwampwwwMySyncwalb_prod_validationtest_updates.php on line 37[/QUOTE]
Trying to wrap my tiny brain around your logic I'm at a bit of a loss trying to sort it out.

Any thoughts?
Copy linkTweet thisAlerts:
@NightShift58Jan 16.2007 — Sorry, my mistake... Line 37 should be like this:[code=php] echo "<td colspan='2'><textarea name='{desc[$row['id']}' rows='5' cols='60'>{$row['Description']}</textarea></td>";[/code]
Copy linkTweet thisAlerts:
@tjmcdauthorJan 16.2007 — Sorry, my mistake... Line 37 should be like this:[code=php] echo "<td colspan='2'><textarea name='{desc[$row['id']}' rows='5' cols='60'>{$row['Description']}</textarea></td>";[/code][/QUOTE]
Well, that inches things forward a bit. At least now the page loads,

but update or next returns the same line 37 parse error. I hate leaning so hard on the forums (and your generosity) but, racking my brain to understand all the code prior to line 37, I'm not even close to being able to debug it yet.
Copy linkTweet thisAlerts:
@tjmcdauthorJan 16.2007 — Okay My bad, actually, no it doesn't load with updated line37, apparently was just reading my_script from cache?

Anyway changed [COLOR="DarkRed"]{desc[$row['id']}[/COLOR] to [COLOR="darkred"]{$row['id']}[/COLOR] in line 37, which now allows page to load, albeit with the following error/warning

Warning: Invalid argument supplied for foreach() in C:AreaProwampwwwMySyncwalb_prod_validationtest_updates.php on line 10[/QUOTE]
however no data is returned. Still picking at it but beginning to think NOT POSSIBLE.
Copy linkTweet thisAlerts:
@NightShift58Jan 17.2007 — Mistake over mistake and not reading the code as carefully as I should.

Line 37 should read:[code=php] echo "<td colspan='2'><textarea name='desc[{$row['id']}]' rows='5' cols='60'>{$row['Description']}</textarea></td>";[/code]We're interpolating $row['id'] and that's the part that needs to go in braces. The above is the equivalent of:[code=php] echo "<td colspan='2'><textarea name='desc[" . $row['id'] . "]' rows='5' cols='60'>" . $row['Description'] . "</textarea></td>";
[/code]
which you may also use if you're more comfortable with that direction.
Copy linkTweet thisAlerts:
@tjmcdauthorJan 17.2007 — Hey Nighshift,

I really do appreciate the effort on your part. Truly. Unfortunately, while I finally did manage to get the data to load, I have yet to effect a successful submission (nor with either of the updated versions of line 37 offered in your last post. ) Lest I chew up anymore of your valuable time and considerable generosity on this bit of silliness (who knows I might need assistance on something a bit more critical) I have opted for a work around wherein I have simply included the following at the end of the submit block in my original script. [code=php]echo "<div style='border:5px solid #808080; position: absolute; width: 650px; height: 231px; z-index: 99; left: 250px; top: 66px; padding-left:4px; padding-right:4px; padding-top:1px; padding-bottom:1px; background-color:#E6E6E6' id='layer1'>
<p align='center'><br>
Update Completed Successfully.<br>
<br>
<input type='button' value='Edit More' onClick='history.go(-1)'></p></div>";[/code]
Now, on submit the confirmation div pops-up over the form (obscuring the appearance that the data did not update) with a button to reload the form for further edits.

Ugly, yes.... but it works.

Again, I really really do appreciate the effort.

Thank You.? ? ?
Copy linkTweet thisAlerts:
@NightShift58Jan 17.2007 — I looked over the code once again and found this discrepancy. The field name "Description" is used in two separate SQL statements: once with an uppercased "D" and once with a lowercased "d".

As field names as case sensitive, at least one of the SQL statements will not work. Please find out what the proper spelling/capitalization is for that field.

If the field name is spelled with a capital "D", please change this line:[code=php]$sql = "SELECT id, description FROM catalog ";[/code]to:[code=php]$sql = "SELECT id, Description FROM catalog ";[/code]

If, however, the field name is spelled with a lowercased "d", then please change these lines instead, from:[code=php]$update .= " SET Description='$thisDesc' ";
$update .= " WHERE id='$thisID' "; // update only on matching ID
$update .= " AND Description <> '$thisDesc' "; // only update if description was changed[/code]
to:[code=php]$update .= " SET description='$thisDesc' ";
$update .= " WHERE id='$thisID' "; // update only on matching ID
$update .= " AND description <> '$thisDesc' "; // only update if description was changed[/code]
and this one as well, from :[code=php] echo "<td colspan='2'><textarea name='desc[$row['id']' rows='5' cols='60'>{$row['Description']}</textarea></td>";[/code]to:[code=php] echo "<td colspan='2'><textarea name='desc[$row['id']' rows='5' cols='60'>{$row['description']}</textarea></td>";[/code]
Copy linkTweet thisAlerts:
@tjmcdauthorJan 17.2007 — Thanks Again Nightshift,

The field is in fact spelled with an UPPER CASE D (not my choosing I'm afraid - dovetails with ebay/kurant ProStores). Sadly, I had already caught and corrected that error much earlier in the day, but after much trial and error I forgot to mention it here. Still, No Joy.?

No Worries though!
Copy linkTweet thisAlerts:
@NightShift58Jan 17.2007 — I found another mistake in the code. I had sworn to give up but it was eating away at me... Somewhere in the <form> statement was a missing single quote. I uploaded this to my server (http://www.nightshift58.com/webdev/test_catalog.php) and it runs in the version that follows:[code=php]<?php
require_once ('../mysql_connect.php');
mysql_select_db ('bike_shop');

$rows_per_page = 1;

// If we're coming from a form submission, processed the submitted information first
IF (isset($_POST['submit'])) :
$arrDesc = $_POST['desc'];
FOREACH ($arrDesc as $thisID => $thisDesc) :
$thisDesc = mysql_real_escape_string($thisDesc);
$update = " UPDATE catalog ";
$update .= " SET Description='$thisDesc' ";
$update .= " WHERE id='$thisID' "; // update only on matching ID
$update .= " AND Description <> '$thisDesc' "; // only update if description was changed
$update .= " LIMIT 1"; // only 1 record will match, no need to look for more
$result=mysql_query($update) or die("Query failed - updating Products: $update<br>" . mysql_error());
ENDFOREACH;
ENDIF;

$sql = "SELECT count(*) as totalrecs FROM catalog";
$result = mysql_query($sql) or die("SQL Error: $sql<br>" . mysql_error());
$row = mysql_fetch_array($result);
$pages = ceil($row['totalrecs'] / $rows_per_page);
$screen = (isset($_GET['screen']) AND $_GET['screen'] <> "") ? $_GET['screen'] : 0;
$start = $screen * $rows_per_page;
$sql = "SELECT id, Description FROM catalog ";
$sql .= "LIMIT $start, $rows_per_page";
$result = mysql_query($sql) or die("SQL Error: $sql<br>" . mysql_error());

echo "<form action='" . $_SERVER['PHP_SELF'] . "?screen=$screen' method='post'>";
echo "<div align='center'>";
echo "<table width='57%' bordercolor='#C0C0C0'>";
WHILE ($row = mysql_fetch_array($result)) :
echo "<tr>";
echo "<td width='14%' align='right'><b>Description:</b></td>";
echo "<td colspan='2'><textarea name='desc[{$row['id']}]' rows='5' cols='60'>{$row['Description']}</textarea></td>";
echo "</tr>";
ENDWHILE;
echo "<tr>";
echo "<td width='14%' align='right' valign='top'></td>";
echo "<td align='center'><input type='submit' value='Submit changes' name='submit'></td>";
echo "</tr>";
echo "</table></div>";
echo "</form>";

echo "<p align='center'><table><tr><td>n";
$prevlink = "Previous || ";
$nextlink = "Next";
IF ($screen > 0) :
$prevlink = "<a href='" . $_SERVER['PHP_SELF'] . "?screen=" . ($screen-1) . "'>Previous</a> || ";
ENDIF;
IF ($screen < $pages-1) :
$nextlink = "<a href='" . $_SERVER['PHP_SELF'] . "?screen=" . ($screen+1) . "'>Next</a> || ";
ENDIF;
echo $prevlink . $nextlink . "<br>";
echo "</td></tr></table></p>";
?> [/code]
Copy linkTweet thisAlerts:
@tjmcdauthorJan 17.2007 — I had sworn to give up but it was eating away at me... [/QUOTE]
LOL! Sometimes you've just GOT TO HAVE CLOSURE!

Anyway, I tried it again, and sure enough it works! I opted to comment out the [COLOR="Red"]AND Description <> '$thisDesc'[/COLOR] condition in the update query as it prevents updates so mundane as "training wheels" to "Training Wheels", otherwise it's a thing of beauty!

Thanks Again! I owe you!
Copy linkTweet thisAlerts:
@NightShift58Jan 17.2007 — You're welcome...
×

Success!

Help @tjmcd spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...