RE: [PHP-DB] Undefined variables in update query
Many thanks for this great advice! Definitely worth keeping and putting into practice! Any way to write cleaner and more concise code is always worth learning. Best regards, Robert -- Robert J. Vander Hart Electronic Resources Librarian | Lamar Soutter Library University of Massachusetts Medical School 01655 508-856-3290 | robert.vanderh...@umassmed.edu http://library.umassmed.edu | LinkedIn: http://www.linkedin.com/in/robertvanderhart -Original Message- From: tamouse mailing lists [mailto:tamouse.li...@gmail.com] Sent: Tuesday, March 26, 2013 6:57 PM To: VanderHart, Robert Cc: php-db@lists.php.net Subject: Re: [PHP-DB] Undefined variables in update query On Tue, Mar 26, 2013 at 10:57 AM, VanderHart, Robert wrote: > I appreciate the replies I've received already; thanks! Sorry for not > catching my simple errors before sending out that message. No worries, no one I have met yet begins life knowing this stuff! If you don't mind, Robert, I'd like to introduce you to a couple of concepts that might make your way easier: 1) Don't Repeat Yourself, aka DRY -- in writing code, it sometimes seems easier to just cut and paste something over and over again with a couple of minor tweaks. However, if you find you have to change something structurally (like that form!), having the change in one place is so much simpler. 2) Think in functions, procedures, and modules. It's sometimes very easy when starting out to just write everything in one straight, linear flow. Going along with #1, using functions to handle the reduced repetition should help a lot. If I can give a simple example, you have a whole swath of lines dealing with pulling things out of $_POST that look almost exactly the same, save for the key. $fields = ['authorid','lname','fname','mname','email','institution', 'department','comments','sent_email','suffix'] foreach ($fields as $field) { if (isset($_POST[$field])) $$field = mysqli_real_escape_string($link, $_POST[$field]); } If you notice the '$$field' thing in the middle there, it's a Variable Variable, which is described in: http://www.php.net/manual/en/language.variables.variable.php It might look advanced, but most every language has some form of indirection like that. Makes the above operation really nice and short. You said you've been around the block a couple of times in other languages and scripts (and if your photo is accurate, we're probably of an age!) so I hope you'll take this as just trying to help a person get a leg up.
Re: [PHP-DB] Undefined variables in update query
On Tue, Mar 26, 2013 at 10:57 AM, VanderHart, Robert wrote: > I appreciate the replies I've received already; thanks! Sorry for not > catching my simple errors before sending out that message. No worries, no one I have met yet begins life knowing this stuff! If you don't mind, Robert, I'd like to introduce you to a couple of concepts that might make your way easier: 1) Don't Repeat Yourself, aka DRY -- in writing code, it sometimes seems easier to just cut and paste something over and over again with a couple of minor tweaks. However, if you find you have to change something structurally (like that form!), having the change in one place is so much simpler. 2) Think in functions, procedures, and modules. It's sometimes very easy when starting out to just write everything in one straight, linear flow. Going along with #1, using functions to handle the reduced repetition should help a lot. If I can give a simple example, you have a whole swath of lines dealing with pulling things out of $_POST that look almost exactly the same, save for the key. $fields = ['authorid','lname','fname','mname','email','institution', 'department','comments','sent_email','suffix'] foreach ($fields as $field) { if (isset($_POST[$field])) $$field = mysqli_real_escape_string($link, $_POST[$field]); } If you notice the '$$field' thing in the middle there, it's a Variable Variable, which is described in: http://www.php.net/manual/en/language.variables.variable.php It might look advanced, but most every language has some form of indirection like that. Makes the above operation really nice and short. You said you've been around the block a couple of times in other languages and scripts (and if your photo is accurate, we're probably of an age!) so I hope you'll take this as just trying to help a person get a leg up. -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DB] Undefined variables in update query
Premek, Thanks for the reply. I actually found that silly mistake after I sent out my message. You're absolutely right that I need to hardcode the field name into the input name attribute. Even a newbie like me should have realized that one; even though I'm new to PHP I've used other scripting languages for quite a while. I appreciate the replies I've received already; thanks! Sorry for not catching my simple errors before sending out that message. Best to all, Robert -- Robert J. Vander Hart University of Massachusetts Medical School 508-856-3290 | robert.vanderh...@umassmed.edu -Original Message- From: Přemysl [mailto:premysl.fi...@lim.cz] Sent: Tuesday, March 26, 2013 11:46 AM To: php-db@lists.php.net Subject: Re: [PHP-DB] Undefined variables in update query Hi, it appears to me that you have some strange input[name] attribute. instead of ... try ... In php you are not initializing variables. That means $department is only set if $_POST['department'] exists. You can try this: add: $sql = ''; instead of if (isset($_POST['department'])) { $department = mysqli_real_escape_string($link, $_POST['department']); } try if (isset($_POST['department'])) { $sql .= $sql ? ", department='$_POST[department]'" : " department='$_POST[department]"; } .. database: $sql = "update intr_stats_eschol set $sql where authorid='$authorid'"; If you want to update all the rows in you table you can use (for example): ... name="department[$row[authorId]" and in php: $department=$_POST['department'][$authorId]... Premek. On Tue, 26 Mar 2013 16:19:24 +0100, VanderHart, Robert wrote: > Hi, > > I'm pretty new to PHP and to this discussion list. > > I have a web form to update some fields in a data table. I'm getting > "undefined variable" notices in my error logs when I submit the form > and the table row doesn't get updated. > > Here's the code for the form page: > > > include '../db.inc.php'; > > $ID = mysqli_real_escape_string($link, $_GET['ID']); $result = > mysqli_query($link, "select * from intr_stats_eschol where > authorid='$ID'"); if (!$result) { > $error = 'Error fetching eScholarship author details: ' . > mysqli_error($link); > include 'error.html.php'; > exit(); > } > > $num=mysqli_num_rows($result); > > echo "Editing Record action=\"eschol-edit-process.php\"> "; > > for ($i=0; $i<$num; $i++) { > $row = mysqli_fetch_assoc ($result); > > echo > "Last Name: value=\"$row[lname]\"> > First Name: value=\"$row[fname]\"> > Middle Name: value=\"$row[mname]\"> > Suffix: value=\"$row[suffix]\"> > Email: value=\"$row[email]\"> > Institution: name=\"$row[institution]\" value=\"$row[institution]\"> > Department: name=\"$row[department]\" value=\"$row[department]\"> > Comments: value=\"$row[comments]\"> > Send Email?$row[sent_email] > value=\"$row[authorid]\"> Author\"> "; > > } echo " > "; > > ?> > > The data are inserted correctly into the form input fields, but when I > click the "Update Author" button, an "undefined variable" notice gets > logged for each of the variables and the table row doesn't get updated. > The only variable that seems to be OK is the authorid field; it > outputs the correct ID from the var_dump() I have inserted. > > Here's the code that processes the form: > > > include '../db.inc.php'; > > if (isset($_POST['authorid'])) > { > $authorid = mysqli_real_escape_string($link, $_POST['authorid']); > var_dump($authorid); } if (isset($_POST['lname'])) { $lname = > mysqli_real_escape_string($link, $_POST['lname']); } if > (isset($_POST['fname'])) { $fname = mysqli_real_escape_string($link, > $_POST['fname']); } if (isset($_POST['mname'])) { $mname = > mysqli_real_escape_string($link, $_POST['mname']); } if > (isset($_POST['email'])) { $email = mysqli_real_escape_string($link, > $_POST['email']); } if (isset($_POST['institution'])) { $institution = > mysqli_real_escape_string($link, $_POST['institution']); } if > (isset($_POST['department'])) { $department = > mysqli_real_escape_string($link, $_POST['dep
Re: [PHP-DB] Undefined variables in update query
Hi, it appears to me that you have some strange input[name] attribute. instead of value=\"$row[department]\">... try ... In php you are not initializing variables. That means $department is only set if $_POST['department'] exists. You can try this: add: $sql = ''; instead of if (isset($_POST['department'])) { $department = mysqli_real_escape_string($link, $_POST['department']); } try if (isset($_POST['department'])) { $sql .= $sql ? ", department='$_POST[department]'" : " department='$_POST[department]"; } .. database: $sql = "update intr_stats_eschol set $sql where authorid='$authorid'"; If you want to update all the rows in you table you can use (for example): ... name="department[$row[authorId]" and in php: $department=$_POST['department'][$authorId]... Premek. On Tue, 26 Mar 2013 16:19:24 +0100, VanderHart, Robert wrote: Hi, I'm pretty new to PHP and to this discussion list. I have a web form to update some fields in a data table. I'm getting "undefined variable" notices in my error logs when I submit the form and the table row doesn't get updated. Here's the code for the form page: $error = 'Error fetching eScholarship author details: ' . mysqli_error($link); include 'error.html.php'; exit(); } $num=mysqli_num_rows($result); echo "Editing Record "; for ($i=0; $i<$num; $i++) { $row = mysqli_fetch_assoc ($result); echo "Last Name:value=\"$row[lname]\"> First Name:value=\"$row[fname]\"> Middle Name:value=\"$row[mname]\"> Suffix:value=\"$row[suffix]\"> Email:value=\"$row[email]\"> Institution:name=\"$row[institution]\" value=\"$row[institution]\"> Department:name=\"$row[department]\" value=\"$row[department]\"> Comments:value=\"$row[comments]\"> Send Email?$row[sent_email] value=\"$row[authorid]\">Author\"> "; } echo " "; ?> The data are inserted correctly into the form input fields, but when I click the "Update Author" button, an "undefined variable" notice gets logged for each of the variables and the table row doesn't get updated. The only variable that seems to be OK is the authorid field; it outputs the correct ID from the var_dump() I have inserted. Here's the code that processes the form: $error = 'Error fetching eScholarship author details: ' . mysqli_error($link); include 'error.html.php'; exit(); } ?> Can anyone tell where I'm going wrong? I thought it might be some obvious thing like the use of single quotes instead of double quotes in the update query, or vice-versa, but I've tried several different ways. I've looked at numerous stackoverflow.com postings but nothing seems to remedy the issue I'm having. I'm also wondering if the problem is related to my using echo() to output the form fields in the first template. FWIW, here's the design of my table: +-+--+--+-+-++ | Field | Type | Null | Key | Default | Extra | +-+--+--+-+-++ | email | varchar(255) | YES | | NULL | | | institution | varchar(200) | YES | | NULL | | | lname| varchar(100) | YES | | NULL | | | fname | varchar(80)| YES | | NULL || | mname | varchar(20) | YES | | NULL | | | department | varchar(200) | YES | | NULL | | | comments| varchar(255) | YES | | NULL | | | authorid| int(5) | NO | PRI | NULL| auto_increment | | sent_email | varchar(20) | YES | | NULL | | | suffix | varchar(50)| YES || NULL | | +-+--+--+-+-++ Thanks for any help you can give me! -- Robert J. Vander Hart University of Massachusetts Medical School 508-856-3290 | robert.vanderh...@umassmed.edu -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php