JaxPHP / JaxWeb Message Board › coding help

coding help

A former member
Post #: 14
Ok I have a member list that I want to update using a php form and here is what I got.

<!-- connect to database -->
<?php include("../dbcontect2.php"); ?>
<?php
// Query for a list of all existing files
$sql = "Select *
FROM table_name
ORDER BY usr_id ASC";
$result = mysql_query($sql);
if ($result <0){
print("<p style=\"font-weight: bold; color: red;\">There Are Currently No Members In The Database</p>");
}

$row_count = 1;
$query1 = mysql_query ('Select *
FROM table_name
ORDER BY usr_id ASC');



// Print each file
while ($row = @mysql_fetch_array($query1)) {
$usr_id = $row['usr_id'];
$fname = $row['fname'];
$lname = $row['lname'];
$usrname = $row['usrname'];
$email = $row['email'];
$level = $row['level'];


//display user info
echo "
<div>
First Name: $fname <br />
Last Name: $lname<br />
User Name: $usrname<br />
Email: $email<br />
Level: $level<br />
<a href=deletemember.php>$usr_id Delete</a><br />
<a href=updatemember.php>$usr_id Update</a>
<br />
<br />
</div>";

$row_count++;

}
?>

PAGE 2 updatemember.php
<!-- connect DB -->
<?php include ("../dbcontect2.php") ?>
<!-- pull data in from DB -->
<?php
$query=" SELECT * FROM table_name WHERE usr_id='$usr_id'";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();

// set value for each row

$i=0;
while ($i < $num) {
$usr_id=mysql_result($result,$i,"­usr_id");
$fname=mysql_result($result,$i,"f­name");
$lname=mysql_result($result,$i,"l­name");
$email=mysql_result($result,$i,"e­mail");
$level=mysql_result($result,$i,"l­evel");



++$i;
}
?>

<!-- echo the value to be updated -->

<form action="updatemember.php" method="post">
<input type="hidden" name="usr_id" value="<?php echo $usr_id; ?>">
First Name: <input type="text" name="fnamet" value="<?php echo $fname; ?>"><br>
Last Name: <input type="text" name="lname" value="<?php echo $lname; ?>"><br>
Phone Number: <input type="text" name="email" value="<?php echo $email; ?>"><br>
Mobile Number: <input type="text" name="level" value="<?php echo $level; ?>"><br>
<input type="Submit" value="Update">
</form>

<?php
include ("../dbcontect2.php")
// now we update the DB

$usr_id=$_POST['usr_id'];
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$email=$_POST['email'];
$level=$_POST['level'];

$query="UPDATE `djrick_paws`.`pawsusr` SET fname='$fname', lname='$lname', email='$email', level='$level' WHERE usr_id='$usr_id'";
mysql_query($query);
echo "Record Updated";
mysql_close();

?>


The errors I get are syntax line 38 on update and it is not passing usr_id from member page because when I take out everything below the form it still comes up empty but with no errors.

Thanks
Gene
A former member
Post #: 125
On first look I get on (my editor) line 87 a syntax error due to no semicolon.

include ("../dbcontect2.php");


I am still looking at it to see what else could be causing other issues.

Tim


A former member
Post #: 15
On first look I get on (my editor) line 87 a syntax error due to no semicolon.

include ("../dbcontect2.php");


I am still looking at it to see what else could be causing other issues.

Tim



Hey Tim that did get rid of the error. But it still doesn't populate the fields to update. One step at a time thanks for your time.
A former member
Post #: 126
Also no Semicolon on line 51 (wonder why editor did not catch)
<?php include ("../dbcontect2.php") ?>

Usually use on line 9
if (!$result) {

Never seen
if ($result <0){

So you may want to change this to properly catch an empty query?

Tim
A former member
Post #: 127
Spoke to soon, you use mysql_close(); right after your query on line 57.

Tim
A former member
Post #: 16
Also no Semicolon on line 51 (wonder why editor did not catch)
<?php include ("../dbcontect2.php") ?>

Usually use on line 9
if (!$result) {

Never seen
if ($result <0){

So you may want to change this to properly catch an empty query?

Tim

Hey Tim are you saying that if(!$result) { catches empty query differently than if ($result<0){ ?
<0 is just saying greater than and it may be obsolete syntax I don't know.
A former member
Post #: 128
Okay I was doing a few other things and just glanced over the code figuring minor error as most are, one note is on the PAGE 2 updatemember.php you are using usr_id before $usr_id=$_POST['usr_id']; so it is unassigned. So move assigning the input variables up to the top of updatemember.php


PAGE 2 updatemember.php

<?php
php include ("../dbcontect2.php")

$usr_id=$_POST[­'usr_id'];
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$email=$_POST['email'];
$level=$_POST['level'];

$query=" SELECT * FROM table_name WHERE usr_id='$usr_id'";­
A former member
Post #: 129
Now I have a headache! since this has actually 3 pages or the 2nd should determine if data has been edited.

if (edited == 'Y') // can add hidden input to mark edited = Y
update db
populate form with input from POST
echo 'UPDATED';
else
set your input $usr_id=$_POST['usr_id'];
select record
populate form with database info

DB table is `djrick_paws`.`pawsusr` on Update but you use table_name in all other instances.

Tim

This would be a great use for Ajax and make updating much easier!
A former member
Post #: 17
Yes, the table name is `djrick_paws`.`pawsusr` I thought I edited it out to simplify.
And Ajax maybe better solution but I know less of Ajax than I do PHP.
2nd page should determine if DB has been updated wouldn't you think? or should it pass to yet another page?

Thanks for all your help next time I am in town I'll buy you beer.

Gene
A former member
Post #: 130
I would say keep it at 2 pages and on the update page do a check for an input such as first name or the edited input

when I write code I put it on the board (white board) then section in comments in the editor

// I do everything through the index file so DB connection is set there for me

// inputs to variables
// $errormsg = 'Please Update';

// if edited then use POST for data on form
---// set var $ok to Y
---// check data is correct (such as is zip, it is 5 nmbrs) If error $ok = N and $errormsg = error
---// escape data (protect from a stray quote) with newvar name for DB data
---// if var $ok is Y
-----// Insert data into DB
//else not edited use DB for data on form
---// Select user from DB
---// set data variables

// echo $errormsg
// display form

This allows you to see the changes and edit again if not happy. That is how I would do page2

Hope this helps

Powered by mvnForum

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy