ali shahid ali shahid - 7 months ago 45
HTML Question

Array to string conversion in php check my code

<html>
<head>
</head>
<body>
<form action="mysql.php" method="post">
First Name: <input type="text" name="fname"></br>
last Name: <input type="text" name="lname">< </br>
What is your favrite subject <input type="text" name="subject"></input> </br>
Your Age : <input type="text" name="age"></input> </br>
<input type="submit" name="submit" ></input>
<input type="reset" name="rs"></input>
</form>
<?php
if (isset($_POST\['submit'\]) ){
$_sa = mysql_connect( "localhost","Ali","pakistan");
if (!$_sa){
die("can not caonnect".msql_error());
}

/* ----------- condition ----------------------
if (empty($_POST\[fname\])) {
echo "First name required";
}
------------------condition end ----------------- */


mysql_select_db("google", $_sa );

line 46-- $sql = "INSERT INTO info (firstname,lastname,subject,age) VALUES ('$_POST \[fname\]','$_POST \[lname\]','$_POST \[subject\]', '$_POST \[age\]'')";

mysql_query($sql,$_sa);
mysql_close($_sa);
}

?>][1]


// when i run my code its give me error of


Notice: Array to string conversion in C:\xampp\htdocs\mysql.php on
line 46

Notice: Array to string conversion in C:\xampp\htdocs\mysql.php on
line 46

Notice: Array to string conversion in C:\xampp\htdocs\mysql.php on
line 46

Notice: Array to string conversion in C:\xampp\htdocs\mysql.php on
line 46

Answer

Try this:

$sql = "INSERT INTO info (firstname,lastname,subject,age) VALUES ('{$_POST ['fname']}','{$_POST ['lname']}','{$_POST ['subject']}', '{$_POST ['age']}')";

Explanation:

$_POST is an array So, you need to take care of it while accessing

And, If arrays are accessed inside String, then we should use {} For example: {$_POST['subject']}