updating mySQL table from form - stopped working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
iG9
Forum Commoner
Posts: 38
Joined: Fri Jul 18, 2008 2:11 pm

updating mySQL table from form - stopped working

Post by iG9 »

Hi. I've been learning PHP for a couple months. I made a form to let me add books from my library into a mySQL table. It worked for a while. =) Then I made another page to let me update what page I was on, and when I got that working the book entry form stopped. I don't remember making any changes to it, but then I suppose anything's possible. What happens is I get the "Book successfully added." output, but it doesn't actually add anything. Here's the page that does the processing for the book entry form:

Code: Select all

 
<?php
 
#-----------------------#
# Functions             #
#-----------------------#
 
function check_input ($form_array) {
    if ($form_array['isbn'] && $form_array['title'] && $form_array['author']) {
            return 1;
        }
    else return 0;
}
 
#-----------------------#
# User Variables        #
#-----------------------#
 
$host = "********";
$user = "********";
$pw = ********";
$database = "********";
$table_name = "********";
$pin = "****";
 
#-----------------------#
# Main Body             #
#-----------------------#
 
if ($_POST['pin']==$pin){
    if (check_input($_POST)) {
    
        $db = mysql_connect($host, $user, $pw)
            or die("Cannot connect to mySQL.");
        mysql_select_db($database, $db)
            or die("Cannot connect to database.");
 
        $add_book = "insert into $table_name values('".addslashes($_POST['isbn'])."','".addslashes($_POST['title'])."','".addslashes($_POST['author'])."','".addslashes($_POST['genre'])."','".addslashes($_POST['page_on'])."','".addslashes($_POST['review'])."','".addslashes($_POST['where_got'])."');";
 
        mysql_query($add_book);
    
        print "Book successfully added.<br />";
        mysql_close($db);
    }
else { print "ISBN, Title & Author are all mandatory. Please try again.<br />"; }
}
else { print "Wrong PIN, bucko...<br />";}
 
?>
 
 
 
 
And, just for kicks, here's the page updater:

Code: Select all

 
<?php
 
#-----------------------#
# Functions             #
#-----------------------#
 
function check_input ($form_array) {
    if ($form_array['title'] && $form_array['page']) {
            return 1;
        }
    else return 0;
}
 
#-----------------------#
# User Variables        #
#-----------------------#
 
$host = "********";
$user = "********";
$pw = "********";
$database = "********";
$table_name = "********";
 
#-----------------------#
# Main Body             #
#-----------------------#
 
if (check_input($_POST)) {
    
    $db = mysql_connect($host, $user, $pw)
        or die("Cannot connect to mySQL.");
    mysql_select_db($database, $db)
        or die("Cannot connect to database.");
 
    $update_pages = "update $table_name set page_on = '$_POST[page]' where title like('$_POST[title]%');";
    mysql_query($update_pages)
             or die(mysql_error());
    
    print "Page successfully updated.<br />";
        print "<a href=http://jhoward1.userworld.com/pages.php>Return to Pages...</a>";
    mysql_close($db);
}
 
else { print "Data was NOT entered due to errors.<br />"; }
 
?>
 
Any thoughts?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: updating mySQL table from form - stopped working

Post by RobertGonzalez »

trying adding a call to mysql_error in there. That will tell you a lot when there are errors. Right now you'd never know.

Also, try using mysql_real_escape_string() instead of addslashes. And lastly, that check_input function should be in an include rather than in two pages. And the function itself could be rewritten to:

Code: Select all

<?php
function check_input($array) {
    return isset($array['isbn']) && isset($array['title']) && isset($array['author']);
}
?>
The way you are doing it now will throw errors every time the post array indeces being checked in that function are not used. It will also return false for an empty input field.
iG9
Forum Commoner
Posts: 38
Joined: Fri Jul 18, 2008 2:11 pm

Re: updating mySQL table from form - stopped working

Post by iG9 »

Thanks for the help. mysql_error() illuminated my darkness. Seems I added a column and forgot to update my INSERT, so I was getting a mismatch. All is now well. Thanks again. :P
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: updating mySQL table from form - stopped working

Post by RobertGonzalez »

Glad I could help.
Post Reply