Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2009
    Posts
    57
    Thanks
    6
    Thanked 0 Times in 0 Posts

    mysql insert error

    Hi. I think you all know me by now so I'll cut to the chase.
    Code:
    <?php
    $host="edited";
    $username="edited";
    $password="edited";
    $db_name="edited";
    $tbl_name="topic";
    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
    
    $name=$_POST['name'];
    $detail=$_POST['details'];
    
    $sql="INSERT INTO $tbl_name(topic, detail, datetime)VALUES('$name', '$detail', NOW())";
    $result=mysql_query($sql);
    
    if($result){
    header("location:site.html");}
    else{
    echo("I have failed you master.");}
    ?>
    Displayed error: "I have failed you master." Anyone know a possible cause? Thanks. Bye.

  • #2
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    Instead of "I have failed you master", get the PHP to echo the $sql string that you're sending to the database.

    As an aside, dropping unsanitised input straight into a database leaves you wide open to a SQL injection attack. At the very least, wrap $name and $detail in mysql_real_escape_string(),

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    PHP Code:
    $result=mysql_query($sql) or die(mysql_error()); 
    Quote Originally Posted by php.net
    it's your responsibility to die(), if necessary!
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Posting Permissions

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