Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: [PHP] Help! Can't get function to update db  (Read 3410 times)

Fred K

  • sNews Dude
  • ULTIMATE member
  • *****
  • Karma: 134
  • Posts: 2806
[PHP] Help! Can't get function to update db
« on: August 16, 2013, 12:46:27 PM »

DISCLAIMERS: The problem is with a system I'm "building" which is based on sNews 1.7 so the following code is based on sNews stuff, hence posting the problem here in hopes that someone can see a solution. (And, you can say it, I don't really know what I'm doing...)
****

So, I'm preparing the system for a multiuser layout. To that end I've added a User Profile panel in the admin section, looks like this:
Code: [Select]
<?php // USER PROFILE SETTINGS
function user_profile() {
$id $_GET['id'];
$query "SELECT * FROM "._PRE."users WHERE id = '$id'";
$result mysql_query($query);
$num mysql_num_rows($result);
$r mysql_fetch_array($result);
echo '<div id="profiletabs">
<ul class="tabs inline-block">
<li><a href="#details">Personal Info</a></li>
<li><a href="#credchange">Username &amp; Password</a></li>
</ul>
'
;
# if($num < '1') { echo 'I can haz userzz? Pfsshh.'; } else {
echo '<form id="details" method="post" action="?action=process&amp;task=ep">
<ul id="ep">
<li><label for="rname"><span class="elusive">&#38;#57363;</span> Name:</label><input type="text" id="rname" name="realname" placeholder="Your real name" value="'
.$r['realname'].'"></li>
<li><label for="umail"><span class="elusive">&#38;#57362;</span> Email:</label><input type="email" id="umail" name="user_email" placeholder="name@address.tld" value="'
.$r['user_email'].'"></li>
<li><label for="bio"><span class="elusive">&#38;#57565;</span> Biography:</label><textarea id="bio" name="bio" placeholder="A short meme">'
.$r['user_bio'].'</textarea></li>
<li class="hidden"><input type="hidden" id="store" name="task" value="ep"></li>
<li class="hidden"><input type="hidden" id="id" name="id" value="'
.$id.'"></li>
<li class="buttons"><input type="submit" id="submit_profile" name="submit_profile" value=" '
.l('save').' "></li>
</ul>
</form>
'
;
# }
echo '...
</div>
'
;
?>


Then, in function processing(), there's this bit:
Code: [Select]
<?php case 'ep':
if(isset($_POST['submit_profile'])) {
$id $_POST['id'];
$rname $_POST['realname'];
$umail $_POST['user_email'];
$bio $_POST['user_bio'];
mysql_query("UPDATE "._PRE.'users'." SET
realname = '
$rname',
user_email = '
$umail',
user_bio = '
$bio'
WHERE id = '
$id' LIMIT 1");
echo notification(0,'','profile');
}
break; 
?>


THE PROBLEMS:
1) You can see how two parts of the first code are commented out, e.g "if($num < '1') {...} else {" and "}" this is because if those parts aren't commented out then the user profile form doesn't show up, at all. So: problem.

2) The processing part doesn't update the db table `users`, which in turn means that the whole query, and the $r['blah'] bits in the user profile form don't result in anything. Big problem.

I'm fully aware that I don't know nothing, to quote the guy in the movie, but I seriously don't get why the db doesn't get updated. I get that something's probably missing or needs rewriting some way in the first code block, but ... [throws hands in the air] ... what, I dunno.

I expect you know by now that I'm grateful for any help I can get.
Cheers.

PS. Bob, if you see this, I've sent you a PM recently. It has to do with this issue.

skian

  • Full Member
  • ***
  • Karma: 16
  • Posts: 128
Re: [PHP] Help! Can't get function to update db
« Reply #1 on: August 16, 2013, 04:48:57 PM »

First hint :

When testing :
if($num < '1')

$num is... well a number, but '1' is actually a string.

So you should write if ($num < 1)
Logged

Fred K

  • sNews Dude
  • ULTIMATE member
  • *****
  • Karma: 134
  • Posts: 2806
Re: [PHP] Help! Can't get function to update db
« Reply #2 on: August 16, 2013, 07:34:54 PM »

Hi skian. Yup, you're right of course. Of course it makes no difference: if($num < 1) or if($num <'1') gives exact same result (e.g the message that should appear if there are no users, except the users table has a user record, so it's incorrect). And it doesn't affect the db update issue.
Apart from that, you're absolutely right. ;)

nukpana

  • Hero Member
  • *****
  • Karma: 74
  • Posts: 751
Re: [PHP] Help! Can't get function to update db
« Reply #3 on: August 16, 2013, 10:34:09 PM »

Fred,

Some ideas:

a) In processing - Check the post output to make sure you are getting the key/values you are expecting.

if(isset($_POST['submit_profile'])) {
print_r($_POST);
...

b) In processing - Take the Update statement out of the mysql_query and echo it out - check it in the database to make sure there isn't an error:

$query = 'UPDATE....';
echo $query;
mysql_query($query);
Logged

nukpana

  • Hero Member
  • *****
  • Karma: 74
  • Posts: 751
Re: [PHP] Help! Can't get function to update db
« Reply #4 on: August 17, 2013, 12:39:13 AM »

Fred,

$_POST['user_bio'] is never defined as you are only calling name="bio" in the HTML. 
Logged

Fred K

  • sNews Dude
  • ULTIMATE member
  • *****
  • Karma: 134
  • Posts: 2806
Re: [PHP] Help! Can't get function to update db
« Reply #5 on: August 17, 2013, 02:01:38 AM »

Fred,

Some ideas:

a) In processing - Check the post output to make sure you are getting the key/values you are expecting.

if(isset($_POST['submit_profile'])) {
print_r($_POST);
...

b) In processing - Take the Update statement out of the mysql_query and echo it out - check it in the database to make sure there isn't an error:

$query = 'UPDATE....';
echo $query;
mysql_query($query);

Thanks Jason. As far as I can tell nothing is causing errors although the print_r result flashes by pretty quickly so it's hard to see every piece of it, but even so the db isn't updated so something's going on...

Fred,
$_POST['user_bio'] is never defined as you are only calling name="bio" in the HTML. 

Yeah, I noticed that a few minutes ago. "bio" is in the settings table while "user_bio" is in users table, so going back and forth between the two tables when testing various things out causes glitches in my brain at times. Fixed it before the latest test of the users table stuff, same result though.

Also the fact that "if($num < 1) {echo 'message'} else {...}" results in the message being echoed even though the users table actually does have one user recorded bewilders me.

Appreciate the pointers though, if nothing else it tells me to get my details right... ;)

nukpana

  • Hero Member
  • *****
  • Karma: 74
  • Posts: 751
Re: [PHP] Help! Can't get function to update db
« Reply #6 on: August 17, 2013, 03:15:48 AM »

Thanks Jason. As far as I can tell nothing is causing errors although the print_r result flashes by pretty quickly so it's hard to see every piece of it, but even so the db isn't updated so something's going on...
Comment out the notification or whatever will do the page redirect to see the output.  Same with the $query output when it should add to the DB.  Don't let it redirect.

I did a quick test of what you posted:
Code: [Select]
<?php

print_r($_POST);

if(isset($_POST['submit_profile'])) {
$id $_POST['id'];
$rname $_POST['realname'];
$umail $_POST['user_email'];
$bio $_POST['user_bio'];
$query "UPDATE users SET
realname = '
$rname',
user_email = '
$umail',
user_bio = '
$bio'
WHERE id = '
$id' LIMIT 1";
echo $query;
}

?>

<style>
li { list-style-type: none; }
.hidden { display: none; }
</style>
<div id="profiletabs">
<ul class="tabs inline-block">
<li><a href="#details">Personal Info</a></li>
<li><a href="#credchange">Username &amp; Password</a></li>
</ul>

<form id="details" method="post" action="?action=process&amp;task=ep">
<ul id="ep">
<li>
<label for="rname"><span class="elusive"></span> Name:</label>
<input type="text" id="rname" name="realname" placeholder="Your real name" value=""></li>
<li>
<label for="umail"><span class="elusive"></span> Email:</label>
<input type="email" id="umail" name="user_email" placeholder="name@address.tld" value=""></li>
<li>
<label for="bio"><span class="elusive"></span> Biography:</label>
<textarea id="bio" name="bio" placeholder="A short meme"></textarea></li>
<li class="hidden">
<input type="hidden" id="store" name="task" value="ep">
</li>
<li class="hidden">
<input type="hidden" id="id" name="id" value="1">
</li>
<li class="buttons">
<input type="submit" id="submit_profile" name="submit_profile" value="Save">
</li>
</ul>
</form>
</div>

The query it outputs is:
UPDATE user SET realname = '', user_email = '', user_bio = '' WHERE id = '1' LIMIT 1

Right away, you are putting quotes on the "id" in the WHERE clause.  If your "id" is an integer, then you don't need to quote it.
Logged

Fred K

  • sNews Dude
  • ULTIMATE member
  • *****
  • Karma: 134
  • Posts: 2806
Re: [PHP] Help! Can't get function to update db
« Reply #7 on: August 17, 2013, 11:06:21 AM »

oi... even I think not commenting out notification is too dumb for words... sorry.
have enrolled in CodeAcademy. putting this on hold for a while. thanks for the input, I'll try what you suggest and see what I can learn from it. thanks. sorry. thanks.

fwiw, seems as if id is the problem. no idea why. (Also interesting, sNews 1.7 MU does " WHERE id='$id'" as well, in processing, but that's an aside.)
« Last Edit: August 17, 2013, 11:52:18 AM by Fred K »
Logged

nukpana

  • Hero Member
  • *****
  • Karma: 74
  • Posts: 751
Re: [PHP] Help! Can't get function to update db
« Reply #8 on: August 17, 2013, 02:12:00 PM »

oi... even I think not commenting out notification is too dumb for words... sorry.
have enrolled in CodeAcademy. putting this on hold for a while. thanks for the input, I'll try what you suggest and see what I can learn from it. thanks. sorry. thanks.

fwiw, seems as if id is the problem. no idea why. (Also interesting, sNews 1.7 MU does " WHERE id='$id'" as well, in processing, but that's an aside.)
Good for you on enrolling in CodeAcademy.  IMO, do the PHP & JS.  It will help you get some basics of the languages down.  I am not sure if they go over debugging or best practices, and if they don't, it probably wouldn't of helped here.

If you get into a rut again, post a question, or try out your example in a very stripped down fashion - like I did above with just the form and print_r($_POST), then go from there.  If it helps, copy the SQL of your table and mark as Integer or String, note the submit name you are using too for your PHP, then validate your input from there.
Code: [Select]
<?php

echo '<pre>';
print_r($_POST);
echo '</pre><br>';


/* SQL table
users (
id INTEGER,
name STRING,
email STRING email,
bio STRING
)

Form submit = submitUserForm
*/

if( isset($_POST['submitUserForm']) ) {
$id  = (int) $_POST['id'];
$name  = (string) $_POST['name'];
$email  = (string) $_POST['email'];
$bio  = (string) $_POST['bio'];

$query  'UPDATE users
SET 
name = "'
$name .'",
email = "'
$email .'",
bio = "'
$bio .'"
WHERE 
id = '
$id .'
'
;
echo $query;
}

?>

<form id="userForm" method="post" action="test.php">
Name: <input type="text" name="name" required>
Email: <input type="email" name="email" required>
Bio: <textarea name="bio"></textarea>
<input type="hidden" name="id" value="1">
<input type="submit" name="submitUserForm">
</form>

fwiw, you will find stuff in the script is inconsistent. Test it and test again.
Logged