0
  Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in C:\xampp\htdocs\arun\login.php on line 9

Code:

$db_select = mysql_select_db('my_db', $con);
$username = $_GET['username'];
$password = $_GET['password'];
$role = $_GET['role'];
$result = mysql_query("Insert into  table1(Username,Password,Role) values (Username='$username', Password='$password',Role='$role') ",$con);
$row = mysql_fetch_array($result);

if ( mysql_error() )
 {
  die ( mysql_error());
}
$row_PK = mysql_fetch_assoc($row);
mysql_close($con);
?>
Satish Sharma
  • 9,547
  • 6
  • 29
  • 51
  • [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Itay Feb 24 '14 at 05:47
  • did you googling how to insert data to database in php ? this is really effortless question. put some effort from your end then come to us – Awlad Liton Feb 24 '14 at 05:49
  • @MKhalidJunaid - No, not really – Itay Feb 24 '14 at 05:52

4 Answers4

1

Replace

$result = mysql_query("Insert into  table1(Username,Password,Role) values (Username='$username', Password='$password',Role='$role') ",$con);

with

$result = mysql_query("Insert into  table1(`Username`,`Password`,`Role`) values ('$username', '$password','$role') ",$con);

You don't have to assign the value to the column on your INSERT query as you have done. Just map the values as shown above.

Apart from the above issues... Dissecting your code ..

The main issue with your code is you are doing an INSERT but you are expecting a resultset as you have done a SQL SELECT which is wrong.

You need to change your code like this..

The below code is just an illustration to tell you what you had done was actually wrong. Do not use this code ! You first need to migrate to the latest database API provided for you in the side note section.

$db_select = mysql_select_db('my_db', $con);
if(isset($_GET['username'],$_GET['password'],$_GET['role']))
{
$username = mysql_real_escape_string($_GET['username']);
$password = mysql_real_escape_string($_GET['password']);
$role =     mysql_real_escape_string($_GET['role']);
$result = mysql_query("Insert into  table1(`Username`,`Password`,`Role`) values ('$username', '$password','$role') ",$con);
if (!$result)
{
    die (mysql_error());
}
else
{
    echo "Record Inserted";
}
mysql_close($con);
}

SideNote

The (mysql_*) extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead,the MySQLi or PDO_MySQL extension should be used. Switching to PreparedStatements is even more better to ward off SQL Injection attacks !

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
1

For a INSERT query the return value isn't a resource, it's a TRUE on success or FALSE on error.

A resource is returned for a SELECT, SHOW, DESCRIBE or EXPLAIN query.

user210584
  • 111
  • 2
0

this is your solution change your query to

$result = mysql_query("Insert into table1(Username,Password,Role) values ('$username', '$password','$role') ",$con);

Nishant Solanki
  • 2,119
  • 3
  • 19
  • 32
-1

your query should be like this

$result = mysql_query("Insert into  table1(Username,Password,Role) values ('$username', '$password','$role') ",$con);

NOTE: mysql_* is deprecated use mysqli_* OR PDO

Satish Sharma
  • 9,547
  • 6
  • 29
  • 51