GIDForums

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 17-Mar-2005, 09:08
robsmith robsmith is offline
New Member
 
Join Date: Mar 2005
Posts: 11
robsmith is on a distinguished road
Unhappy

Please help! Insert into database problem


Hi all, I am pretty new to PHP programming and and have a problem with inserting customer data into my MySQL database, I think its probably a really obvious problem, but I've been staring at the same code for hours and its driving me mad
This code fragment is supposed to insert all the customer data into my Customer database. Each time I try, it returns to the clients form saying "The client has been saved" but it hasn't.
I would greatly appreciate any help x

Here's the code:

PHP Code:

//Check to make sure the data has been entered before saving
function SaveClient(&$dbconnection)
{
    //If there are empty fields: a message is displayed
    if (empty($_POST['title']) || empty($_POST['firstname']) || empty($_POST['surname']) || empty($_POST['email']) || empty($_POST['addressline1']) || empty($_POST['town']) || empty($_POST['county']) || empty($_POST['postcode']) || empty($_POST['homephone']) || empty($_POST['dateofbirth']) || empty($_POST['username']))
        return "<p>You must fill in all fields!<br />".AddClientForm(); //Display the form to add a client
        
    $title = EscapeData($_POST['title'], $dbconnection);
    $firstname = EscapeData($_POST['firstname'], $dbconnection);
    $surname = EscapeData($_POST['surname'], $dbconnection);
    $email = EscapeData($_POST['email'], $dbconnection);
    $addressline1 = EscapeData($_POST['addressline1'], $dbconnection);
    $town = EscapeData($_POST['town'], $dbconnection);
    $county = EscapeData($_POST['county'], $dbconnection);
    $postcode = EscapeData($_POST['postcode'], $dbconnection);
    $homephone = EscapeData($_POST['homephone'], $dbconnection);
    $dateofbirth = EscapeData($_POST['dateofbirth'], $dbconnection);
    $username = EscapeData($_POST['username'], $dbconnection);
    
    //Query to show if the user name already exists
    $query = "SELECT username FROM Customer WHERE (username = '$username')";
    $result = @mysql_query($query); //Run the query.
    if ($result) //If it ran ok, display the records.
    {
        if (mysql_num_rows($result) > 0) //The client name already exists
            return "<p>Unfortunately that user name already exists, please choose another</p><br />".AddClientForm(); //Display the form to register the client
        mysql_free_result($result); //Free up the resources.
    }
    //Checks that there are no invalid characters in the title
    if (!preg_match("/^[A-Za-z' -]{2,50}$/", stripslashes(trim($title))))
    return "<p>Please, enter a valid title!</p><br />".AddClientForm(); //Display the form to add a new client
    
    //Client forename checking
    if (!preg_match("/^[^';=]{5,255}$/", $firstname)) //Security (for avoiding malicious entries)
    return "<p>Please enter a valid forename!</p><br />".AddClientForm(); //Display the form to add a new client
    
    //The passwords are checked to see if they are the same
    if (strcmp($_POST['password1'], $_POST['password2']) != 0)
    return "<p>Your password did not match the confirmed password, please try again</p><br />".AddClientForm(); //Display the form to add a client

    //Checks that there are no invalid characters in the password
    $password = EscapeData($_POST['password1'], $dbconnection);
    if (!preg_match("/^\w{5,16}$/", $password))
    return "<p>Please, enter a valid password!</p><br />".AddClientForm(); //Display the form to add a client
    
    else {
        $query = "INSERT INTO Customer (title, firstname, surname, email, addressline1, addressline2, town, county, postcode, homephone, dateofbirth, workphone, mobile, username) " .
                "VALUES ('$title', '$firstname', '$surname', '$email', '$addressline1', '$addressline2', '$town', '$county', '$postcode', '$homephone','$dateofbirth', '$workphone', '$mobile', '$username', PASSWORD('$password'))";
        @mysql_query($query);
        return "<p>The client has been saved</p>".ClientsForm(); //Display the form with all clients
        echo $firstname;
        }
} 


  #2  
Old 24-Apr-2005, 03:44
cyner cyner is offline
New Member
 
Join Date: Apr 2005
Posts: 4
cyner is on a distinguished road
You're not checking if mysql_query is successful, so of course you won't know what happens. (In fact, by using the @ in front of mysql_query you're suppressing all warnings. You shouldn't use it while debugging.) Use something like this instead:

mysql_query($query) or die("Something went wrong; ".mysql_error());
 

Recent GIDBlogUpdates On The All New Toyota VIOS - Part III by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Limit combo box and date time picker choice according to database created in folder shinyhui CPP / C++ Forum 0 22-Feb-2005 20:16
Limit combo box and date time picker choice according to database created in folder shinyhui MS Visual C++ / MFC Forum 0 22-Feb-2005 02:13
Insert problem in linked list with two function code Kay Chan CPP / C++ Forum 1 03-Sep-2004 09:52
insert data into database jilshi Web Design Forum 0 29-Apr-2004 22:47
urgent help needed :c + mysql insert jack C Programming Language 1 13-Apr-2004 21:16

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:48.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.