Error inserting form data into database via PHP and SQL

Discussion in 'PHP' started by depiction, Jul 14, 2008.

  1. #1
    I have a form that is using the following for action in a separate PHP file. It basically connects to the database and inserts the entered information from the form into the database.

    The form fields are named: link,name,title,date. It is connecting fine to the database, however it always results to "Error Entering New News Link Code 1" when trying to fill out the form (add new entry to database). I'm pretty sure the syntax for the SQL Insert command is correct. Is there something I'm not doing right?

    
    				$db = mysql_connect("localhost:8443","username,"password");
    
    
    				mysql_select_db("database",$db);
    						
    				$date = mktime(0,0,0,$month,$day,$year);
    				
    				$Query = "INSERT INTO links " .
    					"(url,name,description,date) " .
    					"VALUES(\"$link\",\"$name\",\"$title\",$date)";
    
    				if(!($result = mysql_query($Query,$db)))
    					print("<p><font face=\"Arial, Helvetica, sans-serif\" class=\"header2\" align=center>Error Entering New News Link Code 1.</font></p>");
    				else
    					print("<p><font face=\"Arial, Helvetica, sans-serif\" class=\"header2\" align=center>News link added to database successfully.</font></p>");
    
    Code (markup):
    I can not replicate it, but once during my troubleshooting it claimed it had sucessfully added the link. However I then went in the database and it appeared that all fields for the entry were empty, except for the date which had a value of "-1".
     
    depiction, Jul 14, 2008 IP
  2. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #2
    replace your query to this.

    $Query = "INSERT INTO links " .
    "(url,name,description,date) " .
    "VALUES(\"$link\",\"$name\",\"$title\",\"$date\")";
     
    php-lover, Jul 14, 2008 IP
  3. mshore

    mshore Peon

    Messages:
    416
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You are using an assignment operator instead of a comparison operator in your if statement. It should look like this:

    if($result != mysql_query($Query,$db))
      print("<p><font face=\"Arial, Helvetica, sans-serif\" class=\"header2\" align=center>Error Entering New News Link Code 1.</font></p>");
    else
      print("<p><font face=\"Arial, Helvetica, sans-serif\" class=\"header2\" align=center>News link added to database successfully.</font></p>");
    Code (markup):
    What you were doing is setting $result equal to mysql_query($Query, $db), so you were always taking the first path. If you use if($result != mysql_query($Query, $db)) then the comparison should work.

    Check out http://us3.php.net/manual/en/language.operators.comparison.php for details on comparison operators.
     
    mshore, Jul 14, 2008 IP
  4. depiction

    depiction Active Member

    Messages:
    822
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Thanks. Changing it to a conditional statement solved the error message. It now says that it has submitted successfully (even though it doesn't).

    The second problem is still an issue. When I check the database it did create a new entry with an ID#. However all fields (url, name, and description) have no values and the date is set to "-1".
     
    depiction, Jul 14, 2008 IP
  5. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #5
    if(!($result = mysql_query($Query,$db)))

    That line is still correct, that is not a comparing process. It's an assigning the variable $result to the resultset that will return from the query.

    pls post your table structure.
     
    php-lover, Jul 14, 2008 IP
  6. depiction

    depiction Active Member

    Messages:
    822
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #6
    Here is the table structure.

    Field Type Null Default
    ID int(11) No
    url varchar(255) No
    name varchar(100) No
    description varchar(255) No
    archived smallint(6) No 0
    date int(11) No 0

    The form uses 3 fields:
    textarea with name="title"
    input name="link"
    input name="name"

    and three additional date fields.
    select name=month
    select name=day
    select name=year

    The form action is most of the code from the first post in an external php file.
     
    depiction, Jul 15, 2008 IP
  7. depiction

    depiction Active Member

    Messages:
    822
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #7
    I tried changing "VALUES(\"$link\",\"$name\",\"$title\",\"$date\")"; to "VALUES('$link','$name','$title','$date')";, however I am still getting null values entered into the database. Any suggestions?
     
    depiction, Jul 15, 2008 IP
  8. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #8
    Make sure that your id field is auto_increment

    Your date data type is integer, so we don't need quote for date.

    $Query = 'INSERT INTO links (url,name,description,date) VALUES("'.$link.'","'.$name.'","'.$title.'",'.$date.')';
     
    php-lover, Jul 15, 2008 IP
  9. depiction

    depiction Active Member

    Messages:
    822
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #9
    Everytime form data was used as a variable, it claimed the variable was undefined even though the field's name was set in the form.

    I found http://matthom.com/archive/2005/02/19/php-passing-variables-across-pages, so I added the following to my code. Everything works great now.

    
    $title = $_POST['title'];
    $link = $_POST['link'];
    $name = $_POST['name'];
    $month = $_POST['month'];
    $day = $_POST['day'];
    $year = $_POST['year'];
    $result = "";
    
    PHP:
     
    depiction, Jul 15, 2008 IP