SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    MySQL query won't insert no error message

    hi I'm new here with a real simple newbie problem I just can't solve. I've a connection to my database but can't the query to insert, or get an error message back. I have other 'select' scripts that work so the connection works. What am I doing wrong??? and why don't I get any errors returned?
    TIA

    PHP Code:

    <html>
    <head>
    <title>Input Links</title>
    </head>
    <body>
    <H1 align=center><FONT color=blue>Web Content Control Panel</FONT></H1><br>

    <TABLE align=center>
    <TR>
        <TD>
    <?php
    //ini_set('display_errors',1); 
    //error_reporting(E_ALL);
     
     

    //connect to database
    $db mysql_connect("localhost""user""*******") or die ('I cannot connect to the database because: ' mysql_error());
    mysql_select_db ("my_db",$db); 


    if (
    $submit) {

            
    $sql "INSERT INTO links (link_desc, link_url) VALUES ('$link_desc','$link_url')";

        
    // run SQL against the DB
        
    $result mysql_query($sql,$db);
        if (!
    $result) {
        die(
    'Invalid query: ' mysql_error());
    }

       echo 
    "<h3 align=center>Link Entered!</h3><br>";
       
    // Close the database connection
        
    mysql_close();
        } 
    ?>

     </td></tr>
    <tr><td>

      
    <!-- display form for links entry  -->
        <form method="post"  name="myForm" action="<?php echo $_SERVER['PHP_SELF'?> " >
     <TABLE align=center border=1>

      <TR><TD align=right>Link Description :</TD><TD><input type="Text" name="link_desc" size="100" ></TD></TR>

     <TR><TD align=right> URL:</TD><TD><input type="Text" name="link_url" size="50" ></Textarea></TD></TR>

       <TR><TD><input type="Submit" name="submit" value="Add Link"></TD><TD></TD></TR>
    </TABLE>
           
      </form>
    </body>
    </html>
    Last edited by SpacePhoenix; Mar 30, 2011 at 00:21. Reason: placed php tags around the php code

  2. #2
    Non-Member
    Join Date
    Apr 2004
    Location
    Miami, FL, USA
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try changing "if($submit)" to this:
    PHP Code:
    if (isset($_POST["submit"])) {
        print(
    "OK!");
        
    $sql = ... etcetc

  3. #3
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thankyou very much transio!! The SQl query now runs against the database but entering null values. This must be a PHP problem. At least now I've got something to work with. I'll get a chance to work on this later after work. Thanks again for the quick response.

  4. #4
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorted! The PHP post needed to be changed to $_POST[link_desc] to enter a value to the db.

  5. #5
    Non-Member
    Join Date
    Apr 2004
    Location
    Miami, FL, USA
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good deal.

  6. #6
    SitePoint Wizard guelphdad's Avatar
    Join Date
    Oct 2003
    Location
    St. Catharines, ON Canada
    Posts
    1,706
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Look in the PHP manual on how to use MYSQL_REAL_ESCAPE_STRING to make sure you are only sending clean data to your database.

  7. #7
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the heads-up on that guelphdad, I'm still just trying to get the thing to work, then I need to clean things up with untainting user input and cleaning input data. Cheers


Bookmarks

Posting Permissions

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