在 php 中将数组转换为字符串检查我的代码


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]

当我运行我的代码时,它给我的错误

注意:在 C:''xampp''htdocs''mysql.php 中数组到字符串的转换 46号线

试试这个:

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

解释:

$_POST 是一个array因此,您需要在访问时照顾好它

而且,如果在 String 中访问数组,那么我们应该使用 {}例如:{$_POST['subject']}

末尾写了一个额外的单引号。