Results 1 to 6 of 6

Thread: [RESOLVED] MySQL Insert Problem

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2007
    Location
    Karachi
    Posts
    551

    Resolved [RESOLVED] MySQL Insert Problem

    hello to everYone!!

    i m using following code for inserting the the data in my sql there is no syntax error no exception but the data is not inserting in the database

    i also tried by $_POST instead of $_REQUEST but no result !

    PHP Code:
    <?
    ini_set("display_errors", 0);
    if (isset($_REQUEST['submit'])) {
    $name =$_REQUEST["username"];

    echo $name;
    $dbh=mysql_connect ('localhost','root','mypass');
    mysql_select_db ("newdatabase");

    mysql_query("insert into  newtable (name) values($name)") 
    or die(mysql_error());  
    echo "Data Inserted!";

    }

    if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form
    ?>
    <html>
    <body>
    <form method="post" action="<?php echo $PHP_SELF;?>">
    <table>
    <tr>
    <td>Please enter your name:</td>
    <td> <input type="text" name="username" /></td>
    </tr>
    <td>Please enter your name1:</td>
    <td> <input type="text" name="username1" /></td>
    </tr>
    <tr><td><input type="submit" value="submit" /></td></tr>
    </table>
    </form>

    <?
    }
     else {
      echo "Hello, " .$_REQUEST['username'];  

    }
    ?>
    Hope any one would reply
    Last edited by penagate; Jul 18th, 2007 at 10:07 AM. Reason: added code tags

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: MySQL Insert Problem

    Split into its own thread from this thread.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: MySQL Insert Problem

    There are a couple of problems with your script.
    Let's start with the obvious: you have turned off error displaying. You say there is no error, but this is probably because you've suppressed them all. Remove this line:
    PHP Code:
    ini_set("display_errors"0); 
    Replace it with this:
    PHP Code:
    error_reporting(E_ALL E_STRICT); 
    Secondly: you are inserting $name directly into the query, which comes from a request variable. This is bad and is a SQL injection vulnerability. You might be able to get away with it if you have magic quotes on, but that is a deprecated "feature" that should not be used.
    Instead, use mysql_real_escape_string($name) to ensure that any funny characters are escaped and will not mess up the SQL query.
    Better yet, find yourself a data access abstraction library, like PDO. But that's a topic for another day, once you've got this working.

    Thirdly: use $_GET and $_POST rather than $_REQUEST. $_REQUEST ignores the semantic difference between GET and POST methods and can possibly lead to problems with people manipulating URLs. Avoid its use.

    Fourth: the opening <?php tag should be preferred to <?, which is not recommended for use in a production situation as it is not always supported. All tags apart from <?php ... ?> are deprecated.

    With those changes made, give the script another whirl and let us know how it goes.


    Moved from Database Development.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2007
    Location
    Karachi
    Posts
    551

    Re: MySQL Insert Problem


    First of i m very thankful to u for replying

    here i m mailing the code again infact the problem is still reside i have follow

    all the step that u told me but still no error and no data is inserting


    VB Code:
    1. <?php
    2. error_reporting(E_ALL | E_STRICT);
    3. if (isset($_POST['submit'])) {
    4. $name1 =$_POST["username"];
    5. $name1=mysql_real_escape_string($name1)
    6. echo $name1;
    7. $dbh=mysql_connect ('localhost','newuser','mypass');
    8. mysql_select_db ("newdatabase");
    9.  
    10. mysql_query("insert into  newtable (name) values($name1)
    11. or die(mysql_error());  
    12. echo "Data Inserted!";
    13.  
    14. }
    15.  
    16. if (!isset($_POST ['submit'])) { // if page is not submitted to itself echo the form
    17. ?>
    18. <html>
    19. <body>
    20. <form method="post" action="<?phpphp echo $PHP_SELF;?>">
    21. <table>
    22. <tr>
    23. <td>Please enter your name:</td>
    24. <td> <input type="text" name="username" /></td>
    25. </tr>
    26. <td>Please enter your name1:</td>
    27. <td> <input type="text" name="username1" /></td>
    28. </tr>
    29. <tr><td><input type="submit" value="submit" name="submit"></td></tr>
    30. </table>
    31. </form>
    32.  
    33. <?php
    34. }
    35.  else {
    36.   echo "Hello, " .$_POST ['username'];  
    37.  
    38. }
    39. ?>

    u can correct my code i m still in ambiguity and doing something wrong

  5. #5
    Lively Member
    Join Date
    Jul 2007
    Posts
    78

    Re: MySQL Insert Problem

    hi
    i think there is problem in line
    Code:
    mysql_query("insert into  newtable (name) values($name1)
    but i dont know php
    like in vb or asp when u insert value of variable it is concatenated with the query
    like

    Code:
    mysql_query("insert into  newtable (name) values(" + $name1 + ")
    This will be error Hope so.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2007
    Location
    Karachi
    Posts
    551

    Re: MySQL Insert Problem

    my problem has been resolve in this way

    PHP Code:

    <?php
    ini_set
    ("display_errors"0);


        if (isset(
    $_POST['submit'])) {
        
    $Loginname $_POST["Loginname"];
        
    $Password1 $_POST["Password1"];
    }

    if (!isset(
    $_POST['submit'])) { // if page is not submitted to itself echo the form


    ?>


    <html>
    <head>
    <title>
    </title>
    </head>
    <body>
    <form method="post" action="<?php echo $PHP_SELF;?>">
    <table  width="50%"   border="1px" align="center">

    <tr bgcolor="Teal" width="100%" align="center" colspan=2>
    <td align="center">User Login</td>
    </tr>

    <tr bgcolor="Biege">
    <td>Enter Login name</td>
    <td><input type="text" name="Loginname"></td>
    </tr>

    <tr bgcolor="Biege">
    <td>Enter Password</td>
    <td><input type="text" name="Password1"></td>
    </tr>

    <tr bgcolor="Biege">
    <td><input type="submit" value="Submit" name="submit"></td>

    </tr>
    </table>
    </form>

    <?
    } else {

    $dbh=mysql_connect ('localhost','root','mypass');
    mysql_select_db ("class");

    mysql_query("INSERT INTO information  (firstName) VALUES ('$_POST[Loginname]')");
    }

    ?> 

    </body>
    </html>

Posting Permissions

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



Click Here to Expand Forum to Full Width