Current location: Hot Scripts Forums » Programming Languages » PHP » Problem with adding content to mysql from php


Problem with adding content to mysql from php

Reply
  #1 (permalink)  
Old 01-10-04, 10:30 AM
HasansWeb HasansWeb is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Problem with adding content to mysql from php

It's a simple page for adding users to the mysql database. I don't understand why it doesn't add the content. It doesn't show the error messege I set with the conditional (if it doesn't add to the database). It says the user has been added. But when I check in phpmyadmin, it's not there.


__________________________________________________ ______
PHP Code:

<?php


$page_title 
"Add User";
$error NULL;

if (isset(
$_POST['submit'])) {

    if (empty(
$_POST['firstname'])) {
    
$fn FALSE;
    echo 
"<font face='verdana' size='1'><b>Error:</b> Please enter your first name!</font><br><br>";
    } else {
    
$fn TRUE;
    }

    if (empty(
$_POST['lastname'])) {
    
$ln FALSE;
    echo 
"<font face='verdana' size='1'><b>Error:</b> Please enter your last name!</font><br><br>";
    } else {
    
$ln TRUE;
    }    

    if (empty(
$_POST['username'])) {
    
$u FALSE;
    echo 
"<font face='verdana' size='1'><b>Error:</b> Please enter your username!</font><br><br>";
    } else {
    
$u TRUE;
    }

    if (empty(
$_POST['email'])) {
    
$e FALSE;
    echo 
"<font face='verdana' size='1'><b>Error:</b> Please enter your email address!</font><br><br>";
    } else {
    
$e TRUE;
    }    

    if (
strlen($_POST['password1']) < 6) {
    
$p FALSE;
    echo 
"<font face='verdana' size='1'><b>Error:</b> Your password must be a minimum of 6 characters!</font><br><br>";
    } else {

        if (
$_POST['password1'] == $_POST['password2']) {
        
$p TRUE;
        } else {
        
$p FALSE;
        echo 
"<font face='verdana' size='1'><b>Error:</b> You didn't confirm your password or it didn't match!<br><br></font>";
        }
    }

if (
$fn && $ln && $u && $e && $p) {


require_once(
"mysql_connect.php");
$query "INSERT INTO `users` (`user`, `password`, `firstname`, `lastname`) VALUES (
VARCHAR(`
$u`), VARCHAR(PASSWORD(`$p`)), VARCHAR(`$fn`), VARCHAR(`$ln`))";
$result mysql_query($query);
    if (
$result TRUE) {

    
$body "Congratulations! Your username and password have been added to the database! You may now log in! \n\n Your username is '$u' \n Your password is '$p' \n Thanks!";
    
mail ($_POST['email'], 'User Account created!'$body'From: [email]admin@blah.com[/email]');
    
    echo 
"<font face='verdana' size='1'>New user account has been created!</font>";

    echo 
"<br><br><a href=\"index.php\">Return</a>";

    } else {
    echo 
"Unable to create new user! mysql_error()";
    }


        }

} else {
?>

<form name="adduser" action="<?php echo $_SERVER['PHP_SELF'?>" method="post">
<?php echo $error?>
<table border="0" cellpadding="4" cellspacing="0" style="border-collapse: collapse" width="300">

<tr><td colspan="2">
<b><font size="2">Add User</font></b>
<br><br>
Fusion Red will automatically send you a copy of your username & password via email once you have entered it. Be sure to save and/or write it somewhere in a safe place.
</td>
</tr>

<tr>
<td valign="top"><b>First Name:</b></td>
<td><input type="text" name="firstname" size="20" maxlength="20" value="<?php if (isset($_POST['firstname'])) echo $_POST['firstname'?>" />
</td>

</tr>

<td valign="top"><b>Last Name:</b></td>
<td><input type="text" name="lastname" size="20" maxlength="20" value="<?php if (isset($_POST['lastname'])) echo $_POST['lastname'?>" />
</td>

</tr>

<tr>

<td valign="top"><b>User Name:</b></td>
<td><input type="text" name="username" size="20" maxlength="20" value="<?php if (isset($_POST['username'])) echo $_POST['username'?>" />
</td>

</tr>

<tr>

<td valign="top"><b>Email Address:</b></td>
<td><input type="text" name="email" size="20" maxlength="50" value="<?php if (isset($_POST['email'])) echo $_POST['email'?>" />
</td>

</tr>

<tr>

<td valign="top"><b>Password:</b><br><i>(Minimum 6 characters)</i></td>
<td><input type="password" size="20" maxlength="20" name="password1">
</td>

</tr>

<tr>

<td valign="top"><b>Confirm Password:</b></td>
<td><input type="password" size="20" maxlength="20" name="password2">
</td>

</tr>

<tr>

<td colspan="2"><center>
<input type="submit" name="submit" value="Add User"> <input type="reset" name="reset" value="Reset"></center>
</td>

</tr>

</table>
</form>

<?php
}
?>
Reply With Quote
  #2 (permalink)  
Old 01-10-04, 02:20 PM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi there,

I believe it is not necessary to specify the data type (VARCHAR) in INSERT statement, though I am not totally sure if it is the cause of your problem.

Check out the INSERT syntax here:
http://www.mysql.com/doc/en/INSERT.html

One debug approach I would always take is to output the SQL statement and see if it is what I expected. If so, then type it directly into MySQL and see what kind of error it gives me.

HTH.
__________________
Blavv =|
Reply With Quote
  #3 (permalink)  
Old 01-10-04, 04:03 PM
Agum Agum is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Here in your code:
Code:
$result = mysql_query($query); 
    if ($result = TRUE) {
it should be:
Code:
$result = mysql_query($query); 
    if ($result === true) {
a single = sign means you're assigning the value "true" to the variable $result, thus making the condition always true. So it goes there no matter the result actually succeeds or fails. a double = sign works as a comparison, but a triple = sign is safer as it also checks the type of the variable.
Reply With Quote
  #4 (permalink)  
Old 01-10-04, 05:26 PM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hello,

Oops I missed that.

Alternatively, you can use:

PHP Code:

$result mysql_query($query); 

    if (
$result) { 
to see whether it was successful, and prepend exclamation mark (!) to $result for testing the opposite.

Good luck.
__________________
Blavv =|
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Error when trying to create MySQL table via PHP HasansWeb PHP 5 05-19-11 06:59 AM
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 02:22 AM
PHP Search script problem vispa PHP 1 11-22-03 12:22 PM
MySQL with PHP question. HELP for a newbie kenfused PHP 3 08-02-03 12:53 AM
Free PHP, Perl and possible mySQL hosting! toby General Advertisements 8 06-17-03 01:04 AM


All times are GMT -5. The time now is 08:25 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.