-1

Inserting data to mysql database through php.

<?php

    session_start();

    $username1=$_GET['uname'];
    $pwd1=$_GET['pwd'];


    mysql_connect("localhost","root","")or die("cannot connect to my sql");
    mysql_select_db("webapp")or die("cannot connect database webapp");

    // CHANGE QUERY FOR INSERT
    $sql="INSERT INTO login (uname, pwd) VALUES ('$username1', '$pwd1')";



    //$sql = "INSERT INTO login ".
         //  "(uname,pwd) ".
           //"VALUES ".
           //"('$username1','$pwd1')";


    // EXECUTE QUERY AND GET RESULT IN $RESULT
    $result=mysql_query($sql);

    //THIS TIME WE ARE NOT CHECKING $COUNT
    if($result)
    {
    echo "You have successfully Inserted your new record";
    }
    else
    {
    echo "Operation Failure please re-attempt";
    }

?>

When i execute this script it is not inserting the values to the database.. Database view in XAMPP

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
Ahsan Jamal
  • 218
  • 1
  • 3
  • 17

1 Answers1

0

Your code seems perfectly able to function as you have intended. Other users have mentioned that it would be better practise to use POST instead of GET. I would also add that you should sanitise those inputs.

I have replicated your code on my own server. It works fine. I would therefore recommend to you to revisit the fact you are not using a password for your root user (Connecting to mysql in xampp without password). I would double check that the reference table names and fields are correct. I would double check permissions to write to the database and possibly print out php errors to the page....

error_reporting(E_ALL);  ini_set('display_errors', 1);

Finally, turn on mysql logging if not already on: http://www.pontikis.net/blog/how-and-when-to-enable-mysql-logs

check your web server log (depends on your set-up) and server log to see if any errors are being outputted to the logs. Your code should work.

And also just print out your query string to see if it is coming out as expected:

print($sql); exit;

I wonder if you are getting anything printed to screen or if you are actually getting "You have successfully Inserted your new record". Of course that would be the first port of call that something is wrong. Perhaps $result is always true/contains resource data no matter if insertion to mysql worked or not. I would also therefore print out the result resource array to see what was actually returned.

Community
  • 1
  • 1