PHP Tutorial 28 – MySQL Inserting Data (PHP For Beginners)




This tutorial takes a look at how to insert data into a MySQL Table and specify which fields to insert data into. There is no example of using this to insert …

Original source


45 responses to “PHP Tutorial 28 – MySQL Inserting Data (PHP For Beginners)”

  1. Hi, can you please check why my code keeps on giving me "not inserted" error.
    I tried to check the syntax but still the same error.

    <?php
    $con = mysqli_connect('********', '*********', '********');

    if (!$con) {
    echo 'Not connected to Server';
    }
    if(!mysqli_select_db($con,'***********'))
    {
    echo 'Database not selected';
    }
    $fname = $_POST['fname'];
    $mname = $_POST['mname'];
    $lname = $_POST['lname'];
    $position = $_POST['position'];
    $mobile1 = $_POST['mobile1'];
    $mobile2 = $_POST['mobile2'];
    $age = $_POST['age'];

    $sql = "INSERT INTO workers (fname, mname, lname, position, mobile1, mobile2, age,) VALUES ('fname','mname','lname','position','mobile1','mobile2','age')";

    if(!mysqli_query($con,$sql))
    {
    echo 'Not Inserted';
    }
    else
    {
    echo 'Inserted';
    }
    header("refresh:10000000; url=insertindex.html");

    ?>

  2. hi

    it says Parse error: syntax error, unexpected T_VARIABLE in /home/a8326296/public_html/hub/index.html on line 8

    here is my code

    <?php

    $accounts = mysql_connect("mysql6.000webhost.com", "a8326296_profile", "***********")
    or die (mysql_error());

    mysql_select_dl("users", $accounts)

    $sql = "
    INSERT INTO users (username, password) VALUES('Chris', 'Pass123')
    ";

    mysql_query($sql, $accounts)

    ?>

  3. sir what if im using
    $accounts=mysqli_connect("localhost", "root", "password") or die (mysqli_error());

    mysqli_select_db("accounts", $accounts);

    the error is
    Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in C:wampwwwMySQL.php on line 4

  4. please help… 
    Parse error: syntax error, unexpected end of file in C:xampphtdocsSampleinsertvalues.php on line 13

    <?php
    $con = mysql_connect("localhost","root","");

    if(!$con){
    die("can not connect".mysql_error());

    mysql_select_db("mydatabase",$con);

    $sql= "INSERT INTO mytable(user_id, firstname, lastname) VALUES('1','jaime','rivera')";

    mysql_query($sql,$con);

    mysql_close($con);

    ?>

    thanks!

  5. Hi brother…. your videos are very useful to me….
    I have one doubt that if we give data like this
    INSERT INTO user2 (USERNAME,PASSWORD,FIRST_NAME,LAST_NAME) VALUES('asdf', 'pwd'123', 'dasdsa', 'sdfsdfsd');
    it will truncate the password field upto pwd because php will take next ' as ending….how to overcome this problem….can you please clarify me this doubt….??
    thanku..

  6. <?php
    $con = mysqli_connect("localhost","root","password","dbname") or die(mysql_error());
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
    // Creating User
      $sql = "INSERT INTO `users2`
      (`ID`,
      `Username`,
      `Password`,
      `FirstName`,
      `LastName`) 
      VALUES (NULL,'user','password','fname','lname')";
    // Checking if created
      if (mysqli_query($con,$sql))
      {
      echo "User created successfully </br>";
      }
    else
      {
      echo "Error creating User: " . mysqli_error($con);
      }
    ?>  

    this is for guys "doesn't worked help me !". 🙂  
    didn't worked because there is new versions of mysql…
    and for mysql_connect i typed mysqli , and same on others…
    or maybe you have not finished your script, explame: forgoted ";" or something like that. 

  7. I spent 3 days trying to get this to work and yours is the only example that actually worked when i changed it to my details, thanks a lot, now to just get these fields into a user submittable form 😀

  8. To all the people that it isn't working for use this code in your text editor: 

    <?php
    $accounts = mysql_connect("localhost", "root", "password")
    or die(mysql_error()); 
    mysql_select_db("accounts", $accounts);

    $sql="
     INSERT INTO users2 (Username, Password, FirstName, LastName) VALUES('Name', 'Password', 'FirstName', 'LastName')
    ";
    echo $sql;
    if (!mysql_query($sql, $accounts))
    {
    die('Error: ' . mysql_error());
    }
    echo "<h1> 1 record added to database </h1>";
    ?>

    A quick tip for your future studying; the 'echo $sql' shows you the process and if it fails you can fix it, this is called 'debugging' and that simple line will help you vastly.

  9. hi , sorry 2 bother but i have a problem in this video and the last one because  i did everything as u said since the first video i even downloaded the same porgram u showed in the tutorials and am using windows 7 ultimate 64-bit but in the last 2 videos technically i couldn't  even add a group to the ((accounts)) database using php , so i can't even modify it or add values to it, the question is what do u think that the problem is?? thank u so much………….

  10. Hey, I tried to link the php page using GET method. And allowed the user to enter the username password etc using an html form.. I am able to link it to the php page which has the mysql query connected to it.. That's not working.. I mean I use the username and password types by the user and save it to the database.. Not working..

  11. hi thanks for your tut
    can you make a tutorial where i can read from .txt file and insert the data into mysql database

    this is my data:
    SEASON OF THE WITCH                            DONE
    RED DAWN                                                DONE
    ZERO DARK THIRTY                                   DONE
    THE HOBBITS                                            DONE
    THE BEST EXOTIC MARIGOLD HOTEL        DONE

    my database table is going to have 3 colones one for id one for movie one for done or not

  12. Parse error: syntax error, unexpected 'INSERT' (T_STRING) in C:xampphtdocstutorialsdata.php on line 9

    $accounts = mysql_connect("localhost", "root", "dolinar")
    or die(mysql_error());
    mysql_select_db("accounts, $accounts);
    $sql= "
    INSERT INTO users2(Username , Password , First , Last) VALUES('Chris','pass123','Chris','Johnson')
    ";
    mysql_query($sql, $accounts);

    Can anyone help me?

  13. $db_connection = mysql_connect("localhost", "root", "") or die(mysql_error());

    mysql_select_db("accounts", $db_connection);

    $sql = "
    INSERT INTO user2 (USERNAME,PASSWORD,FIRST_NAME,LAST_NAME) VALUES('asdf', 'pwd123', 'dasdsa', 'sdfsdfsd')
    ";

    mysql_query($sql, $db_connection);

  14. the mysql_connect(…) or die (mysql_error());
    is not working for me, no errors show up if i type the password wrong, so im not sure that i am even connected… maybe its the phpMyAdmin, config but i have no idea…
    any help?

  15. Sir Teachmecomputer can you make tutorial about making biometric in vb,net i love your tutorial and understand it. Hope you make a tutorial about vb,net biometric cause i cant find one tutorial about biometric in youtube thanks sir

Leave a Reply