SitePoint Sponsor

User Tag List

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

    Need help in PHP MySQL Query !!! [[URGENT]]!!!!

    Hey guys i'am basically making a database driven website and i need to provide my customers a interactive form on the website . When they enter their details their info should enter my database.
    for that purpose i firstly have connected my database to my webpage.
    now i want this :
    user should type firstname and lastname in a form and that should enter my fname and lname attribute in my table Test in the database. i tried doing this but it didnt work out



    I've made a html file ( k.html) whose code is below :

    <html>
    <body>

    <form action="insert.php" method="post">
    Firstname: <input type="text" name="firstname">
    Lastname: <input type="text" name="lastname">
    <input type="submit">
    </form>

    </body>
    </html>

    Now i've made an insert.php file whose code is below :


    <?php
    $c=mysql_connect("localhost","username","password") // (My personal)
    if(!$c)
    {
    echo("Unable to connect to the database at this time ");
    }
    else
    {
    echo("Connection to the database was succesfull ");
    }
    ?>



    <?php
    mysql_select_db("a6279515_nis",$c);
    if(!mysql_select_db("a6279515_nis",$c))
    {
    echo("Can't establish database");
    }
    ?>





    <?php
    $sql="INSERT INTO Test (fname,lname)VALUES('$_POST[firstname]','$_POST[lastname]');
    if (!mysqli_query($c,$sql))
    {
    die('Error: ' . mysqli_error());
    }
    echo "1 record added";

    mysqli_close($c);
    ?>




    The error which my page shows is that :



    Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in /home/a6279515/public_html/insert.php on line 21


    Warning: mysqli_error() expects exactly 1 parameter, 0 given in /home/a6279515/public_html/insert.php on line 23

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I realise you are in a hurry, so my advice is to go and carefully read your code again.

    Did you see how you called mysql instead of mysqli? That'd really halt things in their tracks.

    The other thing you could do -- which won't take long -- is to read about string concatenation in PHP.

    String quoting and string concatenation.

    Then look carefully again at your sql query string, can you see how it breaks some of these rules?

    To see how you are doing with this string concatenation challenge do this from time to time:

    PHP Code:
    echo $sql 
    If you are in a real hurry then try using other php debugging techniques, they will probably be faster than waiting for answers on a forum.


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
  •