www.webdeveloper.com
Results 1 to 4 of 4

Thread: nothing being sent to the database except the ID

Hybrid View

  1. #1
    Join Date
    May 2004
    Posts
    288

    Question nothing being sent to the database except the ID

    For some reason when i submit a form data to the database, that that seems to get stored is the random ID

    Ive attached the PHP code below that im using... can you see any issues with the code that might be stoping both email addreses, name and message not being store in the database


    PHP Code:
    // connect to database
    $link mysql_connect ($host$user$password);
    mysql_select_db ($dbname$link);

    // creates a random number for the id, ans check to see if the random number currently exists in database
    $success FALSE

    while(
    $success == FALSE) { 
        
    $rand rand(100000999999); 

        
    $q "SELECT * FROM $tablename WHERE rand = '$rand'"
        
    $r mysql_query($q$link);

        echo 
    mysql_error();
     

        if(
    mysql_num_rows($r)) { //id exists 
            
    continue; 
        } else { 
            
    $success TRUE
        } 


    // insert your data here with $rand as the id
    $firstname mysql_real_escape_string($_POST['youremail']);
    $lastname mysql_real_escape_string($_POST['name']);
    $email mysql_real_escape_string($_POST['receiveremail']);
    $datepicker mysql_real_escape_string($_POST['message']);

    $query="INSERT INTO $tablename (rand, youremail, name, receiveremail, message) 
      VALUES ('"
    .$rand."', '".$youremail."', '".$name."', '".$receiveremail."',   '".$message."')";
      
     
    mysql_query($query) or die (mysql_error()); 

  2. #2
    Join Date
    Aug 2012
    Posts
    155
    Look at your code closely, there is a glaring error:
    PHP Code:
    $firstname mysql_real_escape_string($_POST['youremail']); 
    $lastname mysql_real_escape_string($_POST['name']); 
    $email mysql_real_escape_string($_POST['receiveremail']); 
    $datepicker mysql_real_escape_string($_POST['message']); 

    $query="INSERT INTO $tablename (rand, youremail, name, receiveremail, message)  
      VALUES ('"
    .$rand."', '".$youremail."', '".$name."', '".$receiveremail."',   '".$message."')"
    In your query you are trying to insert the values for variables $youremail, $name, $receiveremail, $message - yet nowhere in your script do you have these variables declared and assigned values. What you do have are the variables $firstname, $lastname, $email, $datepicker (which don't make any sense in themselves as they don't relate to the posted data) and therefor it should be those variables that you use in your query.

    The $rand number is being inserted as you declare it and assign a value early on in your script.

  3. #3
    Join Date
    May 2004
    Posts
    288
    oh dear, it was late last night, must be why i missed this

  4. #4
    Join Date
    Aug 2012
    Posts
    155

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles